JPanel中的java graphics2D [英] java graphics2D inside JPanel

查看:41
本文介绍了JPanel中的java graphics2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to draw some simple shapes onto a JPanel but am having some difficulty. Apologies if this question appears to have been answered before but the other answers don't seem to help.

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

I followed a simple tutorial and was successful in drawing some basic shapes onto a JFrame, but when I moved the code how it was into a new class which extends JPanel, nothing appears on screen.

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

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

I've tried setting preferred size, and revalidating and repainting. I've added a call to super.paintComponent, although neither of those two were necessary when I was drawing straight onto a JFrame. I ensured I was overriding paintComponent and changed it from public to protected. All following advice from similar threads, but nothing seems to work. I've stepped through in debugger mode and ensured it goes into the right method, and even watched it go into the paintManager, but still nothing appears on the window.

推荐答案

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

You've forget to set the appropriate layout manager.

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

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

When you extends JFrame your default layout is the BorderLayout, but when you extends JPanel your default layout is the FlowLayout.

这篇关于JPanel中的java graphics2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