Results 1 to 3 of 3
Thread: Paint in jPanel
- 03-08-2012, 06:45 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
Paint in jPanel
Hi people.
I'm Brazilian, so, sorry cause my english is not good yet.
Well, i have a big problem (at least I think so), that i have to paint a image in a specific JPANEL (drag and drop) to move my image in this jPanel.
I tried many ways to do this, but i couldn't. So, i need a help, please!
The Jpanel calls: ujPanelTeste
The component jLabel (label) sets the icon (my image).
When i drag my mouse (mouseDragged) i would like to this icon move, but in this Panel.
Any idea?
Thanks so much!!
below my cod:
PHP Code:public class InterConfiguracoes extends javax.swing.JFrame { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(getClass().getResource("/Images/player_play.png")); JLabel label; int x = 360; int y = 300; /** Creates new form InterConfiguracoes */ public InterConfiguracoes() { initComponents(); this.setLocationRelativeTo(null); ujPanelTeste.setBounds(600, 600, 600, 600); this.setSize(900, 650); this.setResizable(false); setVisible(true); ImageIcon icon = new ImageIcon(image); label = new JLabel(icon, JLabel.CENTER); label = new JLabel(icon); label.addMouseListener(new MouseListener() { public void paint(Graphics g) { ujPanelTeste.paintComponent(g); g.drawImage(image, x, y, ujPanelTeste); } public void setX(int x1) { x = x1; } public void setY(int y1) { y = y1; } public void mouseClicked(MouseEvent e) { repaint(); } public void mousePressed(MouseEvent e) { // setX(e.getX()); // setY(e.getY()); repaint(); } public void mouseDragged(MouseEvent e) { setX(e.getX()); setY(e.getY()); repaint(); } public void mouseMoved(MouseEvent e) { } public void mouseReleased(MouseEvent e) { repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); label.setComponentOrientation(ujPanelTeste.getComponentOrientation()); label.addMouseMotionListener(new MouseMotionListener() { public void paint(Graphics g) { ujPanelTeste.paintComponent(g); g.drawImage(image, x, y, ujPanelTeste); } public void setX(int x1) { x = x1; } public void setY(int y1) { y = y1; } public void mouseDragged(MouseEvent e) { setX(e.getX()); setY(e.getY()); repaint(); } public void mouseMoved(MouseEvent e) { } }); ujPanelTeste.add(label); }
- 03-08-2012, 05:21 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Paint in jPanel
This site may help you out. You will most likely have to do some vector math to figure out where the mouse is, where it was, and where that is in relation to the screen. I find that drawing pictures helps. Learn about the differences between getPoint(), getLocation(), and getLocationonScreen(). You will have to use all three.
- 03-08-2012, 08:04 PM #3
Re: Paint in jPanel
Cross posted
Mover imagem em jPanel
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
How To Paint Multiple Objects Of Same Class Onto One JPanel
By Wangagat in forum AWT / SwingReplies: 2Last Post: 02-17-2012, 02:49 PM -
Paint Graphics2D objects onto a JPanel
By sublixt in forum Java 2DReplies: 10Last Post: 10-28-2011, 12:57 PM -
Placing a new JPanel over a paint overriden JPanel
By Tanshaydar in forum AWT / SwingReplies: 4Last Post: 12-08-2010, 07:00 PM -
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 03:26 AM -
help?! paint on top of swing components on JPanel
By beam2008 in forum AWT / SwingReplies: 1Last Post: 12-05-2008, 05:15 AM
Bookmarks