的paintComponent()与涂料()和JPanel的VS画布画笔型GUI [英] paintComponent() vs paint() and JPanel vs Canvas in a paintbrush-type GUI

查看:156
本文介绍了的paintComponent()与涂料()和JPanel的VS画布画笔型GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从<一一些有趣的想法和批评href=\"http://stackoverflow.com/questions/12107163/specifying-the-location-of-canvas-in-swing\">this, <一href=\"http://stackoverflow.com/questions/12158538/clear-foreground-of-a-drawing-panel-in-swing\">this和员额(见最后发表的帖子有问题的GUI的code )。尽管如此,我还是挺困惑的一些事情。主要是,什么是显示用户介绍显卡的最便宜的方式。

更具体地说,我通过在这个类的一个对象使用从的JP​​anel 的paintComponent()方法与的paintComponent的的mouseDragged()方法一起(的getGraphics())办法( AuxClass2 AuxClass1 相应地)。

显然,使用的getGraphics()的paintComponent()而不是重绘() 是坏的想法,我怀疑是与内存使用。还呼吁用户拖动鼠标也是一个坏主意的 AuxClass2 每次。

另外的JPanel VS帆布(即摆动VS AWT)是有点混乱。用什么时候?

我一直试图找到一个解决方法,但还没有找到一个,尤其是对的getGraphics()方法:还能怎么图形添加到面板?


解决方案

  

我一直试图找到一个解决方法,但还没有找到一个,
  特别是对的getGraphics()方法:还能怎么显卡是
  添加到面板?


您还记得需要被描绘成一个变量是什么,并使用在的paintComponent()。
例如,你好像什么是想在您的其他问题做如下所示:

 进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类PaintRectangle继承JPanel {    私人点mouseLocation;    公共PaintRectangle(){
        集preferredSize(新尺寸(500,500));        MouseAdapter监听器=新MouseAdapter(){
            @覆盖
            公共无效鼠标pressed(的MouseEvent E){
                updateMouseRectangle(E);
            }            私人无效updateMouseRectangle(的MouseEvent E){
                mouseLocation = e.getPoint();
                重绘();
            }            @覆盖
            公共无效的mouseDragged(的MouseEvent E){
                updateMouseRectangle(E);
            }            @覆盖
            公共无效的mouseReleased(的MouseEvent E){
                mouseLocation = NULL;
                重绘();
            }
        };
        addMouseListener将(监听);
        addMouseMotionListener(监听);
    }    私人矩形getRectangle(){
        如果(mouseLocation!= NULL){
            返回新的Rectangle(mouseLocation.x - 5,mouseLocation.y - 5,10,10);
        }
        其他{
            返回null;
        }
    }    @覆盖
    保护无效paintComponent(图形G){
        super.paintComponent方法(G);
        矩形矩形= getRectangle();
        如果(矩形!= NULL){
            GG的Graphics2D =(Graphics2D的)克;
            gg.setColor(Color.BLUE);
            gg.fill(矩形);
            gg.setColor(Color.BLACK);
            gg.draw(矩形);
        }
    }    公共静态无效的主要(字串[] args){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                JFrame的帧=新的JFrame(测试);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                。frame.getContentPane()增加(新PaintRectangle());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }
}

又见<一个href=\"http://docs.oracle.com/javase/tutorial/uiswing/painting/\">http://docs.oracle.com/javase/tutorial/uiswing/painting/

I got some interesting ideas and criticism from this, this and this posts (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.

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).

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.

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

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?

解决方案

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?

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

See also http://docs.oracle.com/javase/tutorial/uiswing/painting/

这篇关于的paintComponent()与涂料()和JPanel的VS画布画笔型GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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