Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I've tried making an object and tried using .getWidth and .getHeight but can't get it to work.</p> </blockquote> <p>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</p> <pre><code>Fields inherited from interface java.awt.image.ImageObserver ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH </code></pre> <p>See <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html" rel="nofollow noreferrer">http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html</a></p> <p>If width and height represent state of the frame, then you could refactorize them to fields, and write getters for them.</p> <p>Then, you could create a Constructor that receives both values as parameters</p> <pre><code>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 } </code></pre> <p>If widht and height are going to be constant (after created) then you should use the <strong>final</strong> modifier. This way, once they are assigned a value, they can´t be modified.</p> <blockquote> <p>Also, the variables i use in DrawCircle, should I have them in the constructor or not?</p> </blockquote> <p>The way it is writen now, will only allow you to create <strong>one type of circle</strong>. If you wan´t to create different circles, you should overload the constructor with one with arguments).</p> <p>For example, if you want to change the attributes xPoint and yPoint, you could have a constructor</p> <pre><code>public DrawCircle(int _xpoint, int _ypoint){ //build circle here. } </code></pre> <p>EDIT: </p> <pre><code>Where does _width and _height come from? </code></pre> <p>Those are arguments to constructors. You set values on them when you call the Constructor method.</p> <blockquote> <p>In DrawFrame I set width and height. In DrawCircle I need to access the width and height of DrawFrame. How do I do this?</p> </blockquote> <pre><code>DrawFrame(){ int width = 400; int height =400; /* * call DrawCircle constructor */ content.pane(new DrawCircle(width,height)); // other stuff } </code></pre> <p>Now when the DrawCircle constructor executes, it will receive the values you used in DrawFrame as _width and _height respectively.</p> <p>EDIT:</p> <p>Try doing</p> <pre><code> DrawFrame frame = new DrawFrame();//constructor contains code on previous edit. frame.setPreferredSize(new Dimension(400,400)); </code></pre> <p><a href="http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html" rel="nofollow noreferrer">http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload