I am drawing onto a JPanel using getGraphics and the drawLine and fillOval commands but it is very temperamental when running the program. However, when I debug it it draws every time.
draw.drawPoints(drawing.getGraphics(), xCoord, yCoord);
Calls:
public void drawPoints (Graphics g, int x, int y){
g.setColor(Color.red);
g.fillOval(x, y, 5, 5);
}
edit: It wont always dra开发者_运维知识库w. Most of the time is stays blank.
I am drawing onto a JPanel using getGraphics
You should not draw stuff on the JPanel
by getting a Graphics
object from drawing.getGraphics()
.
Instead, you should override the paintComponent(Graphics g)
method and do your painting there.
A simple example to get you started:
container.add(new JPanel() {
public void paintComponent(Graphics g) {
drawPoints(g, xCoord, yCoord);
}
});
You need to do that every time the object is repainted.
精彩评论