I've somehow lost the concept of the easiest part of this assignment...
OK, We have a program which draws random rectangles based on mousePressed and mouseReleased coordinates. I am adding a Circle class which draws circles as well. I have the class written, the draw method written but now when I try to call drawCircle, it says it cannot find the method.
How do I call this method? Have I missed something exceedingly easy?
(Also, I am part way through the process of adding radio buttons to choose drawing either rectangles or circles, some of the code is there but it hasn't been implemented yet...don't mind that...)
Any other suggestions are appreciated, as well. Thank you!
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MouseTest {
private JFrame frame;
private boolean tracking;
private int xstart;
private int ystart;
private int xend;
private int yend;
private int borderWidth = 5; //width of border of drawing area
public static void main (String [] arg) {
MouseTest first = new MouseTest();
}//main
public MouseTest() {
tracking = false;
frame = new JFrame();
frame.setBounds(250,98,300,200);
frame.setTitle("Window number three");
Container cp = frame.getContentPane();
JButton track = new JButton("Track Mouse");
//Radio Buttons
JRadioButton rectangles = new JRadioButton("Draw Rectangles", true);
JRadioButton circles = new JRadioButton("Draw Circles");
ButtonGroup group = new ButtonGroup();
group.add(rectangles);
group.add(circles);
track.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
trackMouse();
}
});
JPanel top = new JPanel();
top.add(track);
cp.add(top,BorderLayout.NORTH);
MyPanel pane = new MyPanel();
cp.add(pane,BorderLayout.CENTER);
pane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xstart = e.getX();
ystart = e.getY();
}
public void mouseReleased(MouseEvent e) {
xend = e.getX();
yend = e.getY();
if (xend < xstart) { int tmp = xstart; xstart = xend; xend = tmp; }
if (yend < ystart) { int tmp = ystart; ystart = yend; yend = tmp; }
frame.repaint();
}
});
pane.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
if (tracking) {
int x = e.getX();
int y = e.getY();
msg("(" + x + ", " + y + ")");
}
}
});
frame.setVisible(true);
}//constructor
public void trackMouse() {
tracking = !tracking;
}//trackMouse
public void msg(String s) {
System.out.println(s);
}//msg
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
msg("H = " + height + ", w = " + width);
g.setColor(Color.BLACK);
for (int delta = 0; delta < borderWidth; delta++) {
g.drawRect(delta,delta,width-(2*delta),height-(2*delta));
}//for
if (xstart != xend || ystart != yend) {
int red = (int)(256*Math.random());
int green = (int)(256*Math.random());
int blue = (int)(256*Math.random());
g.setColor(new Color(red, green, blue));
msg("Colors are: " + red + " - " + green + " - " + blue );
msg("Drawing: " + xstart + " - " + ystart + " - " + xend + " - " + yend);
msg("Width is " + (xend-xstart) + " - Height is " + (yend-ystart));
g.fillRect(xstart,ystart,xend-xstart,yend-ystart);
g.fillRect(xstart,ystart,45,45);
}//if
}//paintComponent
}//MyPanel
public class Circle
{
private int radius;
public Circle(){
Circle circle = new Circle();
}
public void drawCircle(Graphics gc, int x, int y, int width, int height)
{
gc.setColor(Color.BLACK);
if (xstart == xend || ystart == yend){
int red = (int)(256*Math.random());
int green = (int)(256*Math.random());
int blue = (int)(256*Math.random());
gc.setColor(new Color(red, green, blue));
gc.drawOval(30, 30, width, height);
}//if
}//drawCircle
}//Circle Class
}//MouseTest