在加载时试图让窗口自行绘制

事实证明这比我想象的要困难得多。我只是在做一些学习和使用绘图功能。我想要的是一个程序启动一个包含一个矩形的窗口。该矩形的大小将根据窗口的大小进行缩放(即,如果窗口为1000px且矩形设置为按比例缩放为90%,则矩形将为900px)。我有数学计算出如何居中并确定它的大小,但通过使用存根,我发现使用object.getWidth()和height等,每次都返回0,完全为数学浆纱。 在考虑了一段时间之后,我假设它是因为它试图从仍在构造的对象获得宽度和高度。根据我自己的逻辑,只要你在构造函数的构建部分之后进行数学运算,它应该没问题,但是唉 - 我似乎错了。 我尝试创建单独的类来创建一个框架和一个绘制图形的框架。绘图对象接受JFrame作为构造函数的一部分并尝试绘制它。我分别实例化每一个,先框架,然后绘制对象(将框架对象发送给它),但这似乎也无效。 有什么建议? 编辑: 根据安德鲁的建议。这是我要添加到窗口的面板对象
package scaling_test;
import java.awt.*;
import javax.swing.*;

public class MyDrawing extends JPanel
{

    public MyDrawing() throws HeadlessException
    {
        this.setVisible(true);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        //set scaling
        int usrScale = 90;
        int scaleRef = (100 - usrScale) / 2;
        int xStart = this.getWidth() * (scaleRef / 100);
        int yStart = this.getHeight() * (scaleRef / 100);
        int width = (usrScale / 100) * this.getWidth();
        int height = (usrScale / 100) * this.getHeight(); 

        //draw square outline
        g.setColor(Color.green);
        g.fillRect(xStart, yStart, width, height);
    }
}
这是添加jpanel对象的窗口:
package scaling_test;
import java.awt.*;
import javax.swing.*;

public class DrawThis extends JFrame
{
    MyDrawing drawing;

    public DrawThis() throws HeadlessException
    {
        drawing = new MyDrawing();
        this.add(drawing);
    }
}
而发射器
package scaling_test;

public class ScaleTest
{
    public static void main(String[] args)
    {
        DrawThis program = new DrawThis();
        program.setBounds(250, 250, 800, 600);
        program.setVisible(true);
    }
}
更新的面板(仍然无法正常工作):
package scaling_test;
import java.awt.*;
import javax.swing.*;

public class MyDrawing extends JPanel
{

    public MyDrawing() throws HeadlessException
    {
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);

        //set scaling
        int usrScale = 90;
        int scaleRef = (100 - usrScale) / 2;
        int xStart = this.getWidth() * scaleRef / 100;
        int yStart = this.getHeight() * scaleRef / 100;

        int width = usrScale * this.getWidth() / 100;
        int height = usrScale * this.getHeight() / 100;

        //draw square outline
        g.setColor(Color.green);
        g.fillRect(xStart, yStart, width, height);
    }
}
    
已邀请:
当paintComponent(...)方法传递Graphics对象时,为什么要使用getGraphics(...)方法?此外,您应该调用super.paintComponent(g)作为方法中的第一个语句。我不知道你为什么评论它。 阅读自定义绘画的Swing教程中的部分以获取工作示例。 如果您需要更多帮助,请发布您的SSCCE,这样我们就不会猜测您的实际代码是什么样的。 编辑: 您是否添加了任何显示语句以查看drawRect(...)方法中使用的值是否有意义?不要认为你的数学论坛是正确的。首先,您需要了解整数计算的工作原理:
//        int width = (usrScale / 100) * this.getWidth();
//        int height = (usrScale / 100) * this.getHeight();
        int width = usrScale * this.getWidth() / 100;
        int height = usrScale * this.getHeight() / 100;
在注释掉的代码(usrScale / 100)= 0中,因为在完成乘法之前该值被转换为整数。     
paintComponent(Graphics)
方法中计算加入
JFrame
JPanel
。只有在屏幕上“实现”组件后,才会调用
paintComponent()
方法。     
没有看到你的代码就有点棘手,但有一个选择是setPreferredSize(x,y);在JFrame上,然后调用pack();之后,您可以检查窗口大小。     

要回复问题请先登录注册