为什么不显示Graphics2D矩形?

时间:2021-10-02 21:45:24

I need to be able to display filled rectangles for the program i am creating, however the following code produces the following GUI with only the black text 'test' after calling start then change, could anyone explayin why please?

我需要能够显示我正在创建的程序的填充矩形,但是下面的代码生成以下GUI,在调用start然后更改后只有黑色文本'test',是否有人可以解释为什么请?

package core;

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

@SuppressWarnings("serial")
public class GUI extends JFrame{

private Graphics2D g;
private int[][][] clickable;

public void start(){

    this.setSize(500, 500);
    this.setTitle("Placeholder");
    this.setVisible(true);
    g = (Graphics2D) this.getGraphics();
}       

public void change(String[] fields, int type[], boolean forwards){
    g.setColor(new Color(28,35,57));
    g.drawRect(0, 0, 100, 100);
    g.drawRect(50, 50, 150, 150);
    g.fillRect(0, 0, 100, 100);
    g.drawString("test", 300, 300);     
}
}

And here is what it looks like ..

这就是它的样子......

为什么不显示Graphics2D矩形?

1 个解决方案

#1


1  

Drawing on Swing components (like JFrame) works only in onPaint event. The event can be fired using repaint() method. This event fires automatically when frame needs to be painted. To implement this event behavior override paint() method.

在Swing组件上绘图(如JFrame)仅适用于onPaint事件。可以使用repaint()方法触发事件。需要绘制框架时,此事件会自动触发。要实现此事件行为,请覆盖paint()方法。

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

public class GUI extends JFrame{

    private Graphics2D g;

    public void start(){
        this.setSize(500, 500);
        this.setTitle("Placeholder");
        this.setVisible(true);
    }       

    public void change(){
        g.setColor(new Color(28,35,57));
        g.drawRect(0, 0, 100, 100);
        g.drawRect(50, 50, 150, 150);
        g.fillRect(0, 0, 100, 100);
        g.drawString("test", 300, 300);
    }

    public void paint(Graphics g2d){
        g = (Graphics2D) g2d;
        change();
    }

    public static void main(String[] args){
        GUI frame = new GUI();
        frame.start();
    }

}

#1


1  

Drawing on Swing components (like JFrame) works only in onPaint event. The event can be fired using repaint() method. This event fires automatically when frame needs to be painted. To implement this event behavior override paint() method.

在Swing组件上绘图(如JFrame)仅适用于onPaint事件。可以使用repaint()方法触发事件。需要绘制框架时,此事件会自动触发。要实现此事件行为,请覆盖paint()方法。

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

public class GUI extends JFrame{

    private Graphics2D g;

    public void start(){
        this.setSize(500, 500);
        this.setTitle("Placeholder");
        this.setVisible(true);
    }       

    public void change(){
        g.setColor(new Color(28,35,57));
        g.drawRect(0, 0, 100, 100);
        g.drawRect(50, 50, 150, 150);
        g.fillRect(0, 0, 100, 100);
        g.drawString("test", 300, 300);
    }

    public void paint(Graphics g2d){
        g = (Graphics2D) g2d;
        change();
    }

    public static void main(String[] args){
        GUI frame = new GUI();
        frame.start();
    }

}