从另一个类访问变量
非常简单的问题,但我做不到。我有3个班级:
绘制圆
类
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);
}
}
}
}
}
绘制帧
类
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());
}
}
马戏团主
类
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();
}
}
一个类创建一个框架,另一个类绘制一个圆并用较小的圆填充它。在我设置宽度和高度。在我需要访问的宽度和高度.我该怎么做?DrawFrame
DrawCircle
DrawFrame
我尝试过制作一个对象,并尝试使用,但无法使其工作。我在这里需要特定的代码,因为我已经尝试了很多事情,但无法让它工作。我是否在 中声明宽度和高度错误?我在 中以错误的方式创建对象?.getWidth
.getHeight
DrawFrame
DrawCircle
另外,我在 中使用的变量,我是否应该在构造函数中使用它们?DrawCircle