英文:
Add DrawGraph on Panel instead on Frame
问题
我正在使用这个设置来绘制简单的折线图,然后在一个框架上使用.add()
将其添加到一个帧上。
我的问题是,我希望将它.add()
到一个面板中,以便我可以添加多个图形。
如何运行这个?
JPanel myPanel = new JPanel();
DrawGraph graph1 = new DrawGraph(graph_scores);
myPanel.add(graph1);
下面是frame.add()
的代码。但是我希望在上面使用panel.add()
:
public void start_graph()
{
// 创建随机数据列表
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));
}
// 这是我想要更改的部分,谢谢!
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, 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) {
// ...
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
英文:
I'm using this setup do draw simple line graph, then .add()
it on a frame.
My problem is I want to .add()
it into a panel so that I can append multiple graphs.
How do I run this?
JPanel myPanel = new JPanel();
DrawGraph graph1 = new DrawGraph(graph_scores);
myPanel.add(graph1);
Here's the code for frame.add()
below. But I want panel.add()
above instead:
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, 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, getHeight() - 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, y0, x1, 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);
}
}
答案1
得分: 0
在这里,我找到了JFreeChart;用于制作图表,它易于操作,可以轻松地放置在JFrame或JPanel上。
英文:
Here i found JFreeChart; to use in making charts, it is easy to handle and can be placed on either JFrame or JPanel easily
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论