Also ich hab einen Spielautomat versucht zu programmieren der funktioniert so es werde 3 Zahlen generiert falls die 3 Zahlen gleich sind hat man gewonnen. Graphisch wollte ich das so umsetzen das für jede Zahl ein bestimmter Kreis in einer Farbe gezeichnet wird allerdings funktioniert das ganze nicht so richtig.
Java:
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.JFrame;
import javax.swing.JLabel;
public class zufall extends JFrame implements ActionListener {
JFrame jf = new JFrame();
static int x,y,z;
JButton k = new JButton("Neues Spiel!");
public zufall() {
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setSize(400,400);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setLayout(null);
JLabel q = new JLabel();
if( x == y && y == z )
q.setText("Großer Gewinn!");
else
if ( y == z || z == x || x == y)
q.setText("Kleiner Gewinn!");
else
q.setText("Kein Gewinn");
k.setSize(150,60);
k.setLocation(0,310);
k.addActionListener(this);
add(k);
}
public static void main(String args[]){
zufall z1 = new zufall();
z = (int) (Math.random()*3+1);
x = (int) (Math.random()*3+1);
y = (int) (Math.random()*3+1);
}
public void actionPerformed(ActionEvent e) {
k.repaint();
}
public void paintComponent(Graphics stift){
jf.paintComponents(stift);
switch(z)
{
case(1):
stift.setColor(Color.blue);
stift.fillOval(120, 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;
}
}
}