How to draw lines in Java

前端 未结 10 1982
梦毁少年i
梦毁少年i 2020-11-30 02:42

I\'m wondering if there\'s a funciton in Java that can draw a line from the coordinates (x1, x2) to (y1, y2)?

What I want is to do something like this:



        
相关标签:
10条回答
  • 2020-11-30 02:58

    I built a whole class of methods to draw points, lines, rectangles, circles, etc. I designed it to treat the window as a piece of graph paper where the origin doesn't have to be at the top left and the y values increase as you go up. Here's how I draw lines:

    public static void drawLine (double x1, double y1, double x2, double y2)
    {       
        ((Graphics2D)g).draw(new Line2D.Double(x0+x1*scale, y0-y1*scale, x0+x2*scale, y0-y2*scale));
    }
    

    In the example above, (x0, y0) represents the origin in screen coordinates and scale is a scaling factor. The input parameters are to be supplied as graph coordinates, not screen coordinates. There is no repaint() being called. You can save that til you've drawn all the lines you need.

    It occurs to me that someone might not want to think in terms of graph paper:

        ((Graphics2D)g).draw(new Line2D.Double(x1, y1, x2, y2));
    

    Notice the use of Graphics2D. This allows us to draw a Line2D object using doubles instead of ints. Besides other shapes, my class has support for 3D perspective drawing and several convenience methods (like drawing a circle centered at a certain point given a radius.) If anyone is interested, I would be happy to share more of this class.

    0 讨论(0)
  • 2020-11-30 03:04

    A very simple example of a swing component to draw lines. It keeps internally a list with the lines that have been added with the method addLine. Each time a new line is added, repaint is invoked to inform the graphical subsytem that a new paint is required.

    The class also includes some example of usage.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class LinesComponent extends JComponent{
    
    private static class Line{
        final int x1; 
        final int y1;
        final int x2;
        final int y2;   
        final Color color;
    
        public Line(int x1, int y1, int x2, int y2, Color color) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.color = color;
        }               
    }
    
    private final LinkedList<Line> lines = new LinkedList<Line>();
    
    public void addLine(int x1, int x2, int x3, int x4) {
        addLine(x1, x2, x3, x4, Color.black);
    }
    
    public void addLine(int x1, int x2, int x3, int x4, Color color) {
        lines.add(new Line(x1,x2,x3,x4, color));        
        repaint();
    }
    
    public void clearLines() {
        lines.clear();
        repaint();
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            g.setColor(line.color);
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
        }
    }
    
    public static void main(String[] args) {
        JFrame testFrame = new JFrame();
        testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        final LinesComponent comp = new LinesComponent();
        comp.setPreferredSize(new Dimension(320, 200));
        testFrame.getContentPane().add(comp, BorderLayout.CENTER);
        JPanel buttonsPanel = new JPanel();
        JButton newLineButton = new JButton("New Line");
        JButton clearButton = new JButton("Clear");
        buttonsPanel.add(newLineButton);
        buttonsPanel.add(clearButton);
        testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
        newLineButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                int x1 = (int) (Math.random()*320);
                int x2 = (int) (Math.random()*320);
                int y1 = (int) (Math.random()*200);
                int y2 = (int) (Math.random()*200);
                Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
                comp.addLine(x1, y1, x2, y2, randomColor);
            }
        });
        clearButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                comp.clearLines();
            }
        });
        testFrame.pack();
        testFrame.setVisible(true);
    }
    
    }
    
    0 讨论(0)
  • 2020-11-30 03:04

    I understand you are using Java AWT API for drawing. The paint method is invoked when the control needs repainting. And I'm pretty sure it provides in the Graphics argument what rectangle is the one who needs repainting (to avoid redrawing all).

    So if you are presenting a fixed image you just draw whatever you need in that method.

    If you are animating I assume you can invalidate some region and the paint method will be invoked automagically. So you can modify state, call invalidate, and it will be called again.

    0 讨论(0)
  • 2020-11-30 03:07
    a simple line , after that you can see also a doted line 
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.Graphics.*;
    
    import java.awt.Graphics2D.*;
    
    import javax.swing.JFrame;
    
    import javax.swing.JPanel;
    
    import java.awt.BasicStroke;
    
    import java.awt.Event.*;
    
    import java.awt.Component.*;
    
    import javax.swing.SwingUtilities;
    
    
    /**
     *
     * @author junaid
     */
    public class JunaidLine extends JPanel{
    
    
    //private Graphics Graphics;
    
    private void doDrawing(Graphics g){
    
    Graphics2D g2d=(Graphics2D) g;
    
    float[] dash1 = {2f,0f,2f};
    
    g2d.drawLine(20, 40, 250, 40);
    
    BasicStroke bs1 = new BasicStroke(1,BasicStroke.CAP_BUTT,
    
                        BasicStroke.JOIN_ROUND,1.0f,dash1,2f);
    
    g2d.setStroke(bs1);
    
    g2d.drawLine(20, 80, 250, 80);
    
        }
    
    @Override
    
    public void paintComponent(Graphics g){
    
    super.paintComponent( g);
    
    doDrawing(g);
    
    }
    
    
    }
    
    class BasicStrokes extends JFrame{
    
    public  BasicStrokes(){
    
    initUI();
    
    }
    
    private void initUI(){
    
    setTitle("line");
    
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    add(new JunaidLine());
    
    setSize(280,270);
    
    setLocationRelativeTo(null);
    
    }
    
    /**
    
    * @param args the command line arguments
    
    */
    
    public static void main(String[] args) {
    
    
    SwingUtilities.invokeLater(new Runnable(){   
    
    @Override
    
    public void run(){
    
    BasicStrokes bs = new BasicStrokes();
    
    bs.setVisible(true);
    
    }                
    
    });
    
    }
    
    
    }
    
    0 讨论(0)
  • 2020-11-30 03:08

    Store the lines in some type of list. When it comes time to paint them, iterate the list and draw each one. Like this:

    DrawLines

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.geom.Line2D;
    
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    class DrawLines {
    
        public static void main(String[] args) {
    
            Runnable r = new Runnable() {
                public void run() {
                    LineComponent lineComponent = new LineComponent(400,400);
                    for (int ii=0; ii<30; ii++) {
                        lineComponent.addLine();
                    }
                    JOptionPane.showMessageDialog(null, lineComponent);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    
    class LineComponent extends JComponent {
    
        ArrayList<Line2D.Double> lines;
        Random random;
    
        LineComponent(int width, int height) {
            super();
            setPreferredSize(new Dimension(width,height));
            lines = new ArrayList<Line2D.Double>();
            random = new Random();
        }
    
        public void addLine() {
            int width = (int)getPreferredSize().getWidth();
            int height = (int)getPreferredSize().getHeight();
            Line2D.Double line = new Line2D.Double(
                random.nextInt(width),
                random.nextInt(height),
                random.nextInt(width),
                random.nextInt(height)
                );
            lines.add(line);
            repaint();
        }
    
        public void paintComponent(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, getWidth(), getHeight());
            Dimension d = getPreferredSize();
            g.setColor(Color.black);
            for (Line2D.Double line : lines) {
                g.drawLine(
                    (int)line.getX1(),
                    (int)line.getY1(),
                    (int)line.getX2(),
                    (int)line.getY2()
                    );
            }
        }
    }
    

    Screenshot

    enter image description here

    0 讨论(0)
  • 2020-11-30 03:13

    You need to create a class that extends Component. There you can override the paint method and put your painting code in:

    package blah.whatever;
    
    import java.awt.Component;
    import java.awt.Graphics;
    
    public class TestAWT extends Component {
    
    /** @see java.awt.Component#paint(java.awt.Graphics) */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawLine(0,0,100,100);
        g.drawLine(10, 10, 20, 300);
        // more drawing code here...
    }
    
    }
    

    Put this component into the GUI of your application. If you're using Swing, you need to extend JComponent and override paintComponent, instead.

    As Helios mentioned, the painting code actually tells the system how your component looks like. The system will ask for this information (call your painting code) when it thinks it needs to be (re)painted, for example, if a window is moved in front of your component.

    0 讨论(0)
提交回复
热议问题