tags:

views:

1106

answers:

4

Hi,

Very simple question but I can't do it. I have 3 classes:

DrawCircle class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DrawCircle extends JPanel
{
    private int w, h, di, diBig, diSmall, maxRad, xSq, ySq, xPoint, yPoint;
    public DrawFrame d;

    public DrawCircle()
    {
     w = 400;
     h = 400;
     diBig = 300;
     diSmall = 10;
     maxRad = (diBig/2) - diSmall;
     xSq = 50;
     ySq = 50;
     xPoint = 200;
     yPoint = 200;
    }

    public void paintComponent(Graphics g)
    {
     super.paintComponent(g);
     g.setColor(Color.blue);
     g.drawOval(xSq, ySq, diBig, diBig);

     for(int y=ySq; y<ySq+diBig; y=y+diSmall*2)
     {
      for(int x=xSq; x<w-xSq; x=x+diSmall)
      {
       if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))<= maxRad)
        {
         g.drawOval(x, y, diSmall, diSmall);
        }    
      }
     }

     for(int y=ySq+10; y<ySq+diBig; y=y+diSmall*2)
     {
      for(int x=xSq+5; x<w-xSq; x=x+diSmall)
      {
       if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))<= maxRad)
       {
        g.drawOval(x, y, diSmall, diSmall);
       } 
      }
     }
    }
}

DrawFrame class

public class DrawFrame extends JFrame
{
    public DrawFrame()
    {
     int width = 400;
     int height = 400;

     setTitle("Frame");
     setSize(width, height);

     addWindowListener(new WindowAdapter()
     {
      public void windowClosing(WindowEvent e)
      {
       System.exit(0);
      }
     });

     Container contentPane = getContentPane();
     contentPane.add(new DrawCircle());
    }
}

CircMain class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CircMain 
{
    public static void main(String[] args)
    {
     JFrame frame = new DrawFrame();
     frame.show();
    }
}

One class creates a frame, the other draws a circle and fills it with smaller circles. In DrawFrame I set width and height. In DrawCircle I need to access the width and height of DrawFrame. How do I do this?

I've tried making an object and tried using .getWidth and .getHeight but can't get it to work. I need specific code here because I've tried a lot of things but can't get it to work. Am I declaring width and height wrong in DrawFrame? Am creating the object the wrong way in DrawCircle?

Also, the variables i use in DrawCircle, should I have them in the constructor or not?

+5  A: 

You could make the variables public fields:

  ...
  public int width;
  public int height;

  DrawFrame()
  {
    this.width = 400;
    this.height = 400;
  }
  ...

You could then access the variables like so:

DrawFrame frame = new DrawFrame();
int theWidth = frame.width;
int theHeight = frame.height;

A better solution, however, would be to make the variables private fields add two accessor methods to your class, keeping the data in the DrawFrame class encapsulated:

 ...

 private int width;
 private int height;

 DrawFrame()
 {
    this.width = 400;
    this.height = 400;
 }

  public int getWidth()
  {
     return this.width;
  }

  public int getHeight()
  {
     return this.height;
  }
  ...

Then you can get the width/height like so:

  DrawFrame frame = new DrawFrame();
  int theWidth = frame.getWidth();
  int theHeight = frame.getHeight();

I strongly suggest you use the latter method.

Peter
i second the strong suggestion on the latter method (as do many others, i presume. see: http://stackoverflow.com/questions/393001/do-any-other-developers-get-yelled-at-for-making-every-thing-public-closed)
akf
Hi,Thanks for your help. I tried something like this earlier. I followed your code and still can't get it to work. There must be a problem somewhere else. Do you have any suggestions. The code in the OP works. My Main class looks like this:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CircMain { public static void main(String[] args) { JFrame frame = new DrawFrame(); frame.show(); }}Is this what is causing the problem?
What's the problem exactly?
Peter
A: 

I've tried making an object and tried using .getWidth and .getHeight but can't get it to work.

That´s because you are not setting the width and height fields in JFrame, but you are setting them on local variables. Fields HEIGHT and WIDTH are inhereted from ImageObserver

Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

See http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html

If width and height represent state of the frame, then you could refactorize them to fields, and write getters for them.

Then, you could create a Constructor that receives both values as parameters

public class DrawFrame extends JFrame {
 private int width;
 private int height;

 DrawFrame(int _width, int _height){

   this.width = _width;
   this.height = _height;

   //other stuff here
}
 public int getWidth(){}
 public int getHeight(){}

 //other methods
}

If widht and height are going to be constant (after created) then you should use the final modifier. This way, once they are assigned a value, they can´t be modified.

Also, the variables i use in DrawCircle, should I have them in the constructor or not?

The way it is writen now, will only allow you to create one type of circle. If you wan´t to create different circles, you should overload the constructor with one with arguments).

For example, if you want to change the attributes xPoint and yPoint, you could have a constructor

public DrawCircle(int _xpoint, int _ypoint){
  //build circle here.
 }

