Also ich bin grad dabei einen Spielautomaten zu programmieren der soll so funktionieren sobald ich den Knopf drücke sollen 3 Zufallszahlen generiert werden und wenn die erste beispielsweise 1 ist soll ein blauer Kreis gezeichnet werden wenn 2 blaue Kreise da sind soll da stehen Kleiner Gewinn mein Problem im Moment ist die Kreise werden nicht gezeichnet und wenn ich den Knopf drücke werden keine neuen Zufallszahlen generiert.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
public class zufall_mit_JFrame extends JFrame{
static int x,y,z;
public zufall_mit_JFrame(){
super("Zufall");
JLabel q = new JLabel("Großer Gewinn! 10€");
JLabel w = new JLabel("Kleiner Gewinn! 5€");
JLabel e = new JLabel("Kein Gewinn! 0€");
q.setSize(50, 20);
w.setSize(50, 20);
e.setSize(50, 20);
q.setBounds(170, 300, 500, 40);
w.setBounds(170, 300, 500, 40);
e.setBounds(170, 300, 500, 40);
this.setVisible(true);
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.add(new MyDraw());
JButton k = new JButton("Neues Spiel!");
k.setBounds(0, 300, 150, 50);
k.addActionListener(new Listener());
this.add(k);
if( x == y && y == z )
this.add(q);
else
if ( y == z || z == x || x == y)
this.add(w);
else
this.add(e);
}
public static void main(String[] args){
new zufall_mit_JFrame();
System.out.print(x);
System.out.print(y);
System.out.print(z);
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
z = (int) (Math.random()*3+1);
x = (int) (Math.random()*3+1);
y = (int) (Math.random()*3+1);
}
}
private class MyDraw extends JPanel {
public void paintComponent(Graphics stift) {
super.paintComponent(stift);
stift.setColor(Color.blue);
stift.fillOval(20, 50, 50, 50);
switch(z)
{
case(1):
stift.setColor(Color.blue);
stift.fillOval(20, 50, 50, 50);
break;
case(2):
stift.setColor(Color.green);
stift.fillOval(20, 50, 50, 50);
break;
case(3):
stift.setColor(Color.red);
stift.fillOval(20, 50, 50, 50);
break;
}
switch(x)
{
case(1):
stift.setColor(Color.blue);
stift.fillOval(70, 50, 50, 50);
break;
case(2):
stift.setColor(Color.green);
stift.fillOval(70, 50, 50, 50);
break;
case(3):
stift.setColor(Color.red);
stift.fillOval(70, 50, 50, 50);
break;
}
switch(y)
{
case(1):
stift.setColor(Color.blue);
stift.fillOval(120, 50, 50, 50);
break;
case(2):
stift.setColor(Color.green);
stift.fillOval(120, 50, 50, 50);
break;
case(3):
stift.setColor(Color.red);
stift.fillOval(120, 50, 50, 50);
break;
}
}
}
}