Inserting an image on mouse click
Hello,
I use the "canvas" to create a program similar to "PacketTracer". I want to insert a picture into drawing "canvas" (or another) every time I click with the mouse. Please give me some hints because it is several days I want a solution. So, any solution will be helpful to me. Thanks in advance for the help.
Re: Inserting an image on mouse click
Hello,
Uses the ImageIO class to read each image. When you view the image in a JPanel or a panel, put a MouseListener on it to see where you click. Then retrieves an instance of Graphics from the BufferedImage that you have obtained the very beginning and calls the drawImage method of Graphics class, passing in parameter the coordinates of the click and others. Hope the following information will help you.
Re: Inserting an image on mouse click
Hello,
I am also trying a similar kind of a program which will insert an image with a mouse click. I have an idea me, please tell me if this is right or not. Is what I can create a new Label (with image as icon) in a panel, each time you click disappointed? is this possible? because I think I can design a code the the same. If it is correct and if i am in the right direction then please confirm me with the same and I will start to code. Thanks in advance.
Re: Inserting an image on mouse click
Hello,
This is my updated code, please check it and if I am wrong somewhere in the code then please guide me with the correct
Code:
Public class frm extends javax.swing.JFrame (
int x, y;
private int prx;
private int pry;
private boolean pressOut;
/ ** Creates new form frame * /
Public frm () (
initComponents ();
)
class nlbl extends javax.swing.JLabel implements MouseListener, MouseMotionListener (
javax.swing.JLabel jlbl = new javax.swing.JLabel ();
Public nlbl (String pci, String t) (
jlbl.setIcon (new javax.swing.ImageIcon (getClass (). getResource (pci)));
jlbl.setText (t);
jlbl.setHorizontalTextPosition (javax.swing.SwingConstants.CENTER);
jlbl.setVerticalTextPosition (javax.swing.SwingConstants.BOTTOM);
)
Public void mouseClicked (MouseEvent e) (
)
Public void mousePressed (MouseEvent e) (
)
Public void mouseReleased (MouseEvent e) (
)
Public void mousePressed (MouseEvent e) (
)
Public void mouseExited (MouseEvent e) (
)
Public void mouseDragged (MouseEvent e) (
)
Public void mouseMoved (MouseEvent e) (
)
}
Re: Inserting an image on mouse click
Hello,
Already, it shows that you did not understand the basics of Swing, stop using the RAD tool for Netbeans, you do not even understand what you do and admit that your code is just the anything that is unnecessarily complicated. Do as I have suggested, take back everything from scratch. When I tell you to add a MouseListener to a component, that means that you must call the above method addMouseListener not that you create a class that implements this interface.
Re: Inserting an image on mouse click
Hello,
I think you can try the following code
Code:
MouseAdapter msadp =new MouseAdapter () (
@ Override
Public Final void mouseClicked (MouseEvent e) (
if(SwingUtilities.isLeftMouseButton (e))
(int e.getX x = (), y = e.getY ();
/ / Store x and y somewhere else or an attribute to serve thee after
)
)
);
panel.addMouseMotionListener (msadp);
panel.addMouseListener (msadp);
Put it in the class derived from JPanel:
Code:
@ Override
protected void paintComponent (Graphics grp) (
great. paintComponent (grp);
grp.drawImage (BufferedImage, 0.0,this);
}