PILA DE BOLITAS
Cabe mencionar que la interfaz grafica de este programa se hizo con Jigloo SWT/Swing GUI Builder, para fines totalmente educativos, en concordancia con las politicas establecidas en el directorio. Esta aclaracion se hace con el fin de establecer la licencia ya que Jigloo es gratis solo para este tipo de uso.
CLASE BOLITA
1 import java.awt.Color;
2
3 import java.awt.Graphics;
4
5 import javax.swing.JPanel;
6 /**
7 *
8 * @author carlos arturo gutierrez alias catudo
9 *
10 */
11
12
13 public class Bolita {
14
15
16 private int x;
17 private int y;
18 private JPanel panel;
19 private Color color;
20
21
22
23
24
25
26 public Bolita(Color col, JPanel panel2) {
27 color = col;
28 panel = panel2;
29 }
30
31
32
33
34 public void modPos(int x_, int y_){
35 x = x_;
36 y = y_;
37 }
38
39 public void paint (Graphics g){
40
41 g = panel.getGraphics();
42
43 g.setColor(color);
44 g.fillOval(x,y,20,20);
45
46
47
48 }
49
50 public void setXORMode( Graphics g ){
51 g = panel.getGraphics();
52 g.fillOval(x,y,20,20);
53 g.clearRect(x, y, 20, 20);
54
55
56 }
57
58
59
60 public int getX() {
61 return x;
62 }
63
64
65
66
67 public void setX(int x) {
68 this.x = x;
69 }
70
71
72
73
74 public int getY() {
75 return y;
76 }
77
78
79
80
81 public void setY(int y) {
82 this.y = y;
83 }
84
85
86
87
88 public Color getColor() {
89 return color;
90 }
91
92
93
94
95 public void setColor(Color color) {
96 this.color = color;
97 }
98 }
CLASE PILA DE BOLITAS (MANEJADOR)
1
2 import java.awt.*;
3
4 /**
5 *
6 * @author carlos arturo gutierrez alias catudo
7 *
8 */
9
10
11 public class PilaDeBolitas {
12
13 /**
14 *
15 */
16 private static final long serialVersionUID = 1L;
17 public static final int MAX = 10;
18
19
20 public class UnderFlowException extends Exception{
21
22 /**
23 *
24 */
25 private static final long serialVersionUID = -9026952799388596808L;
26
27 }
28
29 public class OverFlowException extends Exception{
30
31 /**
32 *
33 */
34 private static final long serialVersionUID = -5485402241073047658L;
35
36 }
37 // Atributos
38
39 private Bolita [] bolitas; // Arreglo que contiene los elementos
40 private int tope;
41
42
43 public int getTope() {
44 return tope;
45 }
46
47
48 public void paint(Graphics g) {
49
50 for(int i = 0; i < tope; i++){
51
52 bolitas[i].paint(g);
53 }
54 }
55
56 public void setXORMode(Graphics g) {
57
58 for(int i = 0; i < tope; i++){
59
60 bolitas[i].setXORMode(g);
61 }
62 }
63
64 public Bolita getTopeBolita(){
65 return bolitas[getTope()];
66 }
67
68
69 public PilaDeBolitas(){
70 //super("SUPER PAINT",false,false,false,false);
71 bolitas = new Bolita [MAX];
72 tope = 0;
73 }
74
75 public void apilar(Bolita b) throws OverFlowException{
76 if(tope + 1 >= MAX){
77 throw new OverFlowException();
78 }
79
80 if(tope==-1){
81 tope = 0;
82 }
83
84
85 bolitas[tope] = b;
86 tope++;
87
88 b.modPos(tope * 20 + 40, 100);
89
90
91 }
92
93
94 public Bolita desapilar() throws UnderFlowException {
95 if(tope + 1 < 0){
96 throw new UnderFlowException();
97 }
98
99 Bolita c;
100 tope--;
101 c = bolitas[tope];
102
103 while(c.getX()==-40){
104 c.modPos(-(tope*20+40),100);
105 break;
106 }
107
108 return c;
109
110
111 }
112
113
114 }
115
CLASE INTERFAZ PILA DE BOLITAS
1
2 import java.awt.Color;
3 import java.awt.Graphics;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.JButton;
8 import javax.swing.JOptionPane;
9 import javax.swing.JPanel;
10 import javax.swing.WindowConstants;
11 import javax.swing.SwingUtilities;
12
13
14 /**
15 * This code was edited or generated using CloudGarden's Jigloo
16 * SWT/Swing GUI Builder, which is free for non-commercial
17 * use. If Jigloo is being used commercially (ie, by a corporation,
18 * company or business for any purpose whatever) then you
19 * should purchase a license for each developer using Jigloo.
20 * Please visit www.cloudgarden.com for details.
21 * Use of Jigloo implies acceptance of these licensing terms.
22 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
23 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
24 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
25 *
26 * *
27 * @author carlos arturo gutierrez alias catudo
28 *
29 */
30
31 public class InterfazPiladeBolitas extends javax.swing.JFrame implements ActionListener{
32 /**
33 *
34 */
35 private static final long serialVersionUID = 1L;
36 private JButton apilar;
37 private static JPanel panel;
38 private JButton desapilar;
39 private PilaDeBolitas p;
40
41 private static Graphics g=null;
42
43
44
45 {
46 //Set Look & Feel
47 try {
48 javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
49 } catch(Exception e) {
50 e.printStackTrace();
51 }
52 }
53
54 @Override
55 public void paint(Graphics g) {
56 super.paint(g);
57 p.paint(g);
58 }
59
60
61
62
63
64
65 /**
66 * Auto-generated main method to display this JFrame
67 */
68 public static void main(String[] args) {
69 SwingUtilities.invokeLater(new Runnable() {
70 public void run() {
71 InterfazPiladeBolitas inst = new InterfazPiladeBolitas();
72 inst.setLocationRelativeTo(null);
73 inst.setVisible(true);
74 }
75 });
76 }
77
78 public InterfazPiladeBolitas() {
79 super();
80 initGUI();
81 p= new PilaDeBolitas();
82
83 }
84
85 private void initGUI() {
86 try {
87 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
88 getContentPane().setLayout(null);
89 {
90 apilar = new JButton();
91 getContentPane().add(apilar);
92
93 apilar.setText("Apilar");
94 apilar.setBounds(56, 230, 167, 43);
95 apilar.addActionListener(this);
96 }
97 {
98 desapilar = new JButton();
99 getContentPane().add(desapilar);
100 desapilar.setText("Desapilar");
101 desapilar.setBounds(340, 230, 167, 43);
102 desapilar.addActionListener(this);
103
104
105
106 }
107 {
108 panel = new JPanel();
109 getContentPane().add(panel);
110 panel.setBounds(41, 28, 467, 168);
111 }
112 pack();
113 this.setSize(566, 335);
114 } catch (Exception e) {
115 e.printStackTrace();
116 }
117 }
118
119 public void actionPerformed(ActionEvent v) {
120 if(v.getSource()==apilar){
121 try{
122
123 Bolita b = new Bolita(Color.red, panel);
124
125
126
127 p.apilar(b);
128
129 b.paint(g);
130
131
132
133 }
134 catch(PilaDeBolitas.OverFlowException e2){
135 JOptionPane.showMessageDialog(null, "La pila ya se lleno", "OverFlowException",JOptionPane.WARNING_MESSAGE);
136 try{
137 panel.paint(g);
138 }catch(Exception e){
139
140 }
141
142 }
143 }
144 else if (v.getSource()==desapilar) {
145 try{
146
147
148 Bolita r = p.desapilar();
149 r.setXORMode(g);
150
151
152 }
153 catch(Exception e2){
154 JOptionPane.showMessageDialog(null, "La pila esta vacia", "OverFlowException",JOptionPane.WARNING_MESSAGE);
155
156 try{
157 panel.repaint();
158 }catch(Exception e){
159
160 }
161 }
162
163 }
164
165 }
166
167
168
169 }
170
171
