画笔式 GUI 中的paintComponent() 与paint() 以及JPanel 与Canvas [英] paintComponent() vs paint() and JPanel vs Canvas in a paintbrush-type GUI

查看:24
本文介绍了画笔式 GUI 中的paintComponent() 与paint() 以及JPanel 与Canvas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个中得到了一些有趣的想法和批评,thisthis 帖子(有关相关 GUI 的代码,请参阅最后一篇文章).尽管如此,我仍然对一些事情感到困惑.主要是,显示用户介绍图形的最便宜的方式是什么?

I got some interesting ideas and criticism from this, this and this post (see last post for the code of the GUI in question). Nevertheless, I'm still quite confused about some things. Mainly, what is the least expensive way of displaying user-introduces graphics?

更具体地说,我使用了 JPanel 类中的 paintComponent() 方法,通过在 MouseDragged() 方法中创建此类的对象连同 paintComponent(getGraphics()) 方法(AuxClass2AuxClass1 相应).

More specifically, I used a paintComponent() method from JPanel class by making an object of this class in the MouseDragged() method together with paintComponent(getGraphics()) method (AuxClass2 and AuxClass1 accordingly).

显然,使用 getGraphics()paintComponent() 而不是 repaint() 是坏主意,我怀疑与内存有关用.每次用户拖动鼠标时也调用 AuxClass2 也是一个坏主意.

Apparently, using getGraphics() and paintComponent() instead of repaint() are bad ideas, I suspect something to do with memory use. Also calling the AuxClass2 every time the user drags the mouse is also a bad idea.

还有 JPanel 与 Canvas(即 Swing 与 awt)有点令人困惑.什么时候用?

Also JPanel vs Canvas (i.e. swing vs awt) is a bit confusing. What is used and when?

我一直在寻找解决方法,但没有找到,特别是对于 getGraphics() 方法:如何将图形添加到面板中?

I've been trying to find a workarounds, but have not found one, especially for the getGraphics() method: how else can the graphics be added to the panel?

推荐答案

我一直在寻找解决方法,但没有找到,特别是对于 getGraphics() 方法:图形还能如何添加到面板?

I've been trying to find a workarounds, but have not found one, especially for the getGraphics() method: how else can the graphics be added to the panel?

您记得需要将什么绘制为变量并在paintComponent() 中使用它.例如,您在其他问题中似乎试图做的事情如下:

You remember what needs to be painted as a variable and use that in paintComponent(). For example, what you seemed to be trying to do in your other question would look like:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintRectangle extends JPanel {

    private Point mouseLocation;

    public PaintRectangle() {
        setPreferredSize(new Dimension(500, 500));

        MouseAdapter listener = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                updateMouseRectangle(e);
            }

            private void updateMouseRectangle(MouseEvent e) {
                mouseLocation = e.getPoint();
                repaint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                updateMouseRectangle(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mouseLocation = null;
                repaint();
            }
        };
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    private Rectangle getRectangle() {
        if(mouseLocation != null) {
            return new Rectangle(mouseLocation.x - 5, mouseLocation.y - 5, 10, 10);
        }
        else {
            return null;
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle rectangle = getRectangle();
        if(rectangle != null) {
            Graphics2D gg = (Graphics2D) g;
            gg.setColor(Color.BLUE);
            gg.fill(rectangle);
            gg.setColor(Color.BLACK);
            gg.draw(rectangle);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(new PaintRectangle());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

另见http://docs.oracle.com/javase/tutorial/uiswing/painting/

这篇关于画笔式 GUI 中的paintComponent() 与paint() 以及JPanel 与Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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