Reputation: 147
I created a program that is supposed to draw a circle with each click(Random size and random color), and each circle is going to be an object. I don't know what is going on but my code doesn't work and I am pretty sure I am almost there.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Circle
{
private JFrame frame;
CircleObj circleObj;
Random rand;
int rColor;
int gColor;
int bColor;
int radius;
public static void main (String [] arg)
{
frame = new JFrame("Circles");
frame.setBounds(200, 100, 600, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel top = new JPanel();
rand = new Random();
top.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int xstart = e.getX();
int ystart = e.getY();
rColor = rand.nextInt(256);
gColor = rand.nextInt(256);
bColor = rand.nextInt(256);
radius = rand.nextInt(20);
circleObj = new CircleObj(xstart, ystart, rColor, gColor, bColor, radius);
repaint();
}
});
frame.add(top, BorderLayout.CENTER);
frame.setVisible(true);
}
}
and here is my CircleObj class
import javax.swing.*;
import java.awt.*;
public class CircleObj extends JPanel
{
private int xVal;
private int yVal;
private int red;
private int green;
private int blue;
private int circleRadius;
public CircleObj (int x, int y, int r, int g, int b, int rad)
{
xVal = x;
yVal = y;
red = r;
green= g;
blue = b;
circleRadius = rad;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.setColor(new Color(red, green, blue));
g.fillOval(xVal,yVal,circleRadius,circleRadius);
}
}
Upvotes: 0
Views: 49
Reputation: 147
Here is a second approach... How would I be able to tweak it store each circle in an object (Arraylist of objects) ?
public class Circle {
public static void main (String[] arg) {
JFrame frame = new JFrame("Circles");
CircleObj canvas = new CircleObj();
frame.add(canvas, BorderLayout.CENTER);
frame.setBounds(250, 98, 600, 480);
//frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} // end main
} //end Circle
CircleObj Class
public class CircleObj extends JPanel {
private int rColor;
private int gColor;
private int bColor;
private int radius;
private Random rand = new Random();
private int xStart;
private int yStart;
public CircleObj () {
addMouseListener(new MouseAdapter() {
public void mouseClicked (MouseEvent e) {
xStart = e.getX();
yStart = e.getY();
rColor = rand.nextInt(256);
gColor = rand.nextInt(256);
bColor = rand.nextInt(256);
radius = rand.nextInt(20);
System.out.println("xstart : " + xStart + ", ystart : " + yStart + ", rColor : " + rColor + ", gColor = " +
gColor + ", bColor: " + bColor + ", radius: " + radius);
repaint();
}
}); // end addMouseListener
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.setColor(new Color(rColor, gColor, bColor));
g.fillOval(xStart, yStart, radius, radius);
}
}
Upvotes: 2
Reputation: 285430
You add a MouseListener to the top JPanel, but when do you add the top JPanel to the top-level window, the GUI? --- In the MouseListener! So this will never work since the top JPanel is not part of the GUI initially, and only gets added when a MouseListener that is added to it is triggered. The MouseListener only works on components that are visible and on the GUI.
Next you create a CircleObj object, but never draw with it in any JComponent's or JPanel's paiintComponent method.
I am pretty sure I am almost there.
So no, not quite. You will want to re-start and:
ArrayList<CircleObj>
to this drawing JPanelpublic void draw(Grpahics g)
method that will draw it.repaint()
to get the drawing JPanel to draw.ArrayList<CircleObj>
calling each object's draw(g)
method.Upvotes: 2