Programa que dibuja una circunferencia en un panel de java esta es un pequeño ejercicio que permite al estudiante comenzar en la programacion grafica en java.
CLASE JFRAME
package appdibujarcirculocalculararea;
public class Principal extends javax.swing.JFrame {
public Principal() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {}
private void btngraficarActionPerformed(java.awt.event.ActionEvent evt) {
int Xo = Integer.parseInt(this.textX0.getText());
int Yo = Integer.parseInt(this.texty0.getText());
int X1 = Integer.parseInt(this.textX1.getText());
int Y1 = Integer.parseInt(this.texty1.getText());
panel1.setXo(Xo);
panel1.setYo(Yo);
panel1.setX1(X1);
panel1.setY1(Y1);
panel1.DibujarCirculo();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btncalcular;
private javax.swing.JButton btngraficar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private appdibujarcirculocalculararea.Panel panel1;
private javax.swing.JTextField textX0;
private javax.swing.JTextField textX1;
private javax.swing.JTextField texty0;
private javax.swing.JTextField texty1;
// End of variables declaration
}
CLASE PANEL
package appdibujarcirculocalculararea;
import java.awt.Graphics;
import java.awt.*;
public class Panel extends javax.swing.JPanel {
private int Xo,Yo;
private int X1,Y1;
public void setX1(int X1) {
this.X1 = X1;
}
public void setXo(int Xo) {
this.Xo = Xo;
}
public void setY1(int Y1) {
this.Y1 = Y1;
}
public void setYo(int Yo) {
this.Yo = Yo;
}
public Panel() {
initComponents();
}
@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>
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawOval(this.Xo, this.Yo, this.X1, this.Y1);
}
public void DibujarCirculo()
{
repaint();
}
// Variables declaration - do not modify
// End of variables declaration
}
Publicar un comentario