在面板内绘制图像 [英] Draw in an image inside panel

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

问题描述

我有一个带两个按钮的面板。我正在尝试在面板中插入图像,我想在单击按钮后在图像内部绘制线条。我使用了以下代码,但这似乎不起作用。

I have a panel with two buttons. I'm trying to insert an image inside the panel and I want to draw lines inside the image after clicking on a button. I have used the below code but this doesn't seem to work.

public class Try_Panel extends JFrame {
  // start attributes
  private JPanel jPanel1 = new JPanel(null, true);
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  // end attributes

  public Try_Panel(String title) {
    // Frame-Init
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 300; 
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // start components

    jPanel1.setBounds(48, 24, 209, 145);
    jPanel1.setOpaque(false);
    cp.add(jPanel1);
    jButton1.setBounds(88, 208, 75, 25);
    jButton1.setText("jButton1");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton1_ActionPerformed(evt);
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(184, 208, 75, 25);
    jButton2.setText("jButton2");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton2_ActionPerformed(evt);
      }
    });
    cp.add(jButton2);
    // end components

    setVisible(true);
  } // end of public Try_Panel

  // start methods
  public void jButton1_ActionPerformed(ActionEvent evt) {
      BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        JLabel l=new JLabel(new ImageIcon(image));
        Graphics graphics = image.getGraphics();
        Graphics2D g = (Graphics2D) graphics;
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(Color.BLUE);
        g.drawLine(0, 0, 300, 400);
        jPanel1.add(l);
  } // end of jButton1_ActionPerformed

  public void jButton2_ActionPerformed(ActionEvent evt) {
    // TODO add your code here
  } // end of jButton2_ActionPerformed

  // end methods

  public static void main(String[] args) {
    new Try_Panel("Try_Panel");
  } // end of main

} // end of class Try_Panel

最大的问题是我的其他课程中使用的代码相同。

The biggest problem is the same code worked in my other class.

推荐答案

尝试在中包装图像 ImageIcon 更新后。此外,当您完成渲染到图形上下文时,还应该调用 Graphics#dispose

Try wrapping the image inside the ImageIcon AFTER you have updated it. Also, you should also call Graphics#dispose when you are finished rendering to the graphics context.

public void jButton1_ActionPerformed(ActionEvent evt) {
    BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g = image.createGraphics();
    g.fillRect(0, 0, image.getWidth(), image.getHeight());
    g.setColor(Color.BLUE);
    g.drawLine(0, 0, 300, 400);
    g.dispose();
    JLabel l=new JLabel(new ImageIcon(image));
    jPanel1.add(l);
}

您还应该依赖布局管理员,而不是自己动手,它只会让你的生活变得更轻松。

You should also rely on the layout managers rather the trying to do it yourself, it will simply make your life easier.

就个人而言,我认为直接绘制到自定义组件更容易,例如 JPanel 。查看执行自定义绘画了解更多详情

Personally, I think it would easier to paint directly to a custom component, like JPanel. Check out Performing Custom Painting for more details

更新示例

基本上,我将您的示例更改为

Basically, I changed your example to


  1. 使用布局管理器

  2. 在EDT上下文中加载UI

  3. revalidate jPanel1

  1. Use layout managers
  2. Load the UI within the context of the EDT
  3. revalidate the jPanel1

public class BadLabel extends JFrame {
    // start attributes

    private JPanel jPanel1 = new JPanel(new BorderLayout(), true);
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    // end attributes

    public BadLabel(String title) {
        // Frame-Init
        super(title);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = 300;
        int frameHeight = 300;
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
//        setResizable(false);
        Container cp = getContentPane();
//        cp.setLayout(null);
        // start components

//        jPanel1.setBounds(48, 24, 209, 145);
        jPanel1.setOpaque(true);
        jPanel1.setBackground(Color.RED);
        cp.add(jPanel1);

        JPanel buttons = new JPanel();
//        jButton1.setBounds(88, 208, 75, 25);
        jButton1.setText("jButton1");
        jButton1.setMargin(new Insets(2, 2, 2, 2));
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton1_ActionPerformed(evt);
            }
        });
        buttons.add(jButton1);
//        jButton2.setBounds(184, 208, 75, 25);
        jButton2.setText("jButton2");
        jButton2.setMargin(new Insets(2, 2, 2, 2));
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton2_ActionPerformed(evt);
            }
        });
        buttons.add(jButton2);
        // end components

        cp.add(buttons, BorderLayout.SOUTH);

        setVisible(true);
    } // end of public BadLabel

    // start methods
    public void jButton1_ActionPerformed(ActionEvent evt) {
        BufferedImage image = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = image.createGraphics();
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(Color.BLUE);
        g.drawLine(0, 0, 300, 400);
        g.dispose();
        JLabel l = new JLabel(new ImageIcon(image));
        l.setBorder(new LineBorder(Color.BLUE));
        jPanel1.add(l);
        jPanel1.revalidate();
    } // end of jButton1_ActionPerformed

    public void jButton2_ActionPerformed(ActionEvent evt) {
        // TODO add your code here
    } // end of jButton2_ActionPerformed

    // end methods
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                }
                new BadLabel("BadLabel");
            }
        });
    } // end of main
} // end of class BadLabel}

这篇关于在面板内绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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