Results 1 to 2 of 2
  1. #1
    jmanswrd is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Bouncing line homework trouble

    Having some trouble with my homework and I figured I would ask here to see if I could get some guidance.

    I need to create a screen that has a line that moves around and bounces around. But, I don't want just a solid line, the line itself needs to be snake like and change directions and such.

    I have come up with the the code that needs to be inserted I am just having a hard time of where it needs to be placed.

    Java Code:
    //  Demonstrates an animation and the use of the Timer class.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Rebound
       //  Displays the main frame of the program.
       public static void main (String[] args)
          JFrame frame = new JFrame ("Rebound");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new ReboundPanel());
    I know I need to insert this stuff below I am just having a hard time where. And if I am missing anything? Thanks for any help or hints:confused:

    //creating line
    private int [] xray= new int [MAXRAY+1];
    private int [] yray= new int [MAXRAY+1];
    for (int z =1; z <MAXRAY; z++)
    {xray[z]= xray [z+1];
    yray[z] = yray[z+1];

    //drawing line
    for (int sub =1; sub<= MAXRAY; sub++)
    page.drawLine (xray[sub], yray[sub], xray[sub], yray[sub]);

    // moving line
    xray[MAXRAY] = xray[MAXRAY-1] + moveX;
    yray[MAXRAY]= yray[MAXRAY-1] + moveY;

    Java Code:
    //  Represents the primary panel for the Rebound program.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ReboundPanel extends JPanel
       private final int WIDTH = 300, HEIGHT = 100;
       private final int DELAY = 20, LENGTH = 35;
       private int MAXRAY;
       private ImageIcon image;
       private Timer timer;
       private int x, y, moveX, moveY;
       //  Sets up the panel, including the timer for the animation.
       public ReboundPanel()
          timer = new Timer(DELAY, new ReboundListener());
          x = 0;
          y = 40;
          moveX = moveY = 3;
          setPreferredSize (new Dimension(WIDTH, HEIGHT));
          setBackground (;
       //  Draws the image in the current location.
       public void paintComponent (Graphics page)
          super.paintComponent (page);
          page.setColor (Color.yellow);      
       //  Represents the action listener for the timer.
       private class ReboundListener implements ActionListener
          //  Updates the position of the image and possibly the direction
          //  of movement whenever the timer fires an action event.
          public void actionPerformed (ActionEvent event)

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    I remember doing this on the Apple //e BASIC in a pong game, and again in MS-DOS BASIC for a nibbles game.. zomg I'm old.

    But, the point is, to make a line segment snake around, at the time I did it in pixels in an array, because at the time pixels are like so huge they are 2D snakes :)

    But here in swing, get a small line segment (2 points : P1(x,y), P2(x,y) , where say d = 2)
    to move around first, such as on timer T, it will advance P1 by (moveX, moveY), and set P2 to be what P1 was before moving it. Considering making this being a Line class

    Java Code:
    class Point {
      int x;
      int y;
      public Point(int x, int y) {
        this.x = x; 
        this.y = y;
    class Line {
       Point p1;
       Point p2;
       public Line(Point p1, Point p2) {
          this.p1 = p1;
          this.p2 = p2;
       public void advance(int moveX, int moveY) {
           p2.x = p1.x;
           p2.y = p1.y;
           p1.x = p1.x + moveX;
           p1.y = p1.y + moveY;
    // and then
    Line l1 = new Line(new Point(5,0), new Point(3,0));
    where inflection and negative movements due to hitting the boundary would have been computed before calling the advance() method.

    Once you get a single small line able to move around within the boundary on timer T,
    it is straight forward to declare an array of these lines, where each segment has an end point coinciding with the next segment's starting point., and we have the onTimer apply the position of Line[n] to the advance(), Line[n-1] to the position that Line[n] was before it was advanced., Line[n-2] to the position of Line[n-1] and so on

    Java Code:
    Line[] snake = new Line[4]; 
    // here we would set up the snake when the program starts.
    // maybe randomly or more creatively than this.
    snake[0] = new Line(new Point(0,0), new Point(2,0));
    snake[1] = new Line(new Point(2,0), new Point(4,0));
    snake[2] = new Line(new Point(4,0), new Point(6,0));
    snake[3] = new Line(new Point(6,0), new Point(8,0));
    // called onTimer(), with some incremental value of moveX and moveY, computed according to boundary constraints 
    //( we would need to peek at snake[3] , the head to see if it's current position plus the 
    // moveX,y would be within bounds and inflect the moveX,Y as required)
    public void advanceSnake(Line[] segments, int moveX, int moveY) {
      for (int n = 0; n < segments.length-1; n++) {
        // make the segment move to the position of the next segment
        segments[n].p1.x = segments[n+1].p1.x;
        segments[n].p1.y = segments[n+1].p1.y;
        segments[n].p2.x = segments[n+1].p2.x;
        segments[n].p2.y = segments[n+1].p2.y;
      //  translate the 'head' (last segment in the array)

Similar Threads

  1. Having trouble with java homework...
    By purinlove88 in forum New To Java
    Replies: 3
    Last Post: 03-02-2009, 04:02 PM
  2. How to create a bouncing ball animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:43 PM
  3. high bouncing ball
    By bouncingball in forum Reviews / Advertising
    Replies: 1
    Last Post: 06-19-2008, 11:21 AM
  4. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 08:35 PM
  5. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 04:48 AM

Posting Permissions

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