dimanche 30 novembre 2014

Draggable circle in swing blinks when I drag it


Vote count:

0




I'm implementing a Swing program that draws shapes to the screen and then lets the user drag them around. I can draw a shape just fine, but once I click on a shape to drag it, that shape starts to blink as it is dragged.


This is my GUI class and my MouseAdapter class:



public class GUI extends JFrame {
private JMenu addShapeMenu = new JMenu("Add Shape");
private JLabel statusBar = new JLabel();
private List<Shape> _shapes;
private Shape chosenShape;
private boolean isShapeClicked = false;
private List<String> shapeSubclasses = new ArrayList<>();

public GUI() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
super("DrawingBoard");
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(addShapeMenu);

getContentPane().setLayout(new BorderLayout());

getLoadedShapes();
for(String shape : shapeSubclasses)
{
Class drawableShape = Class.forName("gui.Drawable" + shape);
addShapeMenu.add(((DrawableShape)drawableShape.newInstance()).getInstance().getMenuItem());
}

add(statusBar, BorderLayout.SOUTH);
statusBar.setText("Add a shape.");

MouseHandler mouseHandler = new MouseHandler();
addMouseListener(mouseHandler);
addMouseMotionListener(mouseHandler);

setVisible(true);
}

private void getLoadedShapes() throws ClassNotFoundException, NullPointerException {
String classPath = Shape.class.getResource("").getPath();
classPath = classPath.replaceAll("%20", " ");

File root = new File(classPath);
for (File file : root.listFiles()) {
if (!file.isDirectory()) {
if (file.getName().endsWith(".class")) {
String className = file.getName().substring(0, file.getName().length() - 6);
Class<?>[] interfaces = Class.forName("draw." + className).getInterfaces();
for(Class<?> classInterface : interfaces)
{
if(classInterface.getName().compareTo("draw.Shape") == 0)
shapeSubclasses.add(className);
}
}
}
}
}

public void setDrawing(Drawing drawing)
{
_shapes = drawing.getShapes();
AddShapeListener.getInstance().setDrawing(drawing);
AddShapeListener.getInstance().setGUI(this);
for (Component component : addShapeMenu.getMenuComponents()) {
((JMenuItem)component).addActionListener(AddShapeListener.getInstance());
}
}

public void paint(Graphics graphics)
{
super.paint(graphics);
for(Shape shape : _shapes)
try
{
Class painter = Class.forName("gui.Drawable" + shape.getClass().getSimpleName());
((DrawableShape)painter.newInstance()).getInstance().paint(getGraphics(), shape);
}

catch(Exception e)
{
JOptionPane.showMessageDialog(this, "Failed to load Drawable" + shape);
}
}

public void setStatusBar(String message)
{
statusBar.setText(message);
}

private class MouseHandler extends MouseAdapter implements MouseMotionListener {
public void mousePressed(MouseEvent event) {
_shapes.forEach(shape -> {
if(shape.isPointInside(event.getX(), event.getY()))
chosenShape = shape;
isShapeClicked = true;
});
}

public void mouseDragged(MouseEvent event) {
if (isShapeClicked) {
chosenShape.moveTo(event.getX(), event.getY());
repaint();
}
}

public void mouseReleased(MouseEvent event) {
isShapeClicked = false;
chosenShape = null;
}
}
}


where my DrawableCircle class has a method paint:



public void paint(Graphics graphics, Shape shape)
{
int radius = ((Circle)shape).getRadius();
graphics.fillOval(shape.getX() - radius, shape.getY() - radius, radius * 2, radius * 2);
}


the DrawRectangle class is similar.



asked 17 secs ago







Draggable circle in swing blinks when I drag it

Aucun commentaire:

Enregistrer un commentaire