En el siguiente blog vamos a describir como utilizar el algoritmo gráfico de la linea en una prueba con un ejercicio:
1. Necesitaremos tener instalado Netbean para comenzar con el ejercicio.
2. Crearemos 2 java class:
2.1 Class JFrame
2.2 Class JPanel
Y escribiremos todo este codigo:
package DibujarPrimitivaLinea;
import java.awt.Graphics;
import java.awt.Color;
public class Lienzo extends javax.swing.JPanel
{
//Creamos los Atributos--> Leer Coordenadas.
private int x0;
private int y0;
private int x1;
private int y1;
Color color;
//Implementar Propiedades--> Escritura(setter)(getter).
//le damos las propiedades de escritura y lectura porque en nuestro caso escribiremos los parametros de la linea.
public void setX0(int x0) {
this.x0 = x0;
}
public void setY0(int y0) {
this.y0 = y0;
}
public void setX1(int x1) {
this.x1 = x1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public Lienzo()
{
initComponents();
}
// a continuacion realizamos el metodo paint donde intrucimos el algoritmo grafico de la linea
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(color.red);
int dx = x1 - x0;
int dy = y1 - y0;
if (Math.abs(dx) > Math.abs(dy))
{
float m = (float) dy / (float) dx;
float b = y0 - m * x0;
if(dx<0)
dx = -1;
else
dx = 1;
while (x0 != x1)
{
x0 += dx;
y0 = Math.round(m*x0 + b);
g.drawLine( x0, y0, x0, y0);
}
} else
if (dy != 0)
{
float m = (float) dx / (float) dy;
float b = x0 - m*y0;
if(dy<0)
dy = -1;
else
dy = 1;
while (y0 != y1)
{
y0 += dy;
x0 = Math.round(m * y0 + b);
g.drawLine( x0, y0, x0, y0);
}
}
}
public void DibujarPrimitivaLinea()
{
repaint();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
Al terminar la programacion anterior dentro del lienzo arrastramos el lienzo hasta el JFrame para poder programar dentro del formulario que se va a mostrar
package DibujarPrimitivaLinea;
public class Formulario extends javax.swing.JFrame
{
public Formulario()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtX0 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtY0 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtX1 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtY1 = new javax.swing.JTextField();
btnDibujarPrimitiva = new javax.swing.JButton();
//como vemos aqui al arrastrar el lienzo se genera el código automáticamente
lienzo1 = new DibujarPrimitivaLinea.Lienzo();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Coordenada X0:");
jLabel2.setText("Coordenada Y0:");
jLabel3.setText("Coordenada X1:");
jLabel4.setText("Coordenada Y1:");
btnDibujarPrimitiva.setText("Dibujar");
Este es el evento para el boton dibujar
btnDibujarPrimitiva.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDibujarPrimitivaActionPerformed(evt);
}
});
private void btnDibujarPrimitivaActionPerformed(java.awt.event.ActionEvent evt) {
int x0= Integer.parseInt(this.txtX0.getText());
int y0= Integer.parseInt(this.txtY0.getText());
int x1= Integer.parseInt(this.txtX1.getText());
int y1= Integer.parseInt(this.txtY1.getText());
lienzo1.setX0(x0);
lienzo1.setY0(y0);
lienzo1.setX1(x1);
lienzo1.setY1(y1);
lienzo1.DibujarPrimitivaLinea();
}
No hay comentarios:
Publicar un comentario