无法在JPanel上绘制椭圆形 [英] Can not draw oval on a JPanel

查看:96
本文介绍了无法在JPanel上绘制椭圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Netbeans的GUI生成器创建的JFrame,其中仅包含一个JPanel.我创建了一个方法getPanel来获取对此JPanel的引用:

I have a JFrame created with GUI builder of Netbeans, which contains a JPanel only. I have created a method getPanel for getting a reference to this JPanel:

public class ShowDrawings extends JFrame {

    public ShowDrawings() {
        initComponents();
    }

    public JPanel getPanel(){
        return panel;
    }

    private JPanel panel;
}

我正在执行的主要功能是:

In my main function I am doing:

public class Main {
    public static void main(String[] args){
        ShowDrawings sd = new ShowDrawings();
        sd.setSize(800, 600);
        Graphics g = sd.getPanel().getGraphics();
        g.setColor(Color.BLACK);
        g.drawOval(400, 300, 50, 50);
        sd.getPanel().paint(g);
        sd.repaint();
        sd.setVisible(true);
    }
}

但是它不会画任何东西.请帮我. 我看过一些相关的问题,但它们都建议扩展JPanel并覆盖其paint方法.但是我不想那样做. 谢谢.

But it does not draw anything. Please help me. I have looked some related questions but they are all suggesting extending JPanel and overriding its paint method. But I did not want to do that way. Thanks.

推荐答案

我看了一些相关的问题,但它们都在暗示 扩展JPanel并覆盖其paint方法.但是我不想 这样做

I have looked some related questions but they are all suggesting extending JPanel and overriding its paint method. But I did not want to do that way

您不应覆盖JPanel paint()方法,而应覆盖paintComponent(..).这是最佳做法,如果您想要不会产生异常的代码,则应这样做.同样,按照您当前的方法(如您所见)进行操作,会使持久性图形的创建变得更加困难,因为在repaint()

You should not override JPanel paint() method, rather paintComponent(..). This is best practice and should be done if you want code that will not produce anomalies. Also doing it in your current approach (as you have seen) makes creating persistent drawings a lot harder as they are wiped away on repaint()

宁可扩展JPanel并覆盖paintComponent(Graphics g),也不要忘记在覆盖的paintComponent(..)方法中作为第一次调用来调用super.paintComponent(g).同样不要忘记覆盖JPanelgetPreferredSize(),以便我们可以返回正确的尺寸,并且pack()可能会在JFrame上调用(+1到@mKorbels注释):

Rather extend JPanel and override paintComponent(Graphics g) not forgetting to call super.paintComponent(g) as first call in overridden paintComponent(..) method. Also dont forget to override getPreferredSize() of JPanel so that we can return correct dimensions and pack() may be called on JFrame (+1 to @mKorbels comment):

这是一些示例代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel testPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                super.paintComponent(grphcs);

                Graphics2D g2d = (Graphics2D) grphcs;

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

                g2d.setColor(Color.GREEN);
                //g2d.drawOval(10,10,100,100);//I like fill :P
                g2d.fillOval(10,10,100,100);

            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(150, 150);
            }
        };

        frame.add(testPanel);

        frame.pack();
        frame.setVisible(true);
    }
}

这篇关于无法在JPanel上绘制椭圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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