繁体   English   中英

Java图形试图绘制矩形

[英]Java Graphics trying to draw rectangle

我的目标是制作一个包含矩形的类,然后使用它并在其他类中对其进行更改。 我试图编写这段代码,并创建一个对象Rect rect = new Rect(); 但是,当我启动程序时,矩形不会显示。 我也试图用window.add(rect);添加它window.add(rect); 但有同样的问题,我敢肯定我做错了什么,但我真的不知道该怎么办。

我尝试的另一件事是从其他类Rect.drawRect(g);调用方法Rect.drawRect(g); 但是然后它要求“ Argument”,如果我像在drawRect方法中那样添加Argument g,它会说“ g无法解析为变量”

我希望有人能解释并告诉我我做错了什么,也很高兴知道如何制作矩形或圆形,然后在其他课程中使用它,并可能更改其颜色和大小,我只在一堂课

import javax.swing.JFrame;

public class Main extends Rect{

    public static void main(String[] args ) {

        JFrame window = new JFrame("test");
        window.setSize(1000, 800);
        window.setVisible(true);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Rect rect = new Rect();
    }   
}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Rect extends JPanel{

    public void drawRect(Graphics g){
        g.setColor(Color.RED);
        g.fillRect(100, 100, 200, 200); 
    }
}

UDP:您需要重写void paintComponent(Graphics g)而不是void drawRect(Graphics g)并在方法内部调用super.paintComponent(g) 然后可以使用window.add(rect); 感谢@FredK进行更正

最重要的是,您需要编写一些代码来进行绘画。 这是通过重写Rect类中的paintComponent方法来完成的,如下所示:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(100, 100, 200, 200); 
    }

第二个问题是您希望能够从其他类中更改矩形的颜色和大小。 对于一个简单的示例,可以通过在Rect类中添加一些静态值来轻松完成此操作:

    public static Color myColor = Color.RED;
    public static int myX = 100;
    public static int myY = 100;
    public static int myWidth = 200;
    public static int myHeight = 200;

现在更新您的绘画方法以使用静态值:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(myColor);
        g.fillRect(myX, myY, myWidth, myHeight); 
    }

现在,无论何时何地使用“ Rect面板,它都会根据静态值显示矩形。

例如,下面是一个简单且有效的程序,请注意它如何使用以下内容:

    //create Rect
    Rect rect = new Rect();
    //set the size of the new panel
    rect.setPreferredSize(new Dimension(800, 600));
    //add the rect to your JFrame
    window.add(rect);
    //now you can change the color for all Rect instances
    //Note how I use Rect instead of rect, however, both will work.
    Rect.myColor = Color.BLUE;
    //And dont forget to repaint it if you want to see the changes immediatly
    rect.repaint();

完整示例,主类:

import javax.swing.JFrame;
import java.awt.Color;

public class Main{

    //Note how we don't need to extend the Rect class (It just adds confusion)
    public static void main(String[] args ) {

        JFrame window = new JFrame("test");
        window.setSize(1000, 800);
        window.setVisible(true);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create Rect
        Rect rect = new Rect();
        //set the size of the new panel
        rect.setPreferredSize(new Dimension(800, 600));
        //add the rect to your JFrame
        window.add(rect);


        //now you can change the color for all Rect instances
        //Note how I use Rect instead of rect, however both will work.
        Rect.myColor = Color.BLUE;
        //And dont forget to update it
        rect.repaint();
    }
}

和Rect类:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Rect extends JPanel{
    public static Color myColor = Color.RED;
    public static int myX = 10;
    public static int myY = 10;
    public static int myWidth = 200;
    public static int myHeight = 200;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(myColor);
        g.fillRect(myX, myY, myWidth, myHeight); 
    }
}

请注意,如果您不想每次更改颜色/大小时都调用Rect.repaint()则只需创建一个新方法即可更改每个值并包含repaint() ,例如:

public void changeWidth(int width){
    myWidth  = width;
    repaint();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM