import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Bolita {
private int x;
private int y;
private JPanel panel;
private Color color;
-
public Bolita(Color col, JPanel panel2) {
color = col;
panel = panel2;
}
public void modPos(int x_, int y_){
x = x_;
y = y_;
}
public void paint (Graphics g){
g = panel.getGraphics();
g.setColor(color);
g.fillOval(x,y,20,20);
}
public void setXORMode( Graphics g ){
g = panel.getGraphics();
g.fillOval(x,y,20,20);
g.clearRect(x, y, 20, 20);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
//CLASE PILA DE BOLITAS (MANEJADOR)
[des]activar nros. de línea
import java.awt.*;
public class PilaDeBolitas {
private static final long serialVersionUID = 1L;
public static final int MAX = 10;
public class UnderFlowException extends Exception{
private static final long serialVersionUID = -9026952799388596808L;
}
public class OverFlowException extends Exception{
private static final long serialVersionUID = -5485402241073047658L;
}
// Atributos
private Bolita [] bolitas; // Arreglo que contiene los elementos
private int tope;
public int getTope() {
return tope;
}
public void paint(Graphics g) {
for(int i = 0; i <>
bolitas[i].paint(g);
}
}
public void setXORMode(Graphics g) {
for(int i = 0; i <>
bolitas[i].setXORMode(g);
}
}
public Bolita getTopeBolita(){
return bolitas[getTope()];
}
public PilaDeBolitas(){
//super("SUPER PAINT",false,false,false,false);
bolitas = new Bolita [MAX];
tope = 0;
}
public void apilar(Bolita b) throws OverFlowException{
if(tope + 1 >= MAX){
throw new OverFlowException();
}
if(tope==-1){
tope = 0;
}
bolitas[tope] = b;
tope++;
b.modPos(tope * 20 + 40, 100);
}
public Bolita desapilar() throws UnderFlowException {
if(tope + 1 <>
throw new UnderFlowException();
}
Bolita c;
tope--;
c = bolitas[tope];
while(c.getX()==-40){
c.modPos(-(tope*20+40),100);
break;
}
return c;
}
}
//CLASE INTERFAZ PILA DE BOLITAS
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class InterfazPiladeBolitas extends javax.swing.JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton apilar;
private static JPanel panel;
private JButton desapilar;
private PilaDeBolitas p;
private static Graphics g=null;
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
p.paint(g);
}
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
InterfazPiladeBolitas inst = new InterfazPiladeBolitas();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public InterfazPiladeBolitas() {
super();
initGUI();
p= new PilaDeBolitas();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
apilar = new JButton();
getContentPane().add(apilar);
apilar.setText("Apilar");
apilar.setBounds(56, 230, 167, 43);
apilar.addActionListener(this);
}
{
desapilar = new JButton();
getContentPane().add(desapilar);
desapilar.setText("Desapilar");
desapilar.setBounds(340, 230, 167, 43);
desapilar.addActionListener(this);
}
{
panel = new JPanel();
getContentPane().add(panel);
panel.setBounds(41, 28, 467, 168);
}
pack();
this.setSize(566, 335);
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent v) {
if(v.getSource()==apilar){
try{
Bolita b = new Bolita(Color.red, panel);
p.apilar(b);
b.paint(g);
}
catch(PilaDeBolitas.OverFlowException e2){
JOptionPane.showMessageDialog(null, "La pila ya se lleno", "OverFlowException",JOptionPane.WARNING_MESSAGE);
try{
panel.paint(g);
}catch(Exception e){
}
}
}
else if (v.getSource()==desapilar) {
try{
Bolita r = p.desapilar();
r.setXORMode(g);
}
catch(Exception e2){
JOptionPane.showMessageDialog(null, "La pila esta vacia", "OverFlowException",JOptionPane.WARNING_MESSAGE);
try{
panel.repaint();
}catch(Exception e){
}
}
}
}
}
No hay comentarios:
Publicar un comentario