如何解决在面板上而不是框架上添加DrawGraph
我正在使用此设置绘制简单的折线图,然后.add()
将其绘制在框架上。
我的问题是我想.add()
将其放入面板中,以便可以附加多个图形。
我该如何运行?
JPanel myPanel = new JPanel();
DrawGraph graph1 = new DrawGraph(graph_scores);
myPanel.add(graph1);
这是下面的frame.add()
的代码。但我要改用panel.add()
:
public void start_graph()
{
//Make random list of data
List<Integer> scores = new ArrayList<Integer>();
Random random = new Random();
int maxDataPoints = 16;
int maxscore = 20;
for (int i = 0; i < maxDataPoints ; i++) {
scores.add(random.nextInt(maxscore));
}
//This is the part where i want to change thanks!
DrawGraph graph1 = new DrawGraph(graph_scores);
JFrame frame1 = new JFrame("DrawGraph");
frame1.getContentPane().add(graph1);
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);
}
public class DrawGraph extends JPanel {
private final int MAX_score = 20;
private final int PREF_W = 400;
private final int PREF_H = 200;
private final int BORDER_GAP = 10;
private final Color GRAPH_COLOR = Color.green;
private final Color GRAPH_POINT_COLOR = new Color(150,50,180);
private final stroke GRAPH_stroke = new Basicstroke(3f);
private final int GRAPH_POINT_WIDTH = 12;
private final int Y_HATCH_CNT = 10;
private List<Integer> scores;
public DrawGraph(List<Integer> scores) {
this.scores = scores;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.size() - 1);
double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_score - 1);
List<Point> graPHPoints = new ArrayList<Point>();
for (int i = 0; i < scores.size(); i++) {
int x1 = (int) (i * xScale + BORDER_GAP);
int y1 = (int) ((MAX_score - scores.get(i)) * yScale + BORDER_GAP);
graPHPoints.add(new Point(x1,y1));
}
// create x and y axes
g2.drawLine(BORDER_GAP,getHeight() - BORDER_GAP,BORDER_GAP,BORDER_GAP);
g2.drawLine(BORDER_GAP,getWidth() - BORDER_GAP,getHeight() - BORDER_GAP);
// create hatch marks for y axis.
for (int i = 0; i < Y_HATCH_CNT; i++) {
int x0 = BORDER_GAP;
int x1 = GRAPH_POINT_WIDTH + BORDER_GAP;
int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
int y1 = y0;
g2.drawLine(x0,y0,x1,y1);
}
// and for x axis
for (int i = 0; i < scores.size() - 1; i++) {
int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores.size() - 1) + BORDER_GAP;
int x1 = x0;
int y0 = getHeight() - BORDER_GAP;
int y1 = y0 - GRAPH_POINT_WIDTH;
g2.drawLine(x0,y1);
}
stroke oldstroke = g2.getstroke();
g2.setColor(GRAPH_COLOR);
g2.setstroke(GRAPH_stroke);
for (int i = 0; i < graPHPoints.size() - 1; i++) {
int x1 = graPHPoints.get(i).x;
int y1 = graPHPoints.get(i).y;
int x2 = graPHPoints.get(i + 1).x;
int y2 = graPHPoints.get(i + 1).y;
g2.drawLine(x1,y1,x2,y2);
}
g2.setstroke(oldstroke);
g2.setColor(GRAPH_POINT_COLOR);
for (int i = 0; i < graPHPoints.size(); i++) {
int x = graPHPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
int y = graPHPoints.get(i).y - GRAPH_POINT_WIDTH / 2;
;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
g2.filloval(x,y,ovalW,ovalH);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W,PREF_H);
}
}
解决方法
在这里我找到了JFreeChart;用于制作图表,操作简便,可以轻松放置在JFrame或JPanel上
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。