从另一个类访问变量

很简单的问题,但我做不到。我有3节课:


DrawCircle 类


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 类


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());

    }

}

一类创建框架,另一类绘制一个圆圈,并用较小的圆圈填充它。在DrawFrame我设置宽度和高度。在DrawCircle我需要访问的宽度和高度DrawFrame。我该怎么做呢?


我试过制作一个对象并尝试使用.getWidth,.getHeight但无法使其正常工作。我在这里需要特定的代码,因为我尝试了很多事情,但无法正常工作。我在声明宽度和高度错误DrawFrame吗?创建对象的方式错误DrawCircle吗?


另外,我使用的变量DrawCircle是否应该在构造函数中使用?


莫回无
浏览 471回答 3
3回答

慕神8447489

如果您需要的是圆形中框架的宽度和高度,为什么不将DrawFrame的宽度和高度传递给DrawCircle构造函数:public DrawCircle(int w, int h){&nbsp; &nbsp; this.w = w;&nbsp; &nbsp; this.h = h;&nbsp; &nbsp; diBig = 300;&nbsp; &nbsp; diSmall = 10;&nbsp; &nbsp; maxRad = (diBig/2) - diSmall;&nbsp; &nbsp; xSq = 50;&nbsp; &nbsp; ySq = 50;&nbsp; &nbsp; xPoint = 200;&nbsp; &nbsp; yPoint = 200;}您还可以向DrawCircle添加一些新方法:public void setWidth(int w)&nbsp;&nbsp; &nbsp;this.w = w;public void setHeight(int h)&nbsp; &nbsp;this.h = h;&nbsp;甚至:public void setDimension(Dimension d) {&nbsp; &nbsp;w=d.width;&nbsp; &nbsp;h=d.height;}如果沿着这条路线走,则需要更新DrawFrame来制作DrawCircle的本地变量,以在这些变量上调用这些方法。编辑:如我的文章顶部所述,在更改DrawCircle构造函数时,请不要忘记在DrawFrame的构造函数调用中添加宽度和高度:public class DrawFrame extends JFrame {&nbsp;public DrawFrame() {&nbsp; &nbsp; int width = 400;&nbsp; &nbsp; int height = 400;&nbsp; &nbsp; setTitle("Frame");&nbsp; &nbsp; setSize(width, height);&nbsp; &nbsp; addWindowListener(new WindowAdapter()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void windowClosing(WindowEvent e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; Container contentPane = getContentPane();&nbsp; &nbsp; //pass in the width and height to the DrawCircle contstructor&nbsp; &nbsp; contentPane.add(new DrawCircle(width, height));&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java