Mr Wigg
Mr Wigg

Reputation: 41

Java graphics draw a line using fillRect

I want to draw a line but I cannot use "drawline", only "fillRect". I have the following code, when I am reading a point from the user via mouseclick event and place a rectangle.

I need to do the same but draw a line by drawing rectangles from point A to point B.

Here is my code:

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

 }

Upvotes: 1

Views: 1920

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

This is relatively basic (I say that, but I had to look it up when I was doing these things). Basically what this does is provides you a method by which you can find a point along a line at a given coefficient (or time) between 0 and 1.

This example allows you to adjust the distance between the points, the smaller the coefficient, the small the distance. For example, the first images uses a increment of 0.1d while the second use 0.01d

@0.1@0.01

For this example, try for around 0.05d to make a nice "gaggared" line ;)

All the cool "maths stuff" happens in the between method...

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

}

For runnable example...

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

        }
    }

}

Upvotes: 2

Related Questions