Estructura:
Código :
JFRAME:
package calcularrecta.newpackage;
//PARa Imagen de fondo se importa el
import java.awt.BorderLayout;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
public class JFrame extends javax.swing.JFrame {
public JFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
panel1 = new calcularrecta.newpackage.Panel();
jLabel4 = new javax.swing.JLabel();
textx0 = new javax.swing.JTextField();
textx1 = new javax.swing.JTextField();
texty0 = new javax.swing.JTextField();
texty1 = new javax.swing.JTextField();
btngraficar = new javax.swing.JButton();
lx0 = new javax.swing.JLabel();
lx1 = new javax.swing.JLabel();
ly0 = new javax.swing.JLabel();
ly1 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel2.setIcon(new javax.swing.ImageIcon("D:\\Projecto PO\\pizarron.gif")); // NOI18N
jLabel2.setAutoscrolls(true);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(102, 255, 153));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
panel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Variables:");
texty1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
texty1ActionPerformed(evt);
}
});
btngraficar.setText("Graficar");
btngraficar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btngraficarActionPerformed(evt);
}
});
lx0.setFont(new java.awt.Font("Tahoma", 0, 14));
lx0.setForeground(new java.awt.Color(255, 255, 255));
lx0.setText("x0:");
lx1.setFont(new java.awt.Font("Tahoma", 0, 14));
lx1.setForeground(new java.awt.Color(255, 255, 255));
lx1.setText("x1:");
ly0.setFont(new java.awt.Font("Tahoma", 0, 14));
ly0.setForeground(new java.awt.Color(255, 255, 255));
ly0.setText("y0:");
ly1.setFont(new java.awt.Font("Tahoma", 0, 14));
ly1.setForeground(new java.awt.Color(255, 255, 255));
ly1.setText("y1:");
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(233, 227, 227));
jLabel1.setText("ECUACION DE LA RECTA");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("® Zona Java");
javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGap(193, 193, 193)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addComponent(btngraficar)
.addGap(85, 85, 85))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addComponent(lx0))
.addComponent(ly0))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textx0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(texty0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(lx1)
.addGap(3, 3, 3))
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(ly1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textx1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(panel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(texty1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(47, 47, 47))))))
.addGroup(panel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textx0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textx1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lx0)
.addComponent(lx1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(texty0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ly0))
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ly1)
.addComponent(texty1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(34, 34, 34)
.addComponent(btngraficar)
.addGap(219, 219, 219))
.addGroup(panel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addGap(324, 324, 324)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}//
/*Antes de que aparezca esto se debio dar clic derecho en el boton graficar luego
Event,Action,Action performed y automaticamente aparece esto
*/
private void btngraficarActionPerformed(java.awt.event.ActionEvent evt) {
//Comienzo a introducir las variables para el texto
// En this.text es el nombre de la variable de cada textfield o entrada de texto
//Lo q hace es leer la informacion de valor entero en la caja de texto
int x0=Integer.parseInt(this.textx0.getText());
int y0=Integer.parseInt(this.texty0.getText());
int x1=Integer.parseInt(this.textx1.getText());
int y1=Integer.parseInt(this.texty1.getText());
//NO ENTIENDO ESTA PARTE VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVvvv
panel1.setX0(x0);
panel1.setY0(y0);
panel1.setX1(x1);
panel1.setY1(y1);
panel1.DibujarRecta();
}
private void texty1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Panel p = new Panel();
this.add( p , BorderLayout.CENTER);
p.repaint();
}
//Comenzamos con la clase principal para la funcion de introducir texto
//Clic derecho luego
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().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 lx0;
private javax.swing.JLabel lx1;
private javax.swing.JLabel ly0;
private javax.swing.JLabel ly1;
private calcularrecta.newpackage.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
}
Diseño :
<!--[endif]-->
Jpanel
package calcularrecta.newpackage;
//1.- Importamos
import java.awt.Graphics;
import java.awt.*;
//**************************
////////////////////////////////////////////////////
//Para ubicar fondo al Jframe
import javax.swing.ImageIcon;
import java.awt.Dimension;
/////////////////////////////////////////////
//Comienzo a programar
public class Panel extends javax.swing.JPanel {
////PARA FONDO DE PANTALLA////////////////////////////////////////////
public Panel(){
this.setSize(700,500);
}
@Override
public void paintComponent(Graphics g){
Dimension tamanio = getSize();
ImageIcon imagenFondo = new ImageIcon(getClass().getResource("/imagen/pizarara.png")); //getResource("/calcularrecta.newpackage/pizarara.png"));
g.drawImage(imagenFondo.getImage(),0,0,tamanio.width, tamanio.height, null);
setOpaque(false);
super.paintComponent(g);
}
//FONDO DE PANTALLA
//2.- Declaramos las variavles privadas
private int x0,y0;
private int x1,y1;
//===========================
//3.- Establecemos metodo escritura dando clic derecho en lugar en blanco y luego insertar codigo, Luego Seeter
//y despues activar las variables privadas para q sea mas rapido la construccion.
//como Resultado:
public void setX0(int x0) {
this.x0 = x0;
}
public void setX1(int x1) {
this.x1 = x1;
}
public void setY0(int y0) {
this.y0 = y0;
}
public void setY1(int y1) {
this.y1 = y1;
}
//Queda Esto===========================
// public Panel() {
//initComponents();
//}
@SuppressWarnings("unchecked")
//
private void initComponents() {
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 643, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 388, Short.MAX_VALUE)
);
}//
//4.- Luego Clic Derecho Insertar,Redefinir objeto,jcomponent, Paint y clic en generar
public void paint(Graphics g) {
super.paint(g);
//Programando.....
//Establesco Color del Marcador o linea
g.setColor(Color.white);
//Establesco Grafico o figura geometrica y sus coordenadas ubicando this osea el valor por teclado
g.drawLine(this.x1,this.y1,this.x0,this.y0);
//Creo objeto calcular que es la clase java q se creo o sea para llamarlo con sus coordenadas
calcular Obj = new calcular(this.x1,this.y1,this.x0,this.y0);
// Creo una variable double q es = al objeto distancia
double dist = Obj.Distancia();
//Creo una Cadena donde se mostrara d
String cadena = "Distancia = " + String.valueOf(dist);
//Luego especifico donde se va a ubicar el texto o gotoxy
g.drawString(cadena, 30,130);
//PUNTOS MEDIOS XY//////////////////////////////////////////
//PM x
double px = Obj.Distx();
String pm = "Punto Medio : " + String.valueOf(px);
g.drawString(pm, 30,160);
//PMY
double py = Obj.Disty();
String pmy = " = " + String.valueOf(py);
g.drawString(pmy, 150,160);
////////////////////////////////////////////////////////////////////////////
}
//A continuacion creamos el Actualizador
public void DibujarRecta()
{
//Repaint: Actualiza cada vez q grafique
repaint();
}
//Listo........
// Variables declaration - do not modify
// End of variables declaration }
CLASE Java CALCULAR
package calcularrecta.newpackage;
public class calcular {
//Creo las variables privadas
private int x0,y0;
private int x1,y1;
//Creo public calcular
public calcular(int x0, int y0, int x1, int y1)
{
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
}
//Comienzo a ubicar para el ejercicio
//DISTANCIA DE UNA RECTA
//Estaba en privada
private double DistNum()
{
double nume=this.y1 - this.y0;
return nume;
}
private double Denom(){
double deno=this.x1 - this.x0;
return deno;
}
private double form(){
double soln= this.DistNum();
double sold= this.Denom();
double re=(soln)/(sold);
return re;
}
//Aplicar el Resultado
public double Distancia()
{
double distancia = this.form();
return distancia;
}
//PUNTO MEDIO //////////////////////////////////////////////////////////////
//Tienen que ser publicas para ser mostradaaaas
public double Distx()
{
double dex =(this.x1+this.x0)/2;
return dex;
}
public double Disty()
{
//int x;
//int y=0;
double dy =(this.y1+this.y0)/2; // (((this.y1-this.y0)/(this.x1-this.x0))*(this.x0));
return dy;
}
public double puntomedios()
{
double px = this.Distx();
double py = this.Disty();
//Si elimino esto por q esta por gusto no funciona el programa
double rp = px+py ;
return rp;
}
}