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?