Saltar al contenido

Cómo dibujar líneas en Java

Contamos con la solución a esta obstáculo, o por lo menos eso creemos. Si continuas con interrogantes coméntalo y sin dudarlo te ayudaremos

Solución:

Un ejemplo muy simple de un componente swing para dibujar líneas. Mantiene internamente una lista con las líneas que se han agregado con el método addLine. Cada vez que se agrega una nueva línea, se invoca el repintado para informar al subsistema gráfico que se requiere una nueva pintura.

La clase también incluye algún ejemplo de uso.

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 lines = new LinkedList();

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



Almacene las líneas en algún tipo de lista. Cuando llegue el momento de pintarlos, repite la lista y dibuja cada uno. Me gusta esto:

Captura de pantalla

ingrese la descripción de la imagen aquí

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 lines;
    Random random;

    LineComponent(int width, int height) 
        super();
        setPreferredSize(new Dimension(width,height));
        lines = new ArrayList();
        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) 
        super.paintComponent(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()
                );
        
    

Debe crear una clase que amplíe Component. Allí puede anular el método de pintura y poner su código de pintura en:

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...



Coloque este componente en la GUI de su aplicación. Si está utilizando Swing, debe extender JComponent y anular paintComponent, en su lugar.

Como mencionó Helios, el código de pintura en realidad le dice al sistema cómo se ve su componente. El sistema solicitará esta información (llame a su código de pintura) cuando crea que necesita ser (re)pintado, por ejemplo, si una ventana se mueve frente a su componente.

Reseñas y puntuaciones

Puedes defender nuestro cometido añadiendo un comentario y valorándolo te lo agradecemos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *