'Trouble getting multiple moving JComponent shapes into JFrame
I'm new to Java (particularly graphics) and am currently working on a task that requires me to have multiple JComponent shapes bouncing off the JFrame walls. The core of the "bouncing off the walls" code was provided (RectangleMover).
I have 3 separate classes. While I was able to get one rectangle to bounce correctly, I can't seem to figure out how to get multiple shapes into the frame.
This is what I came up with: having a JPanel extended class paint the multiple objects. However, now in the RectangleMover class, the shapes appear, but the moveBox method is no longer working despite the objects being of the correct type (Shape) as underlying Rectangle objects. I'm referring specifically to the shapes I have in the shapes array and the for-each loop in the Timer listener.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.awt.event.ActionEvent;
public class Shape extends JComponent {
public static int i=-1;
public static int j=0;
private Rectangle box;
private Graphics2D g2;
Color c1 = randomColor();
Color c2 = randomColor();
Color c3 = randomColor();
Color c4 = randomColor();
public Shape(int x, int y,int width,int height) {
box = new Rectangle(x, y, width, height);
}
public void draw(Graphics g) {
g2 = (Graphics2D) g;
if(box.width<125) {
g2.setColor(c1);
g2.fill(box);
}
else if(box.width>=125 && box.width<=200) {
g2.setColor(c2);
g2.fill(box);
}
else if(box.width>200 && box.width<250) {
g2.setColor(c3);
g2.fill(box);
}
else {
g2.setColor(c4);
g2.fill(box);
}
}
public Color randomColor() {
Random random = new Random();
int a = random.nextInt(255);
int b = random.nextInt(255);
int c = random.nextInt(255);
Color myColor = new Color(a,b,c);
return myColor;
}
public void moveBox(int dx, int dy) {
box.translate(dx, dy);
repaint();
}
public void growSize() {
box.width+=1;
box.height+=1;
}
public void shrinkSize() {
box.width-=1;
box.height-=1;
}
public class ShapePanel extends JPanel {
private Graphics2D g2;
Shape box1 = new Shape(0,0,20,20);
Shape box2 = new Shape(0,0,80,30);
Shape box3 = new Shape(0,0,40,80);
public Shape[] shapes = {box1, box2, box3};
public void paintComponent(Graphics g) {
g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillRect(0, 0, 500, 600);
for(Shape item: shapes) {
item.draw(g);
}
}
public Shape[] getArray() {
Shape[] newArray = shapes;
return newArray;
}
}
public class RectangleMover {
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 600;
private static int BOX_WIDTH = 20;
private static int BOX_HEIGHT = 20;
public static int x=0;
public static int y=0;
private static int dx=1;
private static int dy=1;
private Graphics2D g2;
private static ShapePanel sp = new ShapePanel();
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Screensaver");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(sp);
frame.revalidate();
frame.repaint();
frame.setVisible(true);
//Shape[] shapes = sp.getArray();
for(Shape item: sp.shapes)
System.out.println(item.toString());
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(x+BOX_WIDTH>= FRAME_WIDTH-260) {
dx=-1;
}
if(x<=0) {
dx=1;
}
if(y+BOX_HEIGHT>= FRAME_HEIGHT-180) {
dy=-1;
}
if(y<=0) {
dy=1;
}
x=x+dx;
y=y+dy;
//shapes.moveBox(dx,dy);
for(Shape item: sp.shapes) {
item.moveBox(dx, dy);
frame.revalidate();
frame.repaint();
}
if(dx==-1) {
//shapes.shrinkSize();
//rect.shrinkSize();
}
if(dx==1) {
//shapes.growSize();
//rect.growSize();
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
