为什么我行不拉丝? [英] Why is my line not drawing?

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

问题描述

所以我定义了一个mouseEventlistener和的MouseMotionListener定义点作为左右。

 保护点el​​ementPosition = NULL;
      公共Point端点= NULL;
      公共轴tempAxis;
      公共图形克;
      类MouseButtonHandler扩展MouseAdapter
      {       公共无效鼠标pressed(的MouseEvent E)
       {
        如果(e.getModifiers()== InputEvent.BUTTON1_MASK)
        {         elementPosition =新点(e.getX(),e.​​getY());
   如果(将AddType == YLABEL)
   {
    YDialog ydia =新YDialog(anApp为);
    ydia.setVisible(真);    值=(双)ydia.getValue();
    ydia.dispose();
   }
        }
      }     公共无效的mouseReleased(的MouseEvent E)
    {
    }
    }   类MouseMoveHandler扩展MouseMotionAdapter
   {
   公共无效的mouseMoved(的MouseEvent E)
   {
    指向currentCursor = e.getPoint();
   }
   公共无效的mouseDragged(的MouseEvent E)
   {
    端点=新的点(e.getX(),e.​​getY());
    tempAxis =新中轴线(elementPosition,端点);
    tempAxis.draw(G);
   }  }

凡轴线类被定义为这样。

 进口java.awt中的*。
 java.awt.event中导入*。 公共类轴线延伸对象
 {
  公共点位;
  公共Point端点; 公共轴(点位置,Point端点)
 {
  this.position =位置;
  this.endPoint =端点;
 }公共无效抽奖(图形G)
{
 g.setColor(Color.red);
 g.drawLine(position.x,position.y,endPoint.x,endPoint.y);
}

}

这些都在一个视图类来实现。即弹出,用于显示菜单的一切都只是按计划进行,但不画当轴的mouseDragged。具体来说它说,在有问题
tempAxis.draw(G);.没有人有任何想法,为什么发生这个错误。我仍然对Java的方式。


解决方案

  

为什么我行不拉丝?


由于这不是如何风俗画作品。

有至少两个主要问题。第一个是,你正在创建一个新的每个拖动事件,这种不必要的和低效的。

您应该创建一个新的小鼠pressed ,传递的起点和更新该实例在中的mouseDragged 事件。如果你需要保持previously画线,你需要把这些东西加到某种形式的列表,使他们能够重新粉刷(记住,画的是破坏性)。

第二个问题是,绘画的组件油漆方法的上下文中执行的事实。假设你正在使用AWT,你应该有一些从组件画布扩展自定义类的很受欢迎

您会覆盖油漆组件的方法并执行你在这里画。这就是为什么你需要的列表

例如...

 进口java.awt.BorderLayout中;
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.EventQueue中;
进口java.awt.Frame中;
进口java.awt.Graphics;
进口java.awt.Point中;
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.awt.event.WindowAdapter中;
进口java.awt.event.WindowEvent中;
进口java.awt.event.WindowListener中;
进口的java.util.ArrayList;
进口的java.util.List;
进口javax.swing.JFrame中;
进口javax.swing.UIManager中;
进口javax.swing.UnsupportedLookAndFeelException;公共类的DrawLine {    公共静态无效的主要(字串[] args){
        新的DrawLine();
    }    公众的DrawLine(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的| InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException前){
                }                帧一帧=新的帧(测试);
                frame.addWindowListener(新WindowAdapter的(){
                    @覆盖
                    公共无效的windowClosing(WindowEvent五){
                        System.exit(0);
                    }
                });
                frame.setLayout(新的BorderLayout());
                frame.add(新TestPane());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }    公共类TestPane扩展画布{        私人列表<轴>线;        公共TestPane(){
            行=新的ArrayList<>(25);            MouseAdapter处理程序=新MouseAdapter(){                私人轴电流;                @覆盖
                公共无效鼠标pressed(的MouseEvent E){
                    的System.out.println(Clicled);
                    电流=新轴(e.getPoint());
                    lines.add(电流);
                }                @覆盖
                公共无效的mouseDragged(的MouseEvent E){
                    的System.out.println(拖);
                    如果(电流!= NULL){
                        current.setEndPoint(e.getPoint());
                        重绘();
                    }
                }                @覆盖
                公共无效的mouseReleased(的MouseEvent E){
                    电流=无效;
                }            };            addMouseListener将(处理器);
            addMouseMotionListener(处理);
        }        @覆盖
        公共尺寸的get preferredSize(){
            返回新尺寸(200,200);
        }        @覆盖
        公共无效漆(图形G){
            super.paint(G);
            对于(轴轴:线){
                的System.out.println(行);
                axis.draw(G);
            }
        }    }    公共类轴线延伸的对象{        公共点位;
        公共Point端点;        公共轴(点位置){
            this.position =位置;
            this.endPoint =位置;
        }        公共无效化setEndpoint(Point端点){
            this.endPoint =端点;
        }        公共无效抽奖(图形G){
            g.setColor(Color.red);
            g.drawLine(position.x,position.y,endPoint.x,endPoint.y);
        }
    }}

看看在AWT 绘画和Swing 了解更多详情关于绘画过程。

现在,除非你有一个很好的理由这样做,我会鼓励你使用Swing API在AWT库,这是与Swing大约15年前所取代。有更多可用的人谁了解的Swing的工作,那谁记得(或有经验)AWT。为此,你应该有一个看看表演风俗画

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);  
   }

  }

Where the the axis class is defined as so.

 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);
}

}

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.

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

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).

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.

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

For example...

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);
        }
    }

}

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

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天全站免登陆