如何在打开的菜单下绘制图像

时间:2012-03-16 16:47:38

标签: java swing graphics bufferedimage double-buffering

我正在为JPanel中的BufferedImage实现我自己的双缓冲,这样我就可以在BufferedImage中显示鼠标位置,而无需在mousemovement上重新将每个对象重新渲染到它上面。当父JFrame中的JMenu打开时,BufferedImage将重新绘制在JMenu之上。

这个类不完整,只有必要的方法,

    public class Foo extends JPanel implements ComponentListener {
        BufferedImage bufferedImage;
        long mousePosX;
        long mousePoxY;

        protected void paintComponent(Graphics g) {
            paintComponent(g, this.xform);
        }
        protected void paintComponent(Graphics graphics, XFormPixel xformIn) {
            bufferedImage = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
            Graphics g = bufferedImage.getGraphics();
            super.paintComponent(g);
            //Draw lots of stuff to graphics

            if(drawMouseLocation) {
                int width = this.getWidth();
                int height = this.getHeight();
                Color origColor = g.getColor();
                g.setColor(textColor);
                if (cursorLocation != null) {
                    g.drawString("X: "+mousePosX + " Y: " + mousePosY);
                }
            }

            g.setColor(origColor);
            graphics.drawImage(bufferedImage,0,0,null);
        }

        public void drawMouseLocation() {   

            int width = this.getWidth();
            int height = this.getHeight();
            Image image = bufferedImage;
            Graphics graphics = this.getGraphics();
            Graphics g = image.getGraphics();
            Color origColor = g.getColor();
            g.setColor(textColor);
            if (cursorLocation != null) {
                g.drawString("X: "+mousePosX + " Y: " + mousePosY);
            }
            g.setColor(origColor);
            graphics.drawImage(image,0,0,this);
        }
    }

还有其他办法吗?

另一个可能的交易破坏者是,当初始化Foo JPanel时,它有一个黑色边框,但是当绘制图像以显示鼠标定位时,边框会消失。我假设调用一个repaint()或父母的东西将修复这两个问题,但它也会调用孩子的重绘,我试图避免。

编辑1: 这是请求的可运行代码。在创建它时,我无法使双缓冲工作正常,所以当鼠标移动时我也遇到了鼠标位置闪烁的问题。

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;

    public class DrawingTestFrame extends JFrame {
        private static final long serialVersionUID = 1L;        

        public static void main(String[] args) {
                new DrawingTestFrame();
        }   
        public DrawingTestFrame() {
                init();
        }

        public void init() {
                JMenuBar menuBar = new JMenuBar();                  
                    setJMenuBar(menuBar);
                JMenu dropMenu = new JMenu("Drop This");
                dropMenu.add(needs);
                dropMenu.add(to);
                dropMenu.add(overlap);
                menuBar.add(dropMenu);          
                    DrawingTest test = new DrawingTest();
                setTitle("Drawing Test");
                add(test);
                setMinimumSize(new Dimension(550,270));
                pack();
                setVisible(true);
        }

            public static Action needs = new AbstractAction("Needs") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
            public static Action to = new AbstractAction("To") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
            public static Action overlap = new AbstractAction("Overlap") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
    }


    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;

    public class DrawingTest extends JPanel implements MouseListener  {
        private static final long serialVersionUID = 1L;
            public Component parent;
            private Point mouseLocation;
            private BufferedImage bufferedImage;
            public DrawingTest() {
                    init();
            }        
            public void init() {
                    this.setPreferredSize(new Dimension(100, 100));
                    this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    this.addMouseListener(this);
                    this.addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                    mouseLocation = MouseInfo.getPointerInfo().getLocation();
                    DrawingTest.this.repaint();
                }

                public void mouseMoved(MouseEvent e) {
                    mouseLocation = MouseInfo.getPointerInfo().getLocation();
                    DrawingTest.this.drawLocation();
                }
                    });
                    this.setVisible(true);
            }
        public void mouseClicked(MouseEvent e) {
            mouseLocation = MouseInfo.getPointerInfo().getLocation();
            this.repaint();
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

            protected void paintComponent(Graphics graphics) {
                    bufferedImage = new   
                            BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = bufferedImage.createGraphics();
                    super.paintComponent(g);
                    g.setColor(Color.red);
                    g.drawRect(10,10,110,110);
                    graphics.drawImage(bufferedImage,0,0,null);
                    if (mouseLocation != null) {
                        graphics.drawString("X: " + mouseLocation.getX() + 
                "  Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
                    }
            }
            protected void drawLocation() {
                    this.getGraphics().drawImage(bufferedImage, 0,0,null);
                    this.getGraphics().setColor(Color.green);
                    if (mouseLocation != null) {
                        this.getGraphics().drawString("X: " + mouseLocation.getX() + 
                "  Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
                    }
            }
    }

谢谢!

2 个答案:

答案 0 :(得分:1)

问题是你通过在组件上调用getGraphics()来获取Graphics对象,这是你不应该这样做的。你为什么要做这个自制的双缓冲,你在哪里读到这是实现它的方法?我想看看那个教程。如果这是我的申请,我要么:

  • 对我的背景图像使用BufferedImage,只需在paintComponent中绘制它,或许将我的绘画限制在感兴趣的矩形中......
  • 或使用JLabel显示我的鼠标位置。

例如,此代码显示了两种技术:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class DrawingTestFrame2 {
   private static void createAndShowGui() {
      JFrame frame = new JFrame("DrawingTestFrame2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      DrawingTest2 drawingTest = new DrawingTest2();

      frame.getContentPane().add(drawingTest);
      frame.setJMenuBar(drawingTest.createMenuBar());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

class DrawingTest2 extends JPanel {

   private static final int PREF_W = 500;
   private static final int PREF_H = 300;
   private static final int RECT_W = 100;
   private static final int RECT_H = 20;
   public static final Rectangle BOTTOM_RECT = new Rectangle(PREF_W/2 - RECT_W/2, PREF_H - RECT_H, 
         RECT_W, RECT_H);
   private String[] menuItemStrings = {"One", "Two", "Three"};
   public Point mouseLocation;
   private BufferedImage bufferedImage = new BufferedImage(PREF_W, PREF_H, 
         BufferedImage.TYPE_INT_ARGB);
   private JLabel mousePositionLabel = new JLabel("", SwingConstants.RIGHT);

   public DrawingTest2() {
      MouseAdapter mouseAdapter = new MyMouseAdapter();
      addMouseListener(mouseAdapter);
      addMouseMotionListener(mouseAdapter);

      mousePositionLabel.setForeground(Color.gray);
      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 1;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.anchor = GridBagConstraints.SOUTHEAST;
      add(mousePositionLabel, gbc);

      Graphics2D g2 = bufferedImage.createGraphics();
      g2.setPaint(new GradientPaint(0, 0, Color.yellow, 40, 40, Color.green, true));
      g2.fillRect(0, 0, PREF_W, PREF_H);
      g2.dispose();
      g2 = bufferedImage.createGraphics();
      g2.setColor(Color.red);
      g2.setStroke(new BasicStroke(3f));
      g2.drawRect(10, 10, 110, 110);
      g2.dispose();
   }

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

   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (bufferedImage != null) {
         g.drawImage(bufferedImage, 0, 0, this);
      }
      if (mouseLocation != null) {
         g.drawString("X: " + mouseLocation.getX() + "  Y: "
               + mouseLocation.getY(), this.getWidth() / 2 - 50,
               this.getHeight() - 10);
      }
   }

   public JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Menu");
      for (int i = 0; i < menuItemStrings .length; i++) {
         menu.add(new JMenuItem(menuItemStrings[i]));
      }
      menuBar.add(menu );
      return menuBar;
   }

   class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mouseMoved(MouseEvent mEvt) {
         mouseLocation = mEvt.getLocationOnScreen();
         repaint(BOTTOM_RECT);

         String mousePosStr = String.format("x:%d y:%d", mouseLocation.x, mouseLocation.y);
         mousePositionLabel.setText(mousePosStr);
      }
   }
}

答案 1 :(得分:0)

有一个名为DisplayJAI的类。 这可以显示PlanarImage(您可以使用PlanarImage来包装BufferedImage)。 它扩展了JPanel,并且已经为您提供了所需的所有鼠标监听器。 你不需要覆盖油漆或做任何事情。