JPanel 内的 java Graphics2D

Ric*_*r32 1 java graphics swing awt jpanel

我正在尝试在 JPanel 上绘制一些简单的形状,但遇到一些困难。如果这个问题之前似乎已经得到解答,但其他答案似乎没有帮助,我们深表歉意。

我遵循了一个简单的教程,并成功地将一些基本形状绘制到 JFrame 上,但是当我将代码移动到扩展 JPanel 的新类中时,屏幕上没有显示任何内容。

public class TestingGraphics extends JFrame{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    new TestingGraphics();

}

public TestingGraphics(){

    this.setSize(1000,1000);
    this.setPreferredSize(new Dimension(1000,1000));
    this.setTitle("Drawing tings");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
    this.setVisible(true);

}

public class TestingPanelGraphics extends JPanel {

public TestingPanelGraphics(){

    this.setSize(1000,1000);
    this.setPreferredSize(new Dimension(1000,1000));
    this.add(new DrawStuff(), BorderLayout.CENTER);
    revalidate();
    repaint();
    this.setVisible(true); //probably not necessary

}

private class DrawStuff extends JComponent{

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        Graphics2D graph2 = (Graphics2D) g;

        graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



        Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

        graph2.setColor(Color.BLACK);
        graph2.draw(rootRect);
}
Run Code Online (Sandbox Code Playgroud)

我尝试过设置首选尺寸,然后重新验证和重新绘制。我添加了对 super.paintComponent 的调用,尽管当我直接在 JFrame 上绘图时,这两个调用都不是必需的。我确保我重写了paintComponent,并将其从公共更改为受保护。所有以下建议都来自类似的线程,但似乎没有任何效果。我已经在调试器模式下单步执行并确保它进入正确的方法,甚至看着它进入paintManager,但窗口上仍然没有任何显示。

Ser*_*kyy 5

您忘记设置适当的布局管理器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestingGraphics extends JFrame{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        new TestingGraphics();

    }

    public TestingGraphics(){

        this.setSize(1000,1000);
        this.setPreferredSize(new Dimension(1000,1000));
        this.setTitle("Drawing tings");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
        this.setVisible(true);

    }

    public class TestingPanelGraphics extends JPanel {

        public TestingPanelGraphics(){
            setLayout(new BorderLayout());
            this.setPreferredSize(new Dimension(1000,1000));
            this.add(new DrawStuff(), BorderLayout.CENTER);
            revalidate();
            repaint();
            this.setVisible(true); //probably not necessary

        }

        private class DrawStuff extends JComponent{

            @Override
            protected void paintComponent(Graphics g){
                super.paintComponent(g);

                Graphics2D graph2 = (Graphics2D) g;

                graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



                Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

                graph2.setColor(Color.BLACK);
                graph2.draw(rootRect);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当您扩展 JFrame 时,您的默认布局是 BorderLayout,但是当您扩展 JPanel 时,您的默认布局是 FlowLayout。