//package bouncingBoxes;
import java.awt.Point;
import java.awt.Rectangle;
public class MyBox
{
private Rectangle box;
private int dx;
private int dy;
private int speed = 1;
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 30;
// constructor
public MyBox(Rectangle box, int dx, int dy)
{
this.box = box;
this.dx = dx;
this.dy = dy;
}
// sets the vector
public void setVector(int dx, int dy)
{
this.dx = dx;
this.dy = dy;
}
// move the box
public void move()
{
box.translate(dx, dy);
}
// moves the box just by the x coordinate
public void moveX(int x)
{
dx = x;
box.translate(dx, dy);
}
public void moveY(int y)
{
dy = y;
box.translate(dx, dy);
}
public Rectangle getBox()
{
return box;
}
// gets the Y coordinate
public int getDY()
{
return dy;
}
// gets the X coordinate
public int getDX()
{
return dx;
}
public Point getNextLocation()
{
//next x position
int x = (int)box.getX() + dx;
int y = (int)box.getY() + dy;
//return the next location
return new Point(x,y);
}
public void bounceY()
{
//swap direction in the y direction
dy *= -speed;
speed++;
}
public void bounceX()
{
// swap direction in the x direction
dx *= -speed;
speed++;
}
//package bouncingBoxes;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import javax.swing.JComponent;
/**
This component displays a rectangle that can be moved.
*/
public class RectangleComponent extends JComponent
{
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 30;
private ArrayList<MyBox> boxes;
public RectangleComponent()
{
boxes = new ArrayList<MyBox>();
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
for(MyBox box: boxes)
{
g2.draw( box.getBox() );
}
}
public void addRectangle(int x, int y, int dx, int dy)
{
Rectangle rect = new Rectangle(x, y, BOX_WIDTH, BOX_HEIGHT);
MyBox box = new MyBox(rect, dx, dy);
boxes.add(box);
repaint();
}
public void addRectangle(int x, int y)
{
Rectangle rect = new Rectangle(x, y, BOX_WIDTH, BOX_HEIGHT);
MyBox box = new MyBox(rect, 4, 4);
boxes.add(box);
repaint();
}
public void updateLocations()
{
for(MyBox box: boxes)
{
//get bottom after future move
Point p = box.getNextLocation();
Rectangle r = box.getBox();
//if bottom of box will hit bottom of screen ...
// or hits off of top
if( (p.getY() + r.height > this.getHeight() ) ||
(p.getY() <=0) )
{
//switch direction
box.bounceY();
}
if( (p.getX() + r.width > this.getWidth()) ||
(p.getX() <=0) )
{
//switch direction
box.bounceX();
}
box.move();
}
repaint();
}
/*
* Did not do what I had hoped
* keep it here in event that I need it later
* public void outOfBounds()
{
for(MyBox box: boxes)
{
if(box.getDX() > this.getWidth())
{
int x = box.getDX() - this.getWidth();
box.moveX(x);
}
if(box.getDX() < 0)
{
int x = box.getDX();
box.moveX(-x);
}
if(box.getDY() > this.getHeight())
{
int y = box.getDY() - this.getHeight();
box.moveY(y);
}
if(box.getDY() < 0)
{
int y = box.getDY();
box.moveY(-y);
}
}
}
*/
}
I'm trying to get this to work so that the rectangle will bounce off the walls, increasing speed as it bounces, however it will obviously increase so quickly that it will bounce out of the screen eventually. I want to find out when it goes out of bounds, then find the speed it was at when it went out of bounds, and then remove THAT speed by 1, without incrementing speed after it goes out of bounds, allowing it to stay fast, without causing the out of bounds speed again. I'm kinda burned out from work and classes, and can't think. Anything anyone can do to help, even a bump in the right direction while I continue tot google search would be wonderful. Thank you.