0

I am having trouble. I want to draw a rect onto my JPanel which is added to my JFrame contentPane. I want that x to be at a set pos but moving -x and restarting where +x begins. i.e. If I have a JPanel that is 800 x 400, I want the rext to take in those parameters but moving along the xaxis (x - Velx) repainting itself at 800 and continuing in the - x direction. I know this isn't sufficient info, none of my books that I have touch base on what I am trying to do so I lack proper terminology.

  • Never mind screw it, I'll just start from the basics again and scratch the idea I had in mind. I lack proper terminology as to what I am even looking for. – Cameron Dindy Apr 25 '16 at 01:53
  • Something like [this](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) or [this](http://stackoverflow.com/questions/16908418/paintcomponent-not-working/16908462#16908462)? – MadProgrammer Apr 25 '16 at 01:53

1 Answers1

1

// Here is a good example of doing this

public class AnimatedBoat {

public static void main(String[] args) {
    new AnimatedBoat();
}

public AnimatedBoat() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new AnimationPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    });
}

public class AnimationPane extends JPanel {

    private BufferedImage boat;
    private int xPos = 0;
    private int direction = 1;

    public AnimationPane() {
        try {
            boat = ImageIO.read(new File("boat.png"));
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    xPos += direction;
                    // change directions off window width
                    if (xPos + boat.getWidth() > getWidth()) { 
                        xPos = getWidth() - boat.getWidth();
                        direction *= -1;
                    } else if (xPos < 0) {
                        xPos = 0;
                        direction *= -1;
                    }
                    repaint();
                }

            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return boat == null ? super.getPreferredSize() : new Dimension(boat.getWidth() * 4, boat.getHeight());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int y = getHeight() - boat.getHeight();
        g.drawImage(boat, xPos, y, this);

    }

}

}

  • Lifted, verbatim from [How to move an image (animation)?](http://stackoverflow.com/questions/14432816/how-to-move-an-image-animation/14436331#14436331) or [I am trying to move a ball in applet using thread but its not moving](http://stackoverflow.com/questions/14456072/i-am-trying-to-move-a-ball-in-applet-using-thread-but-its-not-moving/14456808#14456808). I think you'll find that this kind of behaviour isn't appreciated, as you're attempting to improve your reputation at the cost of others. If the example is truly helpful, then the question should be closed as a duplictae – MadProgrammer Apr 25 '16 at 02:22
  • I'd also discourage the "blind" code dump as it teaches the OP nothing, why would you use a Swing `Timer` in this case? What are the pros or cons of using a `JPanel` in this way? – MadProgrammer Apr 25 '16 at 02:25
  • @MadProgrammer I just found an example that seemed to be what the OP was needing an example of changing directions. I am not try to build reputation points those come over time. I guess I could have just commented but I can't because I need 50 rp in order to right a comment to link him to an example. At no point did I say this was my code it is and example from and open source form. Sorry if you don't appreciate me trying to help out. I would want someone to give me a piece of code to look at but that is just my opinion sorry if I offended you and the community. –  Apr 25 '16 at 02:35
  • I don't mind you helping, I have no issue, but equally, you made no attempt to not claim the code was yours. If I was in the OPs position, I'd prefer to learn the skills I need to find the answers I need, rather then been spoon feed, but I guess I like to know the "whys" of something rather than just the "hows", SO is about "growing", not "feeding' developers ;) – MadProgrammer Apr 25 '16 at 02:42
  • @MadProgrammer You are absolutely right. I will try to do a better job of developing custom solutions to questions. I research a lot so maybe I should try just writing code to solve things. I have a question for you though. What all do you need to know to become an entry level java developer. I mean in the sense of like degree in CS or like what avenue to approach about getting in the door. –  Apr 25 '16 at 02:49