Results 1 to 3 of 3

Thread: Paint in jPanel

  1. #1
    willer.reis is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Question 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);
    
        }

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Paint in jPanel

    Cross posted
    Mover imagem em jPanel

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2012, 02:49 PM
  2. Paint Graphics2D objects onto a JPanel
    By sublixt in forum Java 2D
    Replies: 10
    Last Post: 10-28-2011, 12:57 PM
  3. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 07:00 PM
  4. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 03:26 AM
  5. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 05:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •