一个Graph
对象被添加到一个JFrame
. 此对象绘制轴,然后绘制图表。当通过usingpaint()
隐式调用对象时:JFrame
this.getContentPane().add(new Graph());
轴和函数都绘制。但是,当paint()
显式调用该方法时,通过:
Graph g = new Graph();
g.paint(this.getContentPane().getGraphics());
轴不绘制,但函数绘制。的完整构造函数JFrame
如下:
public GraphFrame() {
super("");
setSize(800, 800);
setVisible(true);
//One of the above blocks is called here
}
paint
对象中的函数Graph
如下:
public void paint(Graphics w) {
w.setColor(Color.WHITE);
w.fillRect(0, 0, 800, 800); //Clears the screen
w.setColor(Color.BLACK);
w.drawLine(100, 0, 100, 800);
w.drawLine(0, 700, 800, 700); //(Should) Draw the axes
for(int i = 1; i < 650; i++) {
//Draws the function
//This is just a repeated drawLine call.
}
}
为什么在组件绘制时隐式调用轴会绘制,但在显式调用时不会绘制?请记住,函数绘制(for
循环中的块),而 for 循环之前的轴不绘制。