Java制定了一条有针对性的线路并使其发展 [英] Java make a directed line and make it move

查看:146
本文介绍了Java制定了一条有针对性的线路并使其发展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制定一条有针对性的路线并让它移动。我可以制作一条有针对性的线条并移动线条,但是当我移动线条时,箭头会移位。



这是我的绘画方法

  Line2D.Double line = new Line2D.Double(startX,startY,endX,endY); 
g2d.draw(line);
tx.setToIdentity();
double angle = Math.atan2(line.y2 - line.y1,line.x2 - line.x1);
tx.translate(line.x2,line.y2);
tx.rotate((angle - Math.PI / 2d));
Graphics2D gClone =(Graphics2D)g2d.create();
gClone.setTransform(tx);
Polygon arrowHead = new Polygon();
arrowHead.addPoint(0,15);
arrowHead.addPoint(-15,-15);
arrowHead.addPoint(15,-15);
面积面积=新阿里亚(arrowHead);
区域lineArea =新区域(line);
lineArea.subtract(area);
gClone.fill(area);
gClone.dispose();

我已经改变了startx的值,并在鼠标按下
和endx时开始y enny at mouse drag

解决方案

这是一个基本的例子,演示了如何使用 Path2D 来定义一个自定义形状和一个 AffineTransformation 来转换形状。



这个例子会导致在面板上移动时指向鼠标的箭头

  import java.awt.BasicStroke; 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateArrow {

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

$ b $ public RotateArrow(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}

JFrame框架= new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new RotatePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class RotatePane extends javax.swing.JPanel {

private Point mousePoint;
私人PointyThing pointyThing;

public RotatePane(){

pointyThing = new PointyThing();

addMouseMotionListener(new MouseAdapter(){
@Override
public void mouseMoved(MouseEvent e){
mousePoint = e.getPoint();
repaint ();
}
});


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


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D g2d =(Graphics2D)g.create();

double rotation = 0f;

int width = getWidth() - 1;
int height = getHeight() - 1;

if(mousePoint!= null){
int x = width / 2;
int y = height / 2;

int deltaX = mousePoint.x - x;
int deltaY = mousePoint.y - y;

rotation = -Math.atan2(deltaX,deltaY);
rotation = Math.toDegrees(rotation)+ 180;
}

Rectangle bounds = pointyThing.getBounds();

AffineTransform at = new AffineTransform();
at.translate((width - bounds.width)/ 2,(height - bounds.height)/ 2);
at.rotate(Math.toRadians(rotation),bounds.width / 2,bounds.height / 2);
Shape shape = new Path2D.Float(pointyThing,at);

g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.RED);

g2d.fill(shape);
g2d.draw(shape);
g2d.dispose();



public class PointyThing extends Path2D.Float {

public PointyThing(){
moveTo(15,0);
lineTo(30,15);
lineTo(0,15);
lineTo(15,0);
moveTo(15,15);
lineTo(15,60);
}

}
}

使用示例2进行更新



基本上,它只使用 PointyThing 作为箭头,

箭头指向所以它会指向线条(朝向鼠标)

  import java.awt.BasicStroke; 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateArrow {

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

$ b $ public RotateArrow(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}

JFrame框架= new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new RotatePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

});
}

public class RotatePane extends javax.swing.JPanel {

private PointyThing pointyThing;
私人点mouseStart;
private mouseEnd;

public RotatePane(){

pointyThing = new PointyThing();

MouseAdapter ma = new MouseAdapter(){
@Override
public void mouseMoved(MouseEvent e){
mouseEnd = e.getPoint();
repaint();
}

@Override
public void mouseClicked(MouseEvent e){
mouseStart = e.getPoint();
repaint();
}
};

addMouseListener(ma);
addMouseMotionListener(ma);


$ b @Override
public Dimension getPreferredSize(){

return new Dimension(200,200);


$ b @Override
protected void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2d =(Graphics2D)g.create();
if(mouseStart!= null&& mouseEnd!= null){

double rotation = 0f;

int width = getWidth() - 1;
int height = getHeight() - 1;

if(mouseEnd!= null){

int x = mouseStart.x;
int y = mouseStart.y;

int deltaX = mouseEnd.x - x;
int deltaY = mouseEnd.y - y;

rotation = -Math.atan2(deltaX,deltaY);
rotation = Math.toDegrees(rotation)+ 180;

}

矩形边界= pointyThing.getBounds();

g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Float(mouseStart,mouseEnd));

AffineTransform at = new AffineTransform();

at.translate(mouseEnd.x - (bounds.width / 2),mouseEnd.y - (bounds.height / 2));
at.rotate(Math.toRadians(rotation),bounds.width / 2,bounds.height / 2);
Shape shape = new Path2D.Float(pointyThing,at);

g2d.fill(shape);
g2d.draw(shape);
}
g2d.dispose();




$ b公共类PointyThing扩展Path2D.Float {

public PointyThing(){
moveTo(15,0);
lineTo(30,15);
lineTo(0,15);
lineTo(15,0);
}

}

}


I want to make a directed line and make it move. I am able to make a directed line and move the line but the arrow get displaced while i move the line

This is my paint method

 Line2D.Double line = new Line2D.Double(startX, startY, endX, endY);
                    g2d.draw(line);
                    tx.setToIdentity();                    
                  double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
                   tx.translate(line.x2, line.y2);
                    tx.rotate((angle - Math.PI / 2d));
                    Graphics2D gClone = (Graphics2D) g2d.create();
                    gClone.setTransform(tx);
 Polygon arrowHead = new Polygon();
        arrowHead.addPoint(0, 15);
        arrowHead.addPoint(-15, -15);
        arrowHead.addPoint(15, -15);
                    Area area = new Arear(arrowHead );
                    Area lineArea = new Area(line);
                    lineArea.subtract(area);
                    gClone.fill(area);
                    gClone.dispose();

and i have change the value of startx and start y at mouse pressedd and endx and enny at mouse drag

解决方案

This is a basic example that demonstrates the use of a Path2D, to define a custom shape and an AffineTransformation to transform the shape.

This example will cause the arrow to point towards the mouse as it moves over the panel

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateArrow {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new RotatePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RotatePane extends javax.swing.JPanel {

        private Point mousePoint;
        private PointyThing pointyThing;

        public RotatePane() {

            pointyThing = new PointyThing();

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mousePoint = e.getPoint();
                    repaint();
                }
            });

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            double rotation = 0f;

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            if (mousePoint != null) {
                int x = width / 2;
                int y = height / 2;

                int deltaX = mousePoint.x - x;
                int deltaY = mousePoint.y - y;

                rotation = -Math.atan2(deltaX, deltaY);
                rotation = Math.toDegrees(rotation) + 180;
            }

            Rectangle bounds = pointyThing.getBounds();

            AffineTransform at = new AffineTransform();
            at.translate((width - bounds.width) / 2, (height - bounds.height) / 2);
            at.rotate(Math.toRadians(rotation), bounds.width / 2, bounds.height / 2);
            Shape shape = new Path2D.Float(pointyThing, at);

            g2d.setStroke(new BasicStroke(3));
            g2d.setColor(Color.RED);

            g2d.fill(shape);
            g2d.draw(shape);
            g2d.dispose();
        }
    }

    public class PointyThing extends Path2D.Float {

        public PointyThing() {
            moveTo(15, 0);
            lineTo(30, 15);
            lineTo(0, 15);
            lineTo(15, 0);
            moveTo(15, 15);
            lineTo(15, 60);
        }

    }
}

Update with example two

Basically, this uses the PointyThing as the arrow head only, and draws the line separately.

The arrow is orientated so it will point down the line (towards the mouse)

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateArrow {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new RotatePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class RotatePane extends javax.swing.JPanel {

        private PointyThing pointyThing;
        private Point mouseStart;
        private Point mouseEnd;

        public RotatePane() {

            pointyThing = new PointyThing();

            MouseAdapter ma = new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mouseEnd = e.getPoint();
                    repaint();
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    mouseStart = e.getPoint();
                    repaint();
                }
            };

            addMouseListener(ma);
            addMouseMotionListener(ma);

        }

        @Override
        public Dimension getPreferredSize() {

            return new Dimension(200, 200);

        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            if (mouseStart != null && mouseEnd != null) {

                double rotation = 0f;

                int width = getWidth() - 1;
                int height = getHeight() - 1;

                if (mouseEnd != null) {

                    int x = mouseStart.x;
                    int y = mouseStart.y;

                    int deltaX = mouseEnd.x - x;
                    int deltaY = mouseEnd.y - y;

                    rotation = -Math.atan2(deltaX, deltaY);
                    rotation = Math.toDegrees(rotation) + 180;

                }

                Rectangle bounds = pointyThing.getBounds();

                g2d.setStroke(new BasicStroke(3));
                g2d.setColor(Color.RED);
                g2d.draw(new Line2D.Float(mouseStart, mouseEnd));

                AffineTransform at = new AffineTransform();

                at.translate(mouseEnd.x - (bounds.width / 2), mouseEnd.y - (bounds.height / 2));
                at.rotate(Math.toRadians(rotation), bounds.width / 2, bounds.height / 2);
                Shape shape = new Path2D.Float(pointyThing, at);

                g2d.fill(shape);
                g2d.draw(shape);
            }
            g2d.dispose();

        }

    }

    public class PointyThing extends Path2D.Float {

        public PointyThing() {
            moveTo(15, 0);
            lineTo(30, 15);
            lineTo(0, 15);
            lineTo(15, 0);
        }

    }

}

这篇关于Java制定了一条有针对性的线路并使其发展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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