AppGraficandoCircuito2
Formulario
Fuente
package appgraficandocircuito2;
public class Formulario extends javax.swing.JFrame {
/** Creates new form Principal */
public Formulario() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
txtXo = new javax.swing.JTextField();
txtYo = new javax.swing.JTextField();
txtX1 = new javax.swing.JTextField();
txtY1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
panel1 = new appgraficandocircuito2.Panel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Graficar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
txtXo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtXoActionPerformed(evt);
}
});
txtX1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtX1ActionPerformed(evt);
}
});
jLabel1.setText("Xo");
jLabel2.setText("Yo");
jLabel3.setText("X1");
jLabel4.setText("Y1");
panel1.setBackground(new java.awt.Color(102, 204, 255));
javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 358, Short.MAX_VALUE)
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 176, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(jButton1)
.addContainerGap(178, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtXo)
.addComponent(txtYo, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 219, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txtY1)
.addComponent(txtX1, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE))))
.addGap(32, 32, 32))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtXo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(txtX1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtYo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtY1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addGap(46, 46, 46))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Leer cajas de texto a variables de memoria.
int xo = Integer.parseInt(this.txtXo.getText());
int yo = Integer.parseInt(this.txtYo.getText());
int x1 = Integer.parseInt(this.txtX1.getText());
int y1 = Integer.parseInt(this.txtY1.getText());
//Envio los valores de las variables de memoria a las
//variables de instancia a travez de las propiedades.
panel1.setXo(xo);
panel1.setYo(yo);
panel1.setX1(x1);
panel1.setY1(y1);
//Llamo al metodo paint a travez del metodo DibujarLinea.
panel1.appgraficandocircuito2();
Calculo Obj = new Calculo();
double rd = Obj.CalcularRadio(xo,yo,x1,y1);
this.txtmensaje.setText("Radio: " + rd);
}
private void txtXoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtX1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Formulario().setVisible(true);
}
});
}
private javax.swing.JLabel txtmensaje;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private appgraficandocircuito2.Panel panel1;
private javax.swing.JTextField txtX1;
private javax.swing.JTextField txtXo;
private javax.swing.JTextField txtY1;
private javax.swing.JTextField txtYo;
// End of variables declaration
}
Diseño
Panel
Fuente
package appgraficandocircuito2;
import java.awt.Color;
import java.awt.Graphics;
public class Panel extends javax.swing.JPanel
{
//Variables de Instancia.
protected int Xo,Yo;
protected int X1,Y1;
//Propiedades Tipo Escritura.
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;
}
/** Creates new form Panel */
public Panel() {
initComponents();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
//Codigo para Dibuja Circulo.
g.setColor(Color.GREEN);
g.drawOval(this.Xo, this.Yo, this.X1, this.Y1);
}
public void appgraficandocircuito2 ()
{
repaint();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setBackground(new java.awt.Color(0, 153, 204));
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
}
Calculo
package appgraficandocircuito2;
public class Calculo
{
public double CalcularRadio(int Xo,int Yo, int X1, int Y1)
{
double d,p1,p2;
p1=Math.pow((Xo + X1),2);
p2=Math.pow((Yo + Y1),2);
d = Math.sqrt(p1 + p2);
return d/2;
}
}
Ejecución