拖动鼠标时在Jpanel上绘制线条 [英] Draw the line on the Jpanel when dragging the mouse

查看:186
本文介绍了拖动鼠标时在Jpanel上绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标拖动时,我想在JPanel上绘制2条(或更多条)线。当我在我的代码中使用 super.paintComponent(g)
时,我无法在面板上绘制2行,但是当我不使用<$时c $ c> super.paintComponent(g) ;,结果很难看,如下图所示:

I want to draw 2 (or more ) lines on JPanel when the mouse drags. When i use super.paintComponent(g) in my code, I couldn't draw 2 lines on the panel, however when I don't use super.paintComponent(g);, the result is ugly, like the pic below :

我理解为什么这些行的行为如此。

I understand why the lines behaved like that.

拖动鼠标时,如何在Jpanel上绘制线条?
BTW,由 g2d.draw(line2d)绘制的线有时它不是平滑的线(下图)

How could I draw the lines on the Jpanel when dragging the mouse? BTW, the line drawn by g2d.draw(line2d) sometimes it's not the smooth line (pic below)

我的到目前为止的代码:

My codes so far :

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


public class LineDrawing extends JPanel implements MouseMotionListener, MouseListener{
    Point point1;
    Point point2;
    Line2D line2d;

  public LineDrawing(){
       super();
       addMouseListener(this);
       addMouseMotionListener(this);
    }

 @Override
  public void paintComponent(Graphics g){

    //super.paintComponent(g);

       Graphics2D g2d = (Graphics2D) g;
       if(point1!=null && point2!=null){

          g2d.setPaint(Color.RED);
          g2d.setStroke(new BasicStroke(1.5f));
          g2d.draw(line2d);

         }
      }   


  @Override
  public void mouseDragged(MouseEvent e) {

    point2 = e.getPoint();
    line2d = new Line2D.Double(point1, point2); 
    repaint();

  }

   @Override
   public void mouseMoved(MouseEvent e) {

   }

   @Override
   public void mouseClicked(MouseEvent e) {

  }

   @Override
   public void mousePressed(MouseEvent e) {
     point1 = e.getPoint();

   }

  @Override
  public void mouseReleased(MouseEvent e) {

  }

 @Override
 public void mouseEntered(MouseEvent e) {

 }

 @Override
 public void mouseExited(MouseEvent e) {

}


public static void main(String a[]){
   EventQueue.invokeLater(new Runnable(){
        @Override
        public void run() {

         JFrame frame = new JFrame();
         LineDrawing linedraw= new LineDrawing();
         frame.add(linedraw);
         frame.setSize(500,500);
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         frame.setVisible(true);   

                         }
                });
   }   

}

推荐答案


..画2行

..draw 2 lines

这似乎是难题这个问题的问题。

That seems like the crux of the matter in this question.

点击/拖动时,在可扩展列表中保留一组行(例如 ArrayList ),添加一个新行到列表并调用 repaint()。在 paintComponent(Graphics)中,迭代集合并绘制每一行。

Keep a collection of lines in an expandable list (e.g. ArrayList) when clicking/dragging, add a new line to the list and call repaint(). In paintComponent(Graphics), iterate the collection and draw each line.

BTW - 我猜你还没有在测试时最小化并恢复窗口。你的线条(漂亮或丑陋)会消失!

BTW - I am guessing you have not minimized and restored your window while testing this. Your lines (beautiful or ugly) would disappear!


..它们消失了。是什么原因?

..they disappeared. What's the reason?

方法 paint()只要GUI需要重绘,就会调用paintComponent()。在应用程序前面出现另一个窗口后,可能会调用它们,然后将它带回到前面。另一个时间是从最小化恢复后。

The methods paint() and paintComponent() are called whenever the GUI needs to redraw. They might be invoked after another window appears in front of the app., then it is brought back to the front. Another time is after being restored from minimized.

保留行的选项包括:


  • 存储的位置每当被问到时(如上所述),所有这些行都会重新绘制。这可以用于大多数目的。即使有数百行,GUI也会在眨眼间重绘它们。

  • 将每一行绘制成 BufferedImage 并将图像放入( ImageIcon 中) JLabel 。如果绘图区域具有固定的尺寸和尺寸,则该方法很有效。什么都没有被删除,并且可以容纳数百万行,弧,半透明区域,较小的图像,文本。使用图像作为渲染表面,您将不再需要 ArrayList ,因为您所做的只是在图像上添加一个新行,然后重新绘制标签以查看新行以及之前的所有行。

  • Store the locations of the line(s) and redraw all of them whenever asked (as described above). This can work for most purposes. Even if there are hundreds of lines, the GUI will redraw them in 'the blink of an eye'.
  • Draw each line to a BufferedImage and put the image in (an ImageIcon in) a JLabel. This approach works well if the drawing area is of a fixed size & nothing is ever removed, and can accommodate ..millions of lines, arcs, semi-transparent areas, smaller images, text.. Using an image as a rendering surface, you would no longer need the ArrayList, since all you do is add a new line to the image, and repaint the label to see the new line and all previous lines.

..该行不是直线。

..the line is not the straight line.

这是因为绘制线条时使用了渲染提示。由对齐的像素行组成的屏幕只能完美地形成垂直或水平线。给出一个直的&的'幻觉'。在任何其他角度的连续线,需要一种称为抖动的技术。阅读 Graphics2D <的开头部分/ code> 有关 RenderingHints 的更多说明和说明。

That is because of the 'rendering hints' used when drawing the line. A screen made of aligned rows of pixels can only make vertical or horizontal lines perfectly. To give the 'illusion' of a straight & continuous line at any other angle, requires a technique known as dithering. Read the opening sections of Graphics2D for more explanation and description of the RenderingHints.

这篇关于拖动鼠标时在Jpanel上绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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