How would I clear a single shape from my JFrame without making the screen flash white.
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;`
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Frame extends JFrame {
static Shape s;
Graphics2D g2D;
/**
*
*/
private static final long serialVersionUID = 7764273188525543212L;
private JPanel contentPane;
public Point startDrag = null, endDrag = null;
HashMap<Shape, Color> shapes = new HashMap<Shape, Color>();
/**
* Launch the application.
*/
public static void main(String[] args) {
Frame f = new Frame();
f.setVisible(true);
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
Shape s = makeRectangle(startDrag.x, startDrag.y, e.getX(),
e.getY());
Color[] colors = {Color.MAGENTA, Color.CYAN, Color.RED,
Color.BLUE, Color.PINK, Color.yellow};
Random r = new Random();
int i = r.nextInt(colors.length - 1);
shapes.put(s, colors[i]);
endDrag = null;
startDrag = null;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
paintBackground(g2);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.50f));
g2.setStroke(new BasicStroke(2));
for (Entry<Shape, Color> s : shapes.entrySet()) {
g2.setPaint(Color.BLACK);
g2.draw(s.getKey());
g2.setPaint(s.getValue());
g2.fill(s.getKey());
}
if (startDrag != null) {
if (endDrag != null) {
g2.setStroke(new BasicStroke(5.0f));
g2.setColor(Color.darkGray);
s = makeRectangle(startDrag.x, startDrag.y, endDrag.x,
endDrag.y);
g2.draw(s);
}
}
}
private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x1 - x2), Math.abs(y1 - y2));
}
private void paintBackground(Graphics2D g2) {
clear(g2);
g2.setPaint(Color.red);
g2.setStroke(new BasicStroke(0.4f));
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(0, i, getSize().width, i);
g2.draw(line);
}
}
private void clear(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
}
}
// If you try this code, just drag and let go to make a rectangle.
But when I drag, the screen flashes white, so it's hard for me to see what I'm doing. Is there anyway to make this work the same way, but that clears the JFrame faster than you can see?