Así quedo terminado este programa de practica en java, les dejo el código fuente, para seguir modificándolo pueden usar el Easy Eclipse
package modelo
Class Pelota.java
package modelo;
import javax.swing.JButton;
public class Pelota extends Thread {
private JButton pelota;
private int x;
private int y;
private Control activo;
private boolean arriba, derecha;
private int aceleracion;
private Tiempo time;
public Pelota(int x, int y,JButton pelota ,Control activo, int acele, Tiempo time)
{
this.x=x;
this.y=y;
this.pelota=pelota;
this.activo=activo;
arriba=false;
derecha=true;
this.aceleracion=acele;
this.time=time;
}
public void run()
{
while(activo.getControl()&&time.getOk())
{
pelota.setLocation(x, y);
if(arriba)
{
y=y-aceleracion;
}
else
{
y=y+aceleracion;
}
if(derecha)
{
x=x+aceleracion;
}
else
{
x=x-aceleracion;
}
if(x<3)
{
derecha=true;
}
if(x>700)
{
derecha=false;
}
if(y>300)
{
arriba=true;
}
if(y<3)
{
arriba=false;
}
try{
sleep(10);
}
catch(Exception e)
{
}
time.disminuye();
}
}
}
Class ControlTiempo.java
package modelo;
import javax.swing.JLabel;
public class ControlTiempo extends Thread {
private Tiempo t;
private JLabel l;
public ControlTiempo(Tiempo t, JLabel l)
{
this.t=t;
this.l=l;
}
public void run()
{
while(t.getTiempo()>0)
{
l.setText(String.valueOf(t.getTiempo()));
try{
sleep(200);
}
catch(Exception error)
{
}
}
}
}
Class Tiempo.java
package modelo;
public class Tiempo {
private boolean ok;
private int tiempo;
public Tiempo()
{
ok=true;
tiempo=10000;
}
public void setOk(boolean x)
{
ok=x;
}
public boolean getOk()
{
return ok;
}
public int getTiempo()
{
return tiempo;
}
public void setTiempo(int t)
{
this.tiempo=t;
}
public void disminuye()
{
tiempo--;
if(tiempo==0)
ok=false;
}
public void rotate()
{
if(ok)
ok=false;
else
ok=true;
}
}
Class Control.java
package modelo;
public class Control {
boolean control;
public Control()
{
control=true;
}
public void setControl(boolean x)
{
control=x;
}
public boolean getControl()
{
return control;
}
public void rotate()
{
if(control)
control=false;
else
control=true;
}
}
package visual
Class Pong.java
package visual;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Color;
import javax.swing.JButton;
import modelo.Control;
import modelo.Pelota;
import modelo.Tiempo;
import modelo.ControlTiempo;
public class Pong extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton JbPlay = null;
private Control z1;
private Control z2;
private Control z3;
private Control z4;
private Control z5;
private Pelota j1;
private Pelota j2;
private Pelota j3;
private Pelota j4;
private Pelota j5;
private JButton JB1 = null;
private JButton JB2 = null;
private JButton JB3 = null;
private JButton JB4 = null;
private JButton JB5 = null;
private JLabel jLabel = null;
private JLabel Nivel = null;
private JLabel JLnivel = null;
private int nivel;
private Tiempo time;
private JLabel jltiempo = null;
private JLabel Tiempo = null;
private JLabel jlpuntaje = null;
private JLabel Puntaje = null;
private ControlTiempo t;
private int puntaje;
private JButton getJbPlay() {
if (JbPlay == null) {
JbPlay = new JButton();
JbPlay.setBounds(new Rectangle(10, 466, 98, 19));
JbPlay.setText("Play");
JbPlay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
nivel++;
time = new Tiempo();
t= new ControlTiempo(time, Tiempo);
z1=new Control();
z2=new Control();
z3=new Control();
z4=new Control();
z5=new Control();
j1 = new Pelota(JB1.getX(),JB1.getY(),JB1, z1,nivel, time);
j2 = new Pelota(JB2.getX(),JB2.getY(),JB2, z2,nivel+1, time);
j3 = new Pelota(JB3.getX(),JB3.getY(),JB3, z3,nivel+2, time);
j4 = new Pelota(JB4.getX(),JB4.getY(),JB4, z4,nivel+3, time);
j5 = new Pelota(JB5.getX(),JB5.getY(),JB5, z5,nivel+4, time);
t.start();
j1.start();
j2.start();
j3.start();
j4.start();
j5.start();
Nivel.setText(String.valueOf(nivel));
}
});
}
return JbPlay;
}
private JButton getJB1() {
if (JB1 == null) {
JB1 = new JButton();
JB1.setBounds(new Rectangle(268, 28, 67, 57));
JB1.setText("1");
JB1.setBackground(new Color(0, 51, 204));
JB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
z1.setControl(false);
Puntaje();
}
});
}
return JB1;
}
private JButton getJB2() {
if (JB2 == null) {
JB2 = new JButton();
JB2.setBounds(new Rectangle(112, 154, 82, 91));
JB2.setText("2");
JB2.setBackground(Color.yellow);
JB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
z2.setControl(false);
Puntaje();
}
});
}
return JB2;
}
private JButton getJB3() {
if (JB3 == null) {
JB3 = new JButton();
JB3.setBounds(new Rectangle(247, 139, 95, 93));
JB3.setText("3");
JB3.setBackground(new Color(0, 204, 255));
JB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
z3.setControl(false);
Puntaje();
}
});
}
return JB3;
}
private JButton getJB4() {
if (JB4 == null) {
JB4 = new JButton();
JB4.setBounds(new Rectangle(465, 137, 96, 127));
JB4.setText("4");
JB4.setBackground(new Color(51, 255, 0));
JB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
z4.setControl(false);
Puntaje();
}
});
}
return JB4;
}
private JButton getJB5() {
if (JB5 == null) {
JB5 = new JButton();
JB5.setBounds(new Rectangle(457, 34, 137, 163));
JB5.setText("5");
JB5.setBackground(new Color(255, 51, 51));
JB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
z5.setControl(false);
Puntaje();
}
});
}
return JB5;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Pong thisClass = new Pong();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public Pong() {
super();
initialize();
}
private void initialize() {
this.setSize(837, 539);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("Pong");
nivel = 0;
puntaje = 0;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
Puntaje = new JLabel();
Puntaje.setBounds(new Rectangle(401, 466, 74, 19));
Puntaje.setText("0");
jlpuntaje = new JLabel();
jlpuntaje.setBounds(new Rectangle(344, 466, 51, 19));
jlpuntaje.setText("Puntaje:");
Tiempo = new JLabel();
Tiempo.setBounds(new Rectangle(279, 466, 62, 19));
Tiempo.setText("10000");
jltiempo = new JLabel();
jltiempo.setBounds(new Rectangle(218, 466, 56, 19));
jltiempo.setText("Tiempo:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 491, 329, 20));
jLabel.setText("Hay que hacer clic sobre los cuadrados para pararlos");
JLnivel = new JLabel();
JLnivel.setBounds(new Rectangle(118, 466, 35, 19));
JLnivel.setText("Nivel:");
Nivel = new JLabel();
Nivel.setBounds(new Rectangle(157, 466, 55, 19));
Nivel.setText("1");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJbPlay(), null);
jContentPane.add(getJB1(), null);
jContentPane.add(getJB2(), null);
jContentPane.add(getJB3(), null);
jContentPane.add(getJB4(), null);
jContentPane.add(getJB5(), null);
jContentPane.add(Nivel, null);
jContentPane.add(JLnivel, null);
jContentPane.add(jLabel, null);
jContentPane.add(jltiempo, null);
jContentPane.add(Tiempo, null);
jContentPane.add(jlpuntaje, null);
jContentPane.add(Puntaje, null);
}
return jContentPane;
}
public void Puntaje()
{
if(!z1.getControl()&&!z2.getControl()&&!z3.getControl()&&!z4.getControl()&&!z5.getControl())
{
puntaje+=time.getTiempo();
time.setTiempo(0);
}
Puntaje.setText(String.valueOf(puntaje));
}
}
Bueno, quería mostrarlo…

0 comentarios:
Publicar un comentario en la entrada