I have an issue I can't figure out. I am trying to display a bunch of boxes randomly around the screen but instead it's like it only displays the boxes in one line. It's like they line up instead of displaying randomly even though the values I log are super random.
Here is an image of how it's currently looking:
Here is my JPanel
extension used to draw the boxes:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class CustomComponent extends JPanel {
private Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,Color.GRAY,Color.GREEN, Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW};
double r;
double maxWidth = 2000;
double maxHeight = 2000;
double width = maxWidth;
double height = maxHeight;
public CustomComponent() {
setMaximumSize(new Dimension((int) maxWidth,(int) maxHeight));
setPreferredSize(new Dimension((int) maxWidth,(int) maxHeight));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int count = 0;
while (count < 50) {
r = Math.random();
g.setColor(colors[(int)(colors.length * r)]);
g.fillRect((int) (1000*r), (int) (1000*r), (int) (1000*r), (int) (1000*r));
count++;
}
}
}
The CustomComponent
is used here:
public JPanel northComponent() {
JPanel panel = new JPanel();
CustomComponent j = new CustomComponent();
panel.add(j);
return panel;
}
I'm using a layout manager with BorderLayout
where the panel object is positioned north.
Aucun commentaire:
Enregistrer un commentaire