|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have five objects of the same class
b1 b2 b3 b4 b5 i want to destroy 1 of them by selecting it and pressing the destroy (vernietig) button but when i select b3 in example and press the destroy button then not only b3 is destroyed but also b4 and b5 and the other things i've drawn with (Graphics g). the buttons stay and b1 and b2 too. who can help me out. thanks in advance I don't know if it's allowed to post such large things here, if not then i'm sorry my program : import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class ballonspel extends Applet implements ActionListener { private Button groei, krimp, links, rechts, onder, boven, selecteer, vernietig; private Ballon b1, b2, b3, b4, b5; private TextField veld; private int selectie; private Achtergrond grond; public void init() { resize(800,600); grond = new Achtergrond(); groei = new Button("Groei"); add(groei); groei.addActionListener(this); krimp = new Button("Krimp"); add(krimp); krimp.addActionListener(this); links = new Button("Links"); add(links); links.addActionListener(this); rechts = new Button("Rechts"); add(rechts); rechts.addActionListener(this); //knop om de ballonnen naar onder te bewegen onder = new Button("Onder"); add(onder); onder.addActionListener(this); //knop om de ballonnen naar boven te bewegen boven = new Button("Boven"); add(boven); boven.addActionListener(this); //knop om de ballon met nummer van de waarde in het textveld te selecteren selecteer = new Button("Selecteer"); add(selecteer); selecteer.addActionListener(this); //textveld om nummer van een ballon op te geven om te selecteren veld = new TextField(5); add(veld); veld.addActionListener(this); //knop om de geselecteerde ballon(nen) te vernietigen vernietig = new Button("Vernietig"); add (vernietig); vernietig.addActionListener(this); b1 = new Ballon(60,50,10, new Color(255,100,83)); b2 = new Ballon(160,50,10, new Color(234,152,78)); b3 = new Ballon(260,50,10, new Color(67,123,255)); b4 = new Ballon(360,50,10, new Color(98,234,62)); b5 = new Ballon(460,50,10, new Color(111,234,154)); } public void actionPerformed(ActionEvent e) { if (e.getSource() == groei) { if (selectie == 1) { b1.groei(); b1.donkerder(); } if (selectie == 2) { b2.groei(); b2.donkerder(); } if (selectie == 3) { b3.groei(); b3.donkerder(); } if (selectie == 4) { b4.groei(); b4.donkerder(); } if (selectie == 5) { b5.groei(); b5.donkerder(); } if (selectie <= 0 | | selectie >= 6) { b1.groei(); b2.groei(); b3.groei(); b4.groei(); b5.groei(); b1.donkerder(); b2.donkerder(); b3.donkerder(); b4.donkerder(); b5.donkerder(); } } if (e.getSource() == krimp) { if (selectie == 1) { b1.krimp(); b1.lichter(); } if (selectie == 2) { b2.krimp(); b2.lichter(); } if (selectie == 3) { b3.krimp(); b3.lichter(); } if (selectie == 4) { b4.krimp(); b4.lichter(); } if (selectie == 5) { b5.krimp(); b5.lichter(); } if (selectie <= 0 | | selectie >= 6) { b1.krimp(); b2.krimp(); b3.krimp(); b4.krimp(); b5.krimp(); b1.lichter(); b2.lichter(); b3.lichter(); b4.lichter(); b5.lichter(); } } if (e.getSource() == links) { if (selectie == 1) { b1.links(); } if (selectie == 2) { b2.links(); } if (selectie == 3) { b3.links(); } if (selectie == 4) { b4.links(); } if (selectie == 5) { b5.links(); } if (selectie <= 0 | | selectie >=6) { b1.links(); b2.links(); b3.links(); b4.links(); b5.links(); } } if (e.getSource() == rechts) { if (selectie == 1) { b1.rechts(); } if (selectie == 2) { b2.rechts(); } if (selectie == 3) { b3.rechts(); } if (selectie == 4) { b4.rechts(); } if (selectie == 5) { b5.rechts(); } if (selectie <= 0 | | selectie >=6) { b1.rechts(); b2.rechts(); b3.rechts(); b4.rechts(); b5.rechts(); } } if (e.getSource() == onder) { if (selectie == 1) { b1.onder(); } if (selectie == 2) { b2.onder(); } if (selectie == 3) { b3.onder(); } if (selectie == 4) { b4.onder(); } if (selectie == 5) { b5.onder(); } if (selectie <= 0 | | selectie >=6) { b1.onder(); b2.onder(); b3.onder(); b4.onder(); b5.onder(); } } if (e.getSource() == boven) { if (selectie == 1) { b1.boven(); } if (selectie == 2) { b2.boven(); } if (selectie == 3) { b3.boven(); } if (selectie == 4) { b4.boven(); } if (selectie == 5) { b5.boven(); } if (selectie <= 0 | | selectie >=6) { b1.boven(); b2.boven(); b3.boven(); b4.boven(); b5.boven(); } } if (e.getSource() == selecteer) { selectie = Integer.parseInt(veld.getText()); } if (e.getSource() == vernietig) { if (selectie == 1) { b1 = null; Ballon.aantal = Ballon.aantal - 1; } if (selectie == 2) { b2 = null; Ballon.aantal = Ballon.aantal - 1; } if (selectie == 3) { b3 = null; Ballon.aantal = Ballon.aantal - 1; } if (selectie == 4) { b4 = null; Ballon.aantal = Ballon.aantal - 1; } if (selectie == 5) { b5 = null; Ballon.aantal = Ballon.aantal - 1; } if (selectie <= 0 | | selectie >=6) { b1 = null; b2 = null; b3 = null; b4 = null; b5 = null; Ballon.aantal = Ballon.aantal - 5; } } repaint(); } public void paint(Graphics g) { int aantalBallon = Ballon.aantal; b1.display(g); b2.display(g); b3.display(g); b4.display(g); b5.display(g); grond.display(g); g.setColor(Color.black); g.drawString("De afstand tussen de twee verst van elkaar afliggende ballonnen is : " + afstand() ,50,530); Ballon.toonAantal(g); } public int afstand() { int a12 = (int)Math.sqrt(((b1.x - b2.x) * (b1.x - b2.x)) + ((b1.y - b2.y) *(b1.y - b2.y))); int a13 = (int)Math.sqrt(((b1.x - b3.x) * (b1.x - b3.x)) + ((b1.y - b3.y) *(b1.y - b3.y))); int a14 = (int)Math.sqrt(((b1.x - b4.x) * (b1.x - b4.x)) + ((b1.y - b4.y) *(b1.y - b4.y))); int a15 = (int)Math.sqrt(((b1.x - b5.x) * (b1.x - b5.x)) + ((b1.y - b5.y) *(b1.y - b5.y))); int a23 = (int)Math.sqrt(((b2.x - b3.x) * (b2.x - b3.x)) + ((b2.y - b3.y) *(b2.y - b3.y))); int a24 = (int)Math.sqrt(((b2.x - b4.x) * (b2.x - b4.x)) + ((b2.y - b4.y) *(b2.y - b4.y))); int a25 = (int)Math.sqrt(((b2.x - b5.x) * (b2.x - b5.x)) + ((b2.y - b5.y) *(b2.y - b5.y))); int a34 = (int)Math.sqrt(((b3.x - b4.x) * (b3.x - b4.x)) + ((b3.y - b4.y) *(b3.y - b4.y))); int a35 = (int)Math.sqrt(((b3.x - b5.x) * (b3.x - b5.x)) + ((b3.y - b5.y) *(b3.y - b5.y))); int a45 = (int)Math.sqrt(((b4.x - b5.x) * (b4.x - b5.x)) + ((b4.y - b5.y) *(b4.y - b5.y))); int geenAfstand = 0; if (a12 >= a13 && a12 >= a14 && a12 >= a15 && a12 >= a23 && a12 >= a24 && a12 >= a25 && a12 >= a34 && a12 >= a35 && a12 >= a45) { return a12; } if (a13 >= a12 && a13 >= a14 && a13 >= a15 && a13 >= a23 && a13 >= a24 && a13 >= a25 && a13 >= a34 && a13 >= a35 && a13 >= a34) { return a13; } if (a14 >= a12 && a14 >= a13 && a14 >= a15 && a14 >= a23 && a14 >= a24 && a14 >= a25 && a14 >= a34 && a14 >= a35 && a14 >= a45) { return a14; } if (a15 >= a12 && a15 >= a13 && a15 >= a14 && a15 >= a23 && a15 >= a24 && a15 >= a25 && a15 >= a34 && a15 >= a35 && a15 >= a45) { return a15; } if (a23 >= a12 && a23 >= a13 && a23 >= a14 && a23 >= a15 && a23 >= a24 && a23 >= a25 && a23 >= a34 && a23 >= a35 && a23 >= a45) { return a23; } if (a24 >= a12 && a24 >= a13 && a24 >= a14 && a24 >= a15 && a24 >= a23 && a24 >= a25 && a24 >= a34 && a24 >= a35 && a24 >= a45) { return a24; } if (a25 >= a12 && a25 >= a13 && a25 >= a14 && a25 >= a15 && a25 >= a23 && a25 >= a24 && a25 >= a34 && a25 >= a35 && a25 >= a45) { return a25; } if (a34 >= a12 && a34 >= a13 && a34 >= a14 && a34 >= a15 && a34 >= a23 && a34 >= a24 && a34 >= a25 && a34 >= a35 && a34 >= a45) { return a34; } if (a35 >= a12 && a35 >= a13 && a35 >= a14 && a35 >= a15 && a35 >= a23 && a35 >= a24 && a35 >= a25 && a35 >= a34 && a35 >= a45) { return a35; } if (a45 >= a12 && a45 >= a13 && a45 >= a14 && a45 >= a15 && a45 >= a23 && a45 >= a24 && a45 >= a25 && a45 >= a34 && a45 >= a35) { return a45; } else return geenAfstand; } } class Ballon { public int diameter; public int x; public int y; private Color kleur; int rood; int groen; int blauw; public static int aantal = 0; //Constructor method om het object van de class Ballon te initialiseren public Ballon(int x,int y,int diameter, Color kleur) { this.x = x; this.y = y; this.diameter = diameter; this.kleur = kleur; rood = kleur.getRed(); groen = kleur.getGreen(); blauw = kleur.getBlue(); plusEen(); } //de methode plusEen public static void plusEen() { aantal++; } public static void toonAantal(Graphics g) { g.drawString("Aantal gemaakte ballonnen is : " + aantal,50,560); } public void display(Graphics g) { g.setColor(kleur); g.fillOval(x,y,diameter,diameter); g.setColor(Color.black); g.drawOval(x,y,diameter,diameter); //Hier worden de stippen gemaakt g.setColor(Color.yellow); g.fillOval(x + diameter / 4, y + diameter / 2, diameter / 5,diameter / 5); if (diameter >= 30 && diameter <= 60) { g.fillOval(x + diameter / 2, y + diameter / 3, diameter / 4,diameter / 4); } if (diameter >60 && diameter <=90) { g.fillOval(x + diameter / 2, y + diameter / 5, diameter / 5,diameter / 5); g.fillOval(x + diameter / 8, y + diameter / 6, diameter / 3,diameter / 3); } if (diameter >90 && diameter <= 120) { g.fillOval(x + diameter / 2, y + diameter / 5, diameter / 6,diameter / 6); g.fillOval(x + diameter / 8, y + diameter / 6, diameter / 4,diameter / 4); g.fillOval(x + diameter / 5, y + ((diameter / 1) - (diameter / 4)) , diameter / 7,diameter / 7); } if (diameter >120 ) { g.fillOval(x + diameter / 2, y + diameter / 5, diameter / 6,diameter / 6); g.fillOval(x + diameter / 8, y + diameter / 6, diameter / 4,diameter / 4); g.fillOval(x + diameter / 5, y + ((diameter / 1) - (diameter / 4)) , diameter / 7,diameter / 7); g.fillOval(x + ((diameter / 1) - (diameter / 3)), y + diameter / 6, diameter / 8,diameter / 8); } } public void groei() { diameter = diameter +5; } public void krimp() { diameter = diameter -5; } public void links() { x = x -5; } public void rechts() { x = x +5; } //method om de ballonnen naar onder te bewegen public void onder() { y = y +5; } //method om de ballonnen naar boven te bewegen public void boven() { y = y -5; } //method om de ballonen lichter te maken van kleur public void lichter() { if (rood >= 10 && groen >= 10 && blauw >= 10) { rood = rood -10; groen = groen -10; blauw = blauw -10; } kleur = new Color(rood, groen, blauw); } //method om de ballonnen donkerder te maken van kleur public void donkerder() { if (rood <= 245 && groen <= 245 && blauw <= 245) { rood = rood +10; groen = groen +10; blauw = blauw +10; } kleur = new Color(rood, groen, blauw); } } //Klasse voor een achtergrond class Achtergrond { public void display(Graphics g) { g.setColor(Color.green); g.fillRect(0,500,800,100); g.fillOval(80,470,80,60); g.fillOval(180,450,100,100); g.fillOval(460,460,120,80); g.fillOval(700,420,160,160); g.setColor(Color.yellow); g.fillOval(600,100,150,150); g.setColor(Color.magenta); g.fillRect(330,400,60,100); g.fillOval(330,370,60,60); g.fillOval(340,340,40,40); g.setColor(Color.blue); g.fillRect(130,400,60,100); g.fillOval(130,370,60,60); g.fillOval(140,340,40,40); g.setColor(Color.yellow); g.fillRect(630,400,60,100); g.fillOval(630,370,60,60); g.fillOval(640,340,40,40); g.setColor(Color.cyan); g.fillOval(100,0,500,50); } } |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > destroying objects |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|