EDIT:

Where does _width and _height come from?

Those are arguments to constructors. You set values on them when you call the Constructor method.

In DrawFrame I set width and height. In DrawCircle I need to access the width and height of DrawFrame. How do I do this?

DrawFrame(){
   int width = 400;
   int height =400;

   /*
   * call DrawCircle constructor
   */
   content.pane(new DrawCircle(width,height));

   // other stuff

}

Now when the DrawCircle constructor executes, it will receive the values you used in DrawFrame as _width and _height respectively.

EDIT:

Try doing

 DrawFrame frame = new DrawFrame();//constructor contains code on previous edit.
 frame.setPreferredSize(new Dimension(400,400));

http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html

Tom
Thatnks for your help, and apologies for not getting it. I can't get it to work. Unfortunately I'm not better off that I was 5 hours ago. Where does _width and _height come from? I'm sort of getting bits of lots of different ways of doing, but no specific one way of doing it.
Ok, see my edit. I think thats it!
Tom
But what passes them into the constructor? I assumed from my main:public static void main(String[] args) { JFrame frame = new DrawFrame(400,400); frame.show(); }I tried this and it didn't work. Essentially I'm still where I was at 9am this morning. I suspect the stuff you are telling me doesn't work with the other code I have.Thanks for your help, I'll leave learning java til another day.
ok. See my edit now. Maybe you should try something simpler. I dont think this is the way to learn either java or swing.
Tom
I'm totally confused. Too many bit-part answers. I'm too new to java to not get confused amongst all the suggestions. I spent 8 hours trying to pass/access a variable. Sometimes you just need to see the code to get it. I suspect I came close numerous times. Thanks for your help but you totally confused me.
A: 

if what you need is the width and height of the frame in the circle, why not pass the DrawFrame width and height into the DrawCircle constructor:

public DrawCircle(int w, int h){
    this.w = w;
    this.h = h;
    diBig = 300;
    diSmall = 10;
    maxRad = (diBig/2) - diSmall;
    xSq = 50;
    ySq = 50;
    xPoint = 200;
    yPoint = 200;
}

you could also add a couple new methods to DrawCircle:

public void setWidth(int w) 
   this.w = w;

public void setHeight(int h)
   this.h = h;

or even:

public void setDimension(Dimension d) {
   w=d.width;
   h=d.height;
}

if you go down this route, you will need to update DrawFrame to make a local var of the DrawCircle on which to call these methods.

edit:

when changing the DrawCircle constructor as described at the top of my post, dont forget to add the width and height to the call to the constructor in DrawFrame:

public class DrawFrame extends JFrame {
 public DrawFrame() {
    int width = 400;
    int height = 400;

    setTitle("Frame");
    setSize(width, height);

    addWindowListener(new WindowAdapter()
    {
            public void windowClosing(WindowEvent e)
            {
                    System.exit(0);
            }
    });

    Container contentPane = getContentPane();
    //pass in the width and height to the DrawCircle contstructor
    contentPane.add(new DrawCircle(width, height));
 }
}
akf
I get errors saying "the constructor DrawCircle is undefined"
johnrambo, your compiler was probably complaining because you changed the params to the constructor but you didnt change the call in DrawFrame which was still referencing the no-arg constructor. see my edit, please.
akf
A: 

I hope I'm understanding the problem correctly, but it looks like you don't have a reference back to your DrawFrame object from DrawCircle.

Try this:

Change your constructor signature for DrawCircle to take in a DrawFrame object. Within the constructor, set the class variable "d" to the DrawFrame object you just took in. Now add the getWidth/getHeight methods to DrawFrame as mentioned in previous answers. See if that allows you to get what you're looking for.

Your DrawCircle constructor should be changed to something like:

public DrawCircle(DrawFrame frame)
{
    d = frame;
    w = 400;
    h = 400;
    diBig = 300;
    diSmall = 10;
    maxRad = (diBig/2) - diSmall;
    xSq = 50;
    ySq = 50;
    xPoint = 200;
    yPoint = 200;
}

The last line of code in DrawFrame should look something like:

contentPane.add(new DrawCircle(this));

Then, try using d.getheight(), d.getWidth() and so on within DrawCircle. This assumes you still have those methods available on DrawFrame to access them, of course.

Lloyd McFarlin
Hi, tried it. Can't get it to work. I've tried so many diofferent things it's possible the error is from somewhere else. I think you understand my problem, but just let me clarify:I set the width and height in DrawFrameIn DrawCircle I need to be able to access the values of width and height. In my code above (which works) I'm resetting manually in the constructor rather than getting it from DrawFrame. I need to be able to set the width and height once only.
I see, I copied your constructor for DrawCircle exactly and just added the parameter and the first line. In light of your comment, it seems like it would also be necessary to add: "w = d.getWidth(); h = d.getHeight();" in place of "w = 400; h = 400;"
Lloyd McFarlin