Results 1 to 3 of 3
  1. #1
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Why the picture is not moving?

    Hi everyone.

    My require of the program is : when I click on the image , image should go down slowly.

    The problem is that when I'm clicking nothing happens.

    Maybe the problem is at MouseClickedEvent.

    I would be thankful if any of you guys help me with this.

    Thanks in advance.

    Here's my code.

    Java Code:
    public class PlayNow extends JFrame {
     
     
        public static Image image;
        int x;
        int y;
        int dy;
        Timer t;
        public PlayNow() throws IOException{
     
            initComponents();
            //setLayout(null);
            x = 100;
            y = 50;
            dy = 1;
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(800,600);
            image = ImageIO.read(new File("D:\\Ertani\\JAVA Program by me\\Birra\\src\\birra\\bottle2.png"));
            JLabel picLabel = new JLabel(new ImageIcon(image));
            picLabel.setLocation(x, y);
     
            picLabel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jLabel1MouseClicked(evt);
                }
            });
     
        }
     
        public void paint(Graphics g)
        {
            g.drawImage(image, x, y, this);
        }
     
        private void jLabel1MouseClicked(MouseEvent evt) {                                     
            // TODO add your handling code here:
                t = new Timer(15,new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    while(y <= 200)
                    {
                        y += dy;
                        repaint();
                    }
                }
                });
     
                    t.start();
                } 
     
        private void initComponents() {
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 457, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 1503, Short.MAX_VALUE)
            );
     
            pack();
        }               
     
     
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why the picture is not moving?

    You need to add it to the Frame
    Java Code:
    add(picLabel);
    And get rid of the paint() override. The way you are doing it you don't need it. The frame will paint itself when you call repaint.

    There are a bunch of things you are doing that you should not do. So check out the tutorials in my signature. They cover painting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Why the picture is not moving?

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2012, 04:44 PM
  2. Picture zoom
    By hades7 in forum New To Java
    Replies: 1
    Last Post: 02-02-2012, 06:10 PM
  3. Picture manipulating help
    By evolutiongsr8 in forum New To Java
    Replies: 0
    Last Post: 03-26-2011, 06:47 AM
  4. Getting a picture out of a jar?
    By Zman3359 in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 11:55 PM
  5. Picture help
    By deathnote11 in forum AWT / Swing
    Replies: 13
    Last Post: 06-03-2008, 05:46 PM

Posting Permissions

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