Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
93 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...