小编典典
你的代码存在问题和建议:
同样,你需要更改组件的preferredSize(在这里为Graph JPanel),而不是大小
不要设置JFrame的边界。
呼叫pack()你的JFrame将组件添加到它后并调用调用setVisible之前(真)
由于ArrayList的大小为0,因此你的foreach循环将不起作用(对其进行测试以确保它是正确的)。而是使用从0到10的for循环。
你的paintComponent(...)方法内部不应包含程序逻辑,而应仅绘制代码。因此,我将ArrayList设为一个类变量,并将其填充到该类的构造函数中。
例如:
import ;
import ;
import ;
import ;
import .Graphics2D;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
@SuppressWarnings("serial")
public class DrawGraph extends JPanel {
private static final int MAX_SCORE = 20;
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private static final int BORDER_GAP = 30;
private static final Color GRAPH_COLOR = ;
private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180);
private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
private static final int GRAPH_POINT_WIDTH = 12;
private static final int Y_HATCH_CNT = 10;
private List scores;
public DrawGraph(List scores) {
= scores;
}
@Override
protected void paintComponent(Graphics g) {
(g);
Graphics2D g2 = (Graphics2D)g;
(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (() - 1);
double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_SCORE - 1);
List graphPoints = new ArrayList();
for (int i = 0; i < (); i++) {
int x1 = (int) (i * xScale + BORDER_GAP);
int y1 = (int) ((MAX_SCORE - (i)) * yScale + BORDER_GAP);
(new Point(x1, y1));
}
// create x and y axes
(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
(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;
(x0, y0, x1, y1);
}
// and for x axis
for (int i = 0; i < () - 1; i++) {
int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (() - 1) + BORDER_GAP;
int x1 = x0;
int y0 = getHeight() - BORDER_GAP;
int y1 = y0 - GRAPH_POINT_WIDTH;
(x0, y0, x1, y1);
}
Stroke oldStroke = ();
(GRAPH_COLOR);
(GRAPH_STROKE);
for (int i = 0; i < () - 1; i++) {
int x1 = (i).x;
int y1 = (i).y;
int x2 = (i + 1).x;
int y2 = (i + 1).y;
(x1, y1, x2, y2);
}
(oldStroke);
(GRAPH_POINT_COLOR);
for (int i = 0; i < (); i++) {
int x = (i).x - GRAPH_POINT_WIDTH / 2;
int y = (i).y - GRAPH_POINT_WIDTH / 2;;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
(x, y, ovalW, ovalH);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
List scores = new ArrayList();
Random random = new Random();
int maxDataPoints = 16;
int maxScore = 20;
for (int i = 0; i < maxDataPoints ; i++) {
((maxScore));
}
DrawGraph mainPanel = new DrawGraph(scores);
JFrame frame = new JFrame("DrawGraph");
(JFrame.EXIT_ON_CLOSE);
().add(mainPanel);
();
(true);
(true);
}
public static void main(String[] args) {
(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
2020-03-24