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

Recommended Answers

All 3 Replies

When you try to call "drawCircle" where? Because in your code above, you don't have any call to that method, nor do you create an instance of your Circle class.

You certainly don't want this in your Circle class constructor

Circle circle = new Circle();

because it's going to call itself recursively until you overflow the stack.

That's the point, I suppose. I don't know how or where to add it?

OK, so I have changed the Circle class constructor to...

public class Circle{
        private int radius;
    
        public Circle(){
          Circle c = new drawCircle();      
	}

    
    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

I think this fixes the problem I had earlier (Thank you!) but now in the earlier part of the program, do I need to just call on "c" to use i in the 1st constructor?

I apologize, it has been five years since I've taken Java 1 and now I'm in part 2. While it is coming back, I have been working on this all day, trying different things and none of them are working. I'm just at loss.

I'm not even really looking for the easy answer but maybe a little guidance or a point in the right direction?

There is so much information on java in google that I get overwhelmed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.