How can I bring my shape that I painted with paintComponent and clicked with a mouse to front?

Edenco :

I have a program that painted three different shapes(circle, triangle and square) and I try to drag them with a mouse. What I want to do is to bring to front that shape that is clicked. Can I pass argument together with repaint() to tell to a program that for example circle that is clicked has to be painted last, or is it possible to create three different paint methods to keep track of paint ordning?

Another questions is my if-loop,it works just fine to drag square through circle and triangel but circle and triangel take square along with them if mouse is in the same area. Can I turn off mouse listener for other figures while I drag one?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MouseMoveScale extends JPanel {

    public boolean first;

private Rectangle2D.Float myRect = new Rectangle2D.Float(50, 50, 100, 100);
private Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
private Polygon myTr   = new Polygon(new int []  {120, 60, 240}, new int[] {150, 200, 200}, 3);

MovingAdapter ma = new MovingAdapter();

public MouseMoveScale() {
    addMouseMotionListener(ma);
    addMouseListener(ma);
}
public void paintComponent( Graphics g ) {

}
public void paint(Graphics g) {

    super.paint(g);
    Graphics2D square = (Graphics2D) g;
    Graphics2D triangle = (Graphics2D) g;
    Graphics2D circle = (Graphics2D) g;

    square.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    square.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    square.setColor(new Color(0, 0, 200));
    square.fill(myRect);

    triangle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    triangle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    triangle.setColor(new Color(139, 89, 255));
    triangle.fill(myTr);

    circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    circle.setColor(new Color(0, 0, 117));
    circle.fill(myCr);
}

class MovingAdapter extends MouseAdapter {

    private int x;
    private int y;

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseDragged(MouseEvent e) {
        int dx = e.getX() - x;
        int dy = e.getY() - y;

        if (myRect.contains(x, y)) {
            myRect.x += dx;
            myRect.y += dy;
            repaint();
        }

        else if (myTr.contains(x, y)) {
          myTr.translate(dx, dy);
            repaint();
       }
        else if (myCr.contains(x, y)) {
            myCr.x += dx;
            myCr.y += dy;
            repaint();
        }
        x += dx;
        y += dy;
    }
    }



    public static void main(String[] args) {
    JFrame frame = new JFrame("Shapes World");
    MouseMoveScale m = new MouseMoveScale();
    m.setDoubleBuffered(true);
    frame.add(m);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
   }
   }
Hovercraft Full Of Eels :

Suggestions:

  1. Put all the shapes into a List<Shape>
  2. Iterate through the list forward in the paintComponent, drawing each shape. The last one drawn will be on top.
  3. In the mouse listener/adapter, iterate through the shapes list backwards to find out which shape was pressed, and exit the loop as soon as any shape has been found. Do this backwards so that the one on top gets checked first.
  4. When a shape is found, remove it from the list, and then re-add it and call repaint(). This will have it drawn on the top.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=388886&siteId=1