在现有图形页面上绘制一个矩形 [英] Drawing a rectangle over an existing Graphics page

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

问题描述

我有一个绘制绘图的Java应用程序。我想让用户可以用鼠标标记区域(例如,为了放大它)。
为此,我使用MouseMotionListener类,当鼠标被(单击然后)移动时,我保存当前所选的位置(由于用户没有释放鼠标而不是最终的)矩形,并使用 repaint()函数。我希望在原始图形上显示该矩形,使其类似于MSPaint中的选择工具。

I have a Java application which draws a drawing. I want to give the user the possibility to mark an area with the mouse (in order to, for example, zoom into it). For that I use the MouseMotionListener class, and when the mouse is (clicked and then) moved, I save the location of the currently selected (it isn't final since the user haven't released the mouse) rectangle, and use the repaint() function. I wish to display that rectangle over the original drawing, making it similar to the Selection tool in MSPaint.

问题是当我调用时repaint()函数,调用方法 paintComponent(图形页面),其中我使用方法 super.paintComponent (页)删除了我的绘图。但是,如果当我知道用户正在选择一个矩形时我不使用该方法,我会得到所有选定的矩形一个在另一个上面打包,这是一个不理想的结果 - 我希望显示当前选中的只有矩形。

The problem is that when I call the repaint() function, the method paintComponent (Graphics page) is invoked, in which I use the method super.paintComponent(page) which erases my drawing. However, if I don't use that method when I know the user is selecting a rectangle, I get that all the selected rectangles are "packed" one above the other, and this is an undesirable result - I wish to display the currently selected rectangle only.

我以为我应该能够保存图形图形页面的副本,并在每次用户移动鼠标时以某种方式恢复它,但我可以找不到任何有用方法的文档。

I thought I should be able to save a copy of the Graphics page of the drawing and somehow restore it every time the user moves the mouse, but I could not find any documentation for helpful methods.

非常感谢,

Ron。

编辑:以下是我的代码的相关部分:

Here are the relevant pieces of my code:

public class DrawingPanel extends JPanel
{
public FractalPanel()
   {
      addMouseListener (new MyListener());
      addMouseMotionListener (new MyListener());

      setBackground (Color.black);
      setPreferredSize (new Dimension(200,200));
      setFocusable(true);
   }

public void paintComponent (Graphics page)
   {
        super.paintComponent(page);
        //that's where the drawing takes place: page.setColor(Color.red), page.drawOval(..) etc
   }
   private class MyListener implements MouseListener, MouseMotionListener
   {
   ...
      public void mouseDragged (MouseEvent event) 
      {
          //saving the location of the rectangle
          isHoldingRectangle = true;
          repaint();
       }
   }
}


推荐答案

我打赌您通过对组件的 getGraphics()调用来获取Graphics对象,并且由于获得的Graphics对象不满意而不满意坚持。出于这个原因,你不应该这样做,而只是在JPanel的paintComponent中进行绘图。如果你这样做,一切都会很高兴。

I'm betting that you are getting your Graphics object via a getGraphics() call on a component, and are disatisfied since this obtains a Graphics object which does not persist. It is for this reason that you shouldn't do this but instead just do your drawing inside of the JPanel's paintComponent. If you do this all will be happy.

顺便说一句 - 如果你告诉我们更多你问题的相关细节,我们将能够更好地帮助你例如,如何获取Graphics对象以及如何使用它绘制,这里的关键问题。否则我们只能对你想要做的事情进行猜测。

As an aside -- we'll be able to help you better if you tell us more of the pertinent details of your problem such as how you're getting your Graphics object and how you're trying to draw with it, key issues here. Otherwise we're limited to taking wild guesses about what you're trying to do.

例如,

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MandelDraw extends JPanel {
private static final String IMAGE_ADDR = "http://upload.wikimedia.org/" +
        "wikipedia/commons/thumb/b/b3/Mandel_zoom_07_satellite.jpg/" +
        "800px-Mandel_zoom_07_satellite.jpg";
private static final Color DRAWING_RECT_COLOR = new Color(200, 200, 255);
private static final Color DRAWN_RECT_COLOR = Color.blue;

   private BufferedImage image;
   private Rectangle rect = null;
   private boolean drawing = false;

   public MandelDraw() {
      try {
         image = ImageIO.read(new URL(IMAGE_ADDR));
         MyMouseAdapter mouseAdapter = new MyMouseAdapter();
         addMouseListener(mouseAdapter);
         addMouseMotionListener(mouseAdapter);
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (image != null) {
         return new Dimension(image.getWidth(), image.getHeight());
      }
      return super.getPreferredSize();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
      if (rect == null) {
         return;
      } else if (drawing) {
         g2.setColor(DRAWING_RECT_COLOR);
         g2.draw(rect);
      } else {
         g2.setColor(DRAWN_RECT_COLOR);
         g2.draw(rect);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      private Point mousePress = null; 
      @Override
      public void mousePressed(MouseEvent e) {
         mousePress = e.getPoint();
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         drawing = true;
         int x = Math.min(mousePress.x, e.getPoint().x);
         int y = Math.min(mousePress.y, e.getPoint().y);
         int width = Math.abs(mousePress.x - e.getPoint().x);
         int height = Math.abs(mousePress.y - e.getPoint().y);

         rect = new Rectangle(x, y, width, height);
         repaint();
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         drawing = false;
         repaint();
      }

   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("MandelDraw");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new MandelDraw());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

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

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