1.- Jframe
2.- Jpanel
Pasos:
1.- Creamos 1 Nuevo Proyecto en Java Aplicacion
2.- Creamos un Paquete Java
3.- Creamos una Clase Java
4.- Clic derecho dentro del Paquete crear:
-Formulario JPanel
-Formulario Jframe
Formulario JPanel: Establece la programación
Formulario Jframe: Establece la Estructura Gráfica.
Clase Java:
package appdibujarcirculocalculararea;
public class Calculos
{
private int Xo,Yo;
private int X1,Y1;
public Calculos(int Xo, int Yo, int X1, int Y1)
{
this.Xo = Xo;
this.Yo = Yo;
this.X1 = X1;
this.Y1 = Y1;
}
private double DistanciaPuntos()
{
double d = Math.sqrt((Math.pow((this.X1 - this.Xo), 2)) + (Math.pow((this.Y1 - this.Yo), 2)));
return d;
}
public double AreaCirculo()
{
double distancia = this.DistanciaPuntos();
double radio = distancia / 2;
double area = Math.PI * Math.pow(radio, 2);
return area;
}
}
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")
//
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)
);
}//
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawOval(this.Xo, this.Yo, this.X1, this.Y1);
Calculos Obj = new Calculos(this.Xo, this.Yo, this.X1, this.Y1);
double a = Obj.AreaCirculo();
String cadena = "Area = " + String.valueOf(a);
g.drawString(cadena, 10, 15);
}
public void DibujarCirculo()
{
repaint();
}
// Variables declaration - do not modify
// End of variables declaration
}
JFrame :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Principal.java
*
* Created on 26/08/2011, 08:04:16 AM
*/
package appdibujarcirculocalculararea;
/**
*
* @author lab.sistemas
*/
public class Principal extends javax.swing.JFrame {
/** Creates new form Principal */
public Principal() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
textX0 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
texty0 = new javax.swing.JTextField();
textX1 = new javax.swing.JTextField();
texty1 = new javax.swing.JTextField();
btngraficar = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
panel1 = new appdibujarcirculocalculararea.Panel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(102, 102, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setForeground(java.awt.Color.white);
jPanel1.setBackground(new java.awt.Color(0, 153, 153));
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("X0:");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Y0:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("x1:");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("y1");
btngraficar.setText("Graficar");
btngraficar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btngraficarActionPerformed(evt);
}
});
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("Zona Java 2011");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textX0, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(69, 69, 69)
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(texty0, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textX1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(texty1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36)
.addComponent(btngraficar)
.addContainerGap())
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(textX0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(textX1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(texty0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(texty1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(59, Short.MAX_VALUE)
.addComponent(btngraficar)
.addGap(15, 15, 15)
.addComponent(jLabel6))
);
jLabel5.setFont(new java.awt.Font("Vineta BT", 0, 18));
jLabel5.setText("CIRCULO CALCULO DE AREA ");
javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 387, Short.MAX_VALUE)
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 133, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5)
.addContainerGap(162, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}//
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();
}
/**
* @param args the command line arguments
*/
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 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
}
Ejecución:
Este comentario ha sido eliminado por el autor.
ResponderEliminarViejo me sirvió mucho, la ultima parte de las variables me sobro.
ResponderEliminarGracias