Note that there are some explanatory texts on larger screens.

plurals
  1. POaccessing a variable from another class
    primarykey
    data
    text
    <p>Very simple question but I can't do it. I have 3 classes:</p> <p><strong><code>DrawCircle</code> class</strong></p> <pre><code>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&lt;ySq+diBig; y=y+diSmall*2) { for(int x=xSq; x&lt;w-xSq; x=x+diSmall) { if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))&lt;= maxRad) { g.drawOval(x, y, diSmall, diSmall); } } } for(int y=ySq+10; y&lt;ySq+diBig; y=y+diSmall*2) { for(int x=xSq+5; x&lt;w-xSq; x=x+diSmall) { if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))&lt;= maxRad) { g.drawOval(x, y, diSmall, diSmall); } } } } } </code></pre> <p><strong><code>DrawFrame</code> class</strong></p> <pre><code>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()); } } </code></pre> <p><strong><code>CircMain</code> class</strong></p> <pre><code>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(); } } </code></pre> <p>One class creates a frame, the other draws a circle and fills it with smaller circles. In <code>DrawFrame</code> I set width and height. In <code>DrawCircle</code> I need to access the width and height of <code>DrawFrame</code>. How do I do this?</p> <p>I've tried making an object and tried using <code>.getWidth</code> and <code>.getHeight</code> 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 <code>DrawFrame</code>? Am creating the object the wrong way in <code>DrawCircle</code>?</p> <p>Also, the variables i use in <code>DrawCircle</code>, should I have them in the constructor or not?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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