2

I'm aiming for the appearance of a rectangle when the mouse is clicked. Here's a snippet of my current code:

    try{
            image = ImageIO.read(file);
            g.setColor(new Color(255,0,0));
            g.drawRect(x, y, 100, 100);
            }

    icon = new ImageIcon(image);
    label = new JLabel(icon);
    label.addMouseListener(this);

     public void mouseReleased(MouseEvent event) {
            // TODO Auto-generated method stub

            if(event.getSource() == label){

                x = event.getX();
                y = event.getY();

                repaint();

                input = JOptionPane.showInputDialog("Something:");
                System.out.println(input);
            }
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
alicedimarco
  • 325
  • 1
  • 5
  • 20

1 Answers1

3

You can extend a JPanel to do exactly what you want:

class MyPanel extends JPanel{
    //....
    private java.awt.Rectangle rectangle = null;
    private Image imageToDraw;
    private Point imageLocation;
    public setImageToDraw(Image toDraw,Point p){
      imageToDraw=toDraw;
      imageLocation = p;
    }
    public void setRectangle(java.awt.Rectangle rectangle overlayRect){
       rectangle = overlayRect;
    }
    // Override paintComponent to draw image and rectangle
    @Override
    public void paintComponent(Graphics g) {
      g.drawImage(imageToDraw,imageLocation.getX(),imageLocation.getY(),this);
      if(rectangle != null) {
         // Draw your rectangle here...
      }
    }
}

In your mouse listener, do the following:

// Declare a field of type MyPanel
private MyPanel drawingPanel = new MyPanel
// ... Initialization stuff...
drawingPanel.setImageToDraw(toDraw,p);
public void mouseReleased(MouseEvent event) {
        // TODO Auto-generated method stub
        if(event.getSource() == label){
           // Compute rectangle boundaries
           drawingPanel.setRectangle(overlayRect);
        }
        drawingPanel.repaint();
}

Basically, a MyPanel object will always have the image to draw set. This way the image is drawn on the panel all the time. When you want an overlay rectangle, all what you have to do is to set the MyPanel.rectangle field and refresh your MyPanel instance. This will redraw the image first and then draw the overlay rectangle on top of the image.

GETah
  • 20,922
  • 7
  • 61
  • 103
  • Is this another class? What if I want to work on a single class only? :) – alicedimarco Nov 20 '11 at 11:04
  • 1
    No, I think it is better to create a new class. Remember, a basic JPanel can not draw an image and a rectangle on top of it. You better have to extend the capabilities of a JPanel and encapsulate all this business logic inside it. This way, you get things done in a nice and clean object oriented way. – GETah Nov 20 '11 at 11:11
  • Great! Let us know how you are getting with this. Please don't forget to +1 if this was helpful and accept if this fixes your issues :) – GETah Nov 20 '11 at 11:16
  • Sorry, I don't understand the other lines of code you put D: like imagetodraw or overlayRect. – alicedimarco Nov 20 '11 at 14:28
  • 1
    Sorry if my code was a bit confusing. imageToDraw is the image you want to draw, overlayRect is the rectangle to be drawn on top of the image. I hope this answers your question :) – GETah Nov 20 '11 at 16:46