Java图形使用fillRect绘制一条线

时间:2014-04-08 07:56:47

标签: java swing

我想绘制一条线,但我不能使用" drawline",只有" fillRect"。 当我通过mouseclick事件从用户那里读取一个点并放置一个矩形时,我有以下代码。

我需要做同样的事情,但是通过从A点到B点绘制矩形画一条线。

这是我的代码:

package graphics_draw_rec;

import java.awt.*;
import java.awt.Point;
import javax.swing.*;
import java.awt.event.*;

public class DrawRectangleOnMouseClick extends JPanel{

    MouseHandler mouseHandler = new MouseHandler();
    Point p2 = new Point(0, 0);
    boolean drawing;

   public DrawRectangleOnMouseClick(){
       this.setPreferredSize(new Dimension(500, 400));
       this.addMouseListener(mouseHandler);
   }

   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.fillRect(p2.x, p2.y, 5, 5);
   }

   private class MouseHandler extends MouseAdapter {

       public void mouseClicked(MouseEvent e) {
           drawing = true;
           p2 = e.getPoint();
           repaint();
       }

       public void mouseReleased(MouseEvent e) {
           drawing = false;
           p2 = e.getPoint();
           repaint();
       }
   }

   public static void main(String[] args) {


 JFrame f = new JFrame("Draw Rectangle On Mouse Click");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.add(new DrawRectangleOnMouseClick());
   f.pack();
   f.setVisible(true);
 }

 }

1 个答案:

答案 0 :(得分:2)

这是相对基本的(我说,但是当我做这些事情时我不得不查阅它)。基本上它的作用是为您提供一种方法,通过该方法,您可以在给定系数(或时间)0到1之间的线上找到一个点。

此示例允许您调整点之间的距离,系数越小,距离越小。例如,第一个图像使用增量0.1d,而第二个图像使用0.01d

@0.1 @0.01

对于这个例子,试着在0.05d周围做一个漂亮的“gaggared”行;)

所有酷炫的“数学东西”都发生在between方法......

public Point2D between(Point p1, Point p2, double time) {

    double deltaX = p2.getX() - p1.getX();
    double deltaY = p2.getY() - p1.getY();

    double x = p1.getX() + time * deltaX;
    double y = p1.getY() + time * deltaY;

    return new Point2D.Double(x, y);

}

对于可运行的示例......

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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
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) {
                }

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

    public class TestPane extends JPanel {

        private Point p1 = new Point(50, 50);
        private Point p2 = new Point(150, 150);

        public TestPane() {
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            for (double t = 0; t < 1; t += 0.01) {

                Point2D p = between(p1, p2, t);
                g2d.fillRect((int)p.getX(), (int)p.getY(), 5, 5);

            }
            g2d.dispose();
        }

        public Point2D between(Point p1, Point p2, double time) {

            double deltaX = p2.getX() - p1.getX();
            double deltaY = p2.getY() - p1.getY();

            double x = p1.getX() + time * deltaX;
            double y = p1.getY() + time * deltaY;

            return new Point2D.Double(x, y);

        }
    }

}