1

Basically I have an image loaded, and when I click a portion of the image, a rectangle (with no fill) shows up. If I click another part of the image again, that rectangle will show up once more. With each click, the same rectangle should appear.

So far I have this code, now I don't know how to make the image appear. My image from my file directory. I have already made the code to get the image from my file directory.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MP2 extends JPanel implements MouseListener{

    JFrame frame;
    JPanel panel;

    int x = 0;
    int y = 0;
    String input;

    public MP2(){

    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        MP2 panel = new MP2();
        panel.addMouseListener(panel);
        frame.add(panel);
        frame.setSize(200,200);
        frame.setVisible(true);

    }

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

        this.x = event.getX();
        this.y = event.getY();
        this.repaint();
        input = JOptionPane.showInputDialog("Something pops out");
        System.out.println(input);

    }

    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

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

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        // this.setBackground(Color.white); *Sets the bg color of the panel

        g.setColor(new Color(255,0,0));
        g.drawRect(x, y, 100, 100);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
alicedimarco
  • 325
  • 1
  • 5
  • 20
  • Your objective isn't really clear. – mre Nov 20 '11 at 14:37
  • I click a part of the image, a rectangle shows up. That's it. – alicedimarco Nov 20 '11 at 14:38
  • sounds like you need to load the image on init, and then override the painting code and in there, blit the rectangles from the image which are currently marked visible. – Nerdtron Nov 20 '11 at 14:52
  • 2
    Do you mean something like his: [drawing-a-rectangle-over-an-existing-graphics-page](http://stackoverflow.com/questions/7822202/drawing-a-rectangle-over-an-existing-graphics-page/7822385#7822385) – Hovercraft Full Of Eels Nov 20 '11 at 15:03
  • Somewhat similar. However, all I want is a simple rectangle with no fill. When I click the image, the rectangle should appear on top of the image. The code above is already working, but the rectangle is only for the frame, not the image. – alicedimarco Nov 20 '11 at 15:28

2 Answers2

2

You may want to look at drawing the rectangle on The Glass Pane, as shown in GlassPaneDemo. For example, in paintComponent(), replace g.fillOval() with g.drawRect().

I don't know how to make the image appear.

This example shows how to display an image in a JLabel.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

this.x and this.y refer to the x and y of your JPanel, not the rectangle you want to draw. You'll need to create two additional fields, rectX and rectY. These get set in mouseClicked and used by paintComponent().

EDIT

I'm sorry, my bad. I'm now confused. You do declare an x and y. These should still be renamed cause they can be confused with the x and y defined in Component, but they are not the problem. When I run your code and click, the red rectangle appears (along with a dialog). So I'm not sure what is the problem???

user949300
  • 15,364
  • 7
  • 35
  • 66