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.
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());
frame.pack();
frame.setVisible(true);
}
}
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;
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 (Color.black);
timer.start();
}
//-----------------------------------------------------------------
// 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)
{
repaint();
}
}
}