【问题标题】:Image drawing on a Java component在 Java 组件上绘制图像
【发布时间】:2016-05-07 22:13:35
【问题描述】:

我正在尝试创建一个绘制图形的简单应用程序...

package Tests.Drawing;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;

public class DrawingTest extends JFrame
{
    private Canvas drwArea;
    private JButton btnClear;

    public static void main(String[] args)
    {
        DrawingTest StartForm = new DrawingTest();
        StartForm.setVisible(true);
    }


    public DrawingTest()
    {
        //Window...
        this.setTitle("Drawing objects test00");
        this.setBounds(0,0,510,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        //Drawing area...
        drwArea = new Canvas();
        drwArea.setBounds(0, 0, 400, 450);      
        drwArea.setBackground(Color.WHITE);
        drwArea.setOpaque(true);
        drwArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        drwArea.addMouseMotionListener(new MouseMotionAdapter()
        {
            @Override
            public void mouseDragged(MouseEvent e)
            {
                //Write code to paint on the image...
            }
        });

        this.getContentPane().add(drwArea);
        //Clear button...
        btnClear = new JButton("Clear");
        btnClear.setBounds(410,50,70,30);
        btnClear.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //Write code to clear the image...
            }
        });

        this.getContentPane().add(btnClear);

    }

    private class Canvas extends JLabel
    {
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            //The idea of overriding this method is
            //achieving persistence...
        }
    }

}

我已经看到要绘制的典型组件是 Jlabel(顺便问一下,有没有更好的组件?)。使用“getGraphics”方法,我可以使用具有多种方法的对象在组件上进行绘制。我的问题是,我不想直接在 JLabel 上绘画,而是想在图像上绘画(在内存中),一旦绘画完成,将结果发送到 JLabel。我怎样才能做到这一点?我有点迷茫……

提前致谢。

【问题讨论】:

  • 如果您对我的回答有任何疑问,请追问!
  • I have seen that the typical component to draw on is a Jlabel - 您不会在 JLabel 上“绘制”(也就是说,您不会覆盖 paintComponent() 来进行绘制)。您可以使用 JLabel 来显示 ImageIcon。如果你想在他们身上画画,你通常会在 JPanelBufferedImage 上画画。有关这两种方法的工作示例,请参阅 Custom Painting Approaches

标签: java image swing jlabel


【解决方案1】:

我想在图像上绘画(在内存中)

我建议您然后创建一个所需大小的 BufferedImage 对象,通过在其上调用 createGraphics() 获取其 Graphics2D 对象,并使用此 Graphics2D 对象在其上绘图。

绘制完成后,将结果发送到 JLabel

然后通过简单的调用从上面的 BufferedImage 中创建一个 ImageIcon

Icon myIcon = new ImageIcon(myBufferedImage);

然后通过myLabel.setIcon(myIcon);设置JLabel的图标

您还可以绘制到在 JPanel 的 paintComponent 方法中显示的 BufferedImage,如果您想在程序运行时更新图像,这可能是一种更好的方法。有关这方面的更多信息,请查看these examples 的一些内容。

其他cmets:

  • 不要使用通过在组件上调用 getGraphics() 获得的 Graphics 对象进行绘制。这将返回一个短暂的 Graphics 对象,冒着消失的图形或更糟的风险,一个 NullPointerException。相反,直接或间接通过在 BufferedImage 上绘制(是的,您可以通过 getGraphics() 获取其 Graphics 对象)在 JPanel 的 paintComponent(...) 方法中绘制,然后在 paintComponent 方法中将 BufferedImage 绘制到 GUI。
  • 虽然空布局和setBounds() 在 Swing 新手看来是创建复杂 GUI 的最简单和最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件大小,它们是增强或维护的皇家女巫,放置在滚动窗格中时它们完全失败,在与原始不同的所有平台或屏幕分辨率上查看时它们看起来很糟糕.

【讨论】:

  • 谢谢!它完美无缺。正是我想要的。遵循您对布局的建议。
猜你喜欢
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2013-01-13
  • 2013-10-29
  • 2021-12-26
  • 2011-10-26
相关资源
最近更新 更多