为什么我的线不画? [英] Why is my line not drawing?

查看:30
本文介绍了为什么我的线不画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我定义了一个 mouseEventlistener 和 mousemotionListener 来定义点.

So I have defined a mouseEventlistener and mousemotionListener to define points as so.

      protected Point elementPosition = null;
      public Point endPoint = null;
      public Axis tempAxis;
      public Graphics g;


      class MouseButtonHandler extends MouseAdapter
      {

       public void mousePressed(MouseEvent e)
       {
        if(e.getModifiers()==InputEvent.BUTTON1_MASK)
        {

         elementPosition =new Point(e.getX(), e.getY()) ;
   if(addType==YLABEL)
   {
    YDialog ydia = new YDialog(anApp);
    ydia.setVisible(true);

    value =(double) ydia.getValue();
    ydia.dispose();
   }


        }
      }

     public void mouseReleased(MouseEvent e)
    {
    }
    }

   class MouseMoveHandler extends MouseMotionAdapter
   {
   public void MouseMoved(MouseEvent e)
   {
    Point currentCursor = e.getPoint();
   }


   public void mouseDragged(MouseEvent e)
   {
    endPoint = new Point(e.getX(), e.getY());
    tempAxis = new Axis(elementPosition, endPoint);
    tempAxis.draw(g);  
   }

  }

轴类的定义是这样的.

 import java.awt.*;
 import java.awt.event.*;

 public class Axis extends Object
 {
  public Point position;
  public Point endPoint;

 public Axis(Point position, Point endPoint)
 {
  this.position = position;
  this.endPoint = endPoint;
 }

public void draw(Graphics g)
{
 g.setColor(Color.red);
 g.drawLine(position.x, position.y, endPoint.x, endPoint.y);
}

}

这些都是在一个视图类中实现的.弹出,按计划显示菜单和所有内容,但在鼠标拖动时不绘制轴.具体来说,它说有问题tempAxis.draw(g);.有谁知道为什么会发生这个错误.顺便说一下,我还是 Java 新手.

These are both implemented in a view class. Which pops up, shows menus an everything just as planned but does not draw an axis when mouseDragged. Specifically it says there is problem at the tempAxis.draw(g);. Does anyone have any idea why this error occurred. I am still new to Java by the way.

推荐答案

为什么我的线没有画出来?

Why is my line not drawing?

因为这不是自定义绘画的工作方式.

Because this is not how custom painting works.

至少有两个主要问题.第一个是,您在每个拖动事件上创建一个新的 Axis,这是不必要且低效的.

There are at least two main problems. The first is, you are creating a new Axis on each drag event, this unnecessary and inefficient.

您应该在 mousePressed 上创建一个新的 Axis,传递起点并在 mouseDragged 事件中更新此实例.如果您需要保留以前绘制的线条,则需要将它们添加到某种类型的 List 中,以便可以重新绘制它们(请记住,绘制是具有破坏性的).

You should create a new Axis on mousePressed, passing the start points and update this instance within the mouseDragged event. If you need to maintain previously draw lines, you will need to add these to a List of some kind so they can be re-painted (remember, painting is destructive).

第二个问题是绘制是在组件paint 方法的上下文中执行的.假设您正在使用 AWT,您应该有某种从 Component 扩展而来的自定义类,Canvas 非常流行.

The second issue is the fact that painting is performed within the context of the components paint method. Assuming that you are using AWT, you should have some kind of custom class that extends from Component, Canvas is very popular.

您将覆盖此组件的 paint 方法并在此处执行绘画.这就是为什么你需要 List

You would override the paint method of this component and perform you painting here. This is why you need the List

例如...

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawLine {

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

    public DrawLine() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Frame frame = new Frame("Testing");
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends Canvas {

        private List<Axis> lines;

        public TestPane() {
            lines = new ArrayList<>(25);

            MouseAdapter handler = new MouseAdapter() {

                private Axis current;

                @Override
                public void mousePressed(MouseEvent e) {
                    System.out.println("Clicled");
                    current = new Axis(e.getPoint());
                    lines.add(current);
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    System.out.println("Dragged");
                    if (current != null) {
                        current.setEndPoint(e.getPoint());
                        repaint();
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    current = null;
                }

            };

            addMouseListener(handler);
            addMouseMotionListener(handler);
        }

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

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            for (Axis axis : lines) {
                System.out.println("line");
                axis.draw(g);
            }
        }

    }

    public class Axis extends Object {

        public Point position;
        public Point endPoint;

        public Axis(Point position) {
            this.position = position;
            this.endPoint = position;
        }

        public void setEndPoint(Point endPoint) {
            this.endPoint = endPoint;
        }

        public void draw(Graphics g) {
            g.setColor(Color.red);
            g.drawLine(position.x, position.y, endPoint.x, endPoint.y);
        }
    }

}

查看在 AWT 和 Swing 中绘制 以了解更多详细信息关于绘画过程.

Take a look at Painting in AWT and Swing for more details about the painting process.

现在,除非您有充分的理由这样做,否则我鼓励您使用 Swing API 而不是 AWT 库,该库在 15 年前被 Swing 取代.有更多的人了解 Swing 的工作原理,然后记住(或有经验)AWT.为此,您应该首先查看执行自定义绘画

Now, unless you have a really good reason for doing so, I would encourage you to use the Swing API over the AWT library, which was replaced with Swing some 15 years ago. There are more people available who understand how Swing works then who remember (or have experience with) AWT. To that end, you should start by having a look at Performing Custom Painting

这篇关于为什么我的线不画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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