我正在尝试使用java swing创建一个图表,这是我刚才所说的。在我的程序中,我从文本文件中获取一些随机数,基于该数字将创建图形。我已经能够制作图表,但我需要添加一个滚动条来查看完整的图形,该图形足够长以适应生成的窗口。 任何人都可以修改或建议我一个非常简单的
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.*; // used to access the JFrame, JPanel, JLabel, and JScrollPane class
import java.awt.*; // used to access the Font class
public class NewLine {
MyPanel panel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Swing Paint Demo");
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel=new MyPanel();
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel {
private static final Color GRAPH_COLOR = Color.red;
private static final Color GRID_COLOR = Color.gray;
public MyPanel() {
JScrollPane scroll = new JScrollPane(this, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
//JLabel label = new JLabel("This is a JScrollPane Component");
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
void drawLines(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(GRID_COLOR);
for(int x =20 ; x <=1020; x +=25 ){
g2d.drawLine(x, 70, x, 500); // Y AXIS
}
for(int y =70 ; y <=500; y +=25 ){
g2d.drawLine(20, y, 1020, y); // X axis
}
g2d.setColor(GRAPH_COLOR);
int x = 20;
int diff = 5;
try{
FileInputStream fstream = new FileInputStream("data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
int counter = 0;
int prevPoint = 0;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//x +=5
if( counter == 0){
System.out.println (strLine);
//g2d.drawLine(x, 500-312, x +=15, Integer.parseInt(strLine));
}
counter ++;
g2d.drawLine(x , 500-prevPoint, x +=5, 500-Integer.parseInt(strLine));
prevPoint = Integer.parseInt(strLine);
}
//Close the input stream
br.close();
}
catch(Exception ec){}
}
public void paint(Graphics g) {
super.paint(g);
drawLines(g);
}
}
文本文件的值如下所示
360 334 327 313 312 302 301 294 292 289 283 287 278 282 275 277 274 273 273 268 273 266 273 266 270 267 267 ....
先谢谢。 阿努邦。
答案 0 :(得分:2)
替换
session_start()
通过
f.add(new MyPanel());
并替换此
JScrollPane scroll = new JScrollPane(
new MyPanel(),
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
);
f.add(scroll);
通过这个
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
答案 1 :(得分:2)
这个解析数据的东西应该在构造函数中:
try{
FileInputStream fstream = new FileInputStream("data.txt");
// ... etc.