Results 1 to 8 of 8
  1. #1
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default Moving Images in JPanel

    dcfgvbhnjkmlafjdknsa
    Last edited by killpoppop; 03-09-2009 at 11:11 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are missing a "Man" class here.

    Also, yes, you are right that you should never call Thread.sleep(...) on the EDT, the event dispatch thread. Use a Swing Timer instead.

  3. #3
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    yeah i have a man class its pretty similar to the cloud class. So there is no way it can be done with Thread.Sleep? Could i get an example on how to use a Swing Timer please?

    Thanks,
    Pop

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Could i get an example on how to use a Swing Timer please?
    There's a decent example in the Sun Swing tutorial. Here's a small app that attempts to moves a ball on a diagonal by two ways: 1) using Thread.sleep(...) and 2) using a Swing Timer. Only the second way works:

    BallPanel.java
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class BallPanel
    {
      private static final Color BALL_COLOR = Color.red;
      private JPanel mainPanel = new JPanel()
      {
        protected void paintComponent(Graphics g)
        {
          super.paintComponent(g);
          myPaint(g);
        }
      };
      private Point ballPoint = new Point(0, 0);
      private int diameter;
      private int delta;
    
      public BallPanel(Dimension preferredSize, int diameter, int delta)
      {
        mainPanel.setPreferredSize(preferredSize);
        this.diameter = diameter;
        this.delta = delta;
      }
      
      private void myPaint(Graphics g)
      {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(BALL_COLOR);
        g2.fillOval(ballPoint.x, ballPoint.y, diameter, diameter);
      }
      
      public void moveBall()
      {
        ballPoint = new Point(ballPoint.x + delta, ballPoint.y + delta);
        mainPanel.repaint();
      }
      
      public void resetBall()
      {
        ballPoint = new Point(0, 0);
        mainPanel.repaint();
      }
      
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    BallPanelTest.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.Timer;
    
    public class BallPanelTest
    {
      private static final int WIDTH = 400;
      private static final Dimension BALL_PANEL_SIZE = new Dimension(WIDTH, WIDTH);
      private static final int DIAMETER = 20;
      private static final int DELTA = 2;
      private static final int SLEEP_TIME = 20;
      private static final String THREAD_SLEEP = "Thread Sleep";
      private static final String SWING_TIMER = "Swing Timer";
      private static final String RESET = "Reset";
      private static final String[] BUTTON_STRINGS =
      {
        THREAD_SLEEP, SWING_TIMER, RESET
      };
      private JPanel mainPanel = new JPanel();
      private BallPanel ballPanel = new BallPanel(BALL_PANEL_SIZE, DIAMETER, DELTA);
    
      public BallPanelTest()
      {
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        btnPanel.setBorder(BorderFactory.createEtchedBorder());
        ButtonListener btnListener = new ButtonListener();
        for (String btnString : BUTTON_STRINGS)
        {
          JButton btn = new JButton(btnString);
          btn.addActionListener(btnListener);
          btnPanel.add(btn);
        }
        
        int blGap = 5;
        mainPanel.setLayout(new BorderLayout(blGap, blGap));
        mainPanel.add(ballPanel.getComponent(), BorderLayout.CENTER);
        mainPanel.add(btnPanel, BorderLayout.SOUTH);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          ballPanel.resetBall();
          String command = e.getActionCommand();
          if (command.equals(RESET))
          {
            ballPanel.resetBall();
          }
          else if (command.equals(THREAD_SLEEP))
          {
            for (int count = 0; count < WIDTH/DELTA - 10; count++)
            {
              ballPanel.moveBall();
              try
              {
                Thread.sleep(SLEEP_TIME);
              } catch (InterruptedException e1) {}
            }
          }
          else if (command.equals(SWING_TIMER))
          {
            new Timer(SLEEP_TIME, new TimerListener()).start();
          }
        }
      }
      
      private class TimerListener implements ActionListener
      {
        int count = 0;
        public void actionPerformed(ActionEvent e)
        {
          if (count < WIDTH/DELTA - 10)
          {
            ballPanel.moveBall();
            count++;
          }
          else
          {
            ((Timer)e.getSource()).stop();
          }
        }
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("MoveBallMain");
        frame.getContentPane().add(new BallPanelTest().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  5. #5
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Right so ballpanel.java is standard. and in ballpaneltest you try doing it the two different ways? Sorry got myself in a mess when i presumed the top one did one and the second did another!

    Pop

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, posted without comments as it was made in a hurry.

    BallPanel will draw a ball on a JPanel.
    Calling getComponent will get the JPanel that does the drawing.
    Calling moveBall() will advance the ball along a diagonal.
    calling resetBall() will return the ball to the left upper corner.

    BallPanelTest holds a JPanel made by BallPanel and has two jbuttons that both move the ball. One button uses a loop and Thread.sleep(...), and the other uses a Swing Timer. If you press the sleep button, it (as expected) puts the whole app to sleep (note that the button itself doesn't unset itself) and the ball doesn't move until the loop is complete and the ball magically appears in the right lower corner.

    The swing timer code however shows a ball smoothly moving along the diagonal and shows that it does not put the entire app to sleep (notice that the button "unpresses" itself promptly).

  7. #7
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Can it be done without a listener? From what i get i need to do something to prompt the movement. I want the objects to move constantly while the application is running.

    Thanks
    Pop

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can it be done without a listener? From what i get i need to do something to prompt the movement. I want the objects to move constantly while the application is running.
    A Swing Timer absolutely needs an ActionListener, but I'm not sure that I understand your issue here as the one thing (using an ActionListener) has nothing to do with the other (needing or not needing a prompt to initiate movement). The Timer starts simply when the start() method is called. How or where you call this is entirely up to you.

Similar Threads

  1. randomly moving shapes
    By marlon in forum New To Java
    Replies: 3
    Last Post: 10-05-2012, 09:37 AM
  2. [Applet] Clicking images & moving them.
    By Perfectworld in forum Java Applets
    Replies: 17
    Last Post: 01-16-2010, 08:09 AM
  3. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 04:07 PM
  4. moving slider with key event
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:50 PM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:52 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
  •