- You almost never should call
paint(...)
directly. I can count the times that I've needed to do this on one hand. - Do not get a Graphics object by calling
getGraphics()
on a component as that will return a non-durable Graphics object. Instead either draw in a BufferedImage and display that in the paint method or draw in the paint method (if AWT). - Since this is a Swing GUI, don't use an AWT component to draw in. Use a JPanel and override the
paintComponent(...)
method, not thepaint(...)
method. Otherwise you lose all benefits of Swing graphics including automatic double buffering. - The
super.paintComponent(g)
method should be called in thepaintComponent(Graphics g)
override, often as the first method call inside of this method. This lets the component do its own housekeeping painting, including erasing drawings that need to be erased. - Read the tutorials on Swing graphics as most of this is all well explained there. For e.g., please have a look here:
Edit
- To have your images persist, I suggest that you draw to a BufferedImage and then display that Image in your JPanel's
paintComponent(...)
method. - Or another option is to create a Collection of Shape objects, perhaps an
ArrayList<Shape>
and fill it with the Shapes you'd like to draw, and then in thepaintComponent(...)
method cast the Graphics object to a Graphics2D object and iterate through the Shape collection drawing each shape withg2d.draw(shape)
as you iterate.
Since Trash posted his code,...
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CircuitTracePlot2 extends JPanel {
private static final int PREF_W = 250;
private static final int PREF_H = PREF_W;
private int drawWidth = 160;
private int drawHeight = drawWidth;
private int drawX = 10;
private int drawY = 10;
private PaintType paintType = PaintType.LINE;
public CircuitTracePlot2() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public void setPaintType(PaintType paintType) {
this.paintType = paintType;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (paintType == null) {
return;
}
switch (paintType) {
case LINE:
g.drawLine(drawX, drawY, drawWidth, drawHeight);
break;
case OVAL:
g.drawOval(drawX, drawY, drawWidth, drawHeight);
break;
case SQUARE:
g.drawRect(drawX, drawY, drawWidth, drawHeight);
default:
break;
}
}
private static void createAndShowGui() {
final CircuitTracePlot2 circuitTracePlot = new CircuitTracePlot2();
JFrame frame = new JFrame("CircuitTracePlot2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(circuitTracePlot);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
int timerDelay = 2 * 1000;
new Timer(timerDelay , new ActionListener() {
private int paintTypeIndex = 0;
@Override
public void actionPerformed(ActionEvent arg0) {
paintTypeIndex++;
paintTypeIndex %= PaintType.values().length;
circuitTracePlot.setPaintType(PaintType.values()[paintTypeIndex]);
}
}).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum PaintType {
LINE, OVAL, SQUARE;
}