JAVA NETBEANS GRAFICOS
Ecuación de una recta:
Este programa realiza los cálculos del punto medio y la destancia de una recta, y al mismo tiempo dibuja la recta para lo cual utilizamos las clases J Frame y Jpanel y una clase normal para realizar los cálculos
Clase normal
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;
}
public double DistanciaPuntos()
{
double d = Math.sqrt((Math.pow((this.X1 - this.Xo), 2)) + (Math.pow((this.Y1 - this.Yo), 2)));
return d;
}
public double puntox()
{
double p1;
p1=(this.Xo+this.X1)/2;
return p1;
}
public double puntoy()
{
double p2;
p2=(this.Yo+this.Y1)/2;
return p2;
}}
Frame o principal:
package appdibujarcirculocalculararea;
public class Principal extends javax.swing.JFrame {
public Principal() {
initComponents();
}
@SuppressWarnings("unchecked")
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.Calculos( Xo, Yo, X1, Y1);
panel1.recta();}
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;
}
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 Calculos(int Xo, int Yo, int X1, int Y1)
{
this.Xo = Xo;
this.Yo = Yo;
this.X1 = X1;
this.Y1 = Y1;
}
public Panel() {
initComponents();
}
@SuppressWarnings("unchecked")
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.ORANGE);
g.drawLine(this.Xo, this.Yo, this.X1, this.Y1);
Calculos Obj = new Calculos(this.Xo, this.Yo, this.X1, this.Y1);
double a = Obj.DistanciaPuntos();
String cadena = "La Distancia es = " + String.valueOf(a);
double aa=Obj.puntox();
double aaa=Obj.puntoy();
String cade = "El punto medio en X = " + String.valueOf(aa)+"Y"+String.valueOf(aaa);
g.drawString(cadena, 5, 10);
g.drawString(cade, 300, 10);
}
public void recta()
{
repaint();
} }
AREA DE UNA SISCUFERENCIA
Este programa sirve para calcular el área de una circunferencia en el cual utilizamos una clase normal para realizar los cálculos, un jpanel para las funciones graficas y un frame para que funcione como principal.
Clase normal:
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;
}
}
Jframe
package appdibujarcirculocalculararea;
public class Principal extends javax.swing.JFrame {
public Principal() {
initComponents();
}
@SuppressWarnings("unchecked")
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 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
}
Jpanel
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")
@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
}
No hay comentarios:
Publicar un comentario