CopyPastehas never been so tasty!

Java Brettspiel

by anonymous

  • 0
  • 0
  • 0
157 views

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class Game extends JPanel implements ActionListener{
 
    private static final long serialVersionUID = 1L;
    JFrame fenster;
    JButton wuerfeln;
    int position;
    boolean gewonnen;
    Rectangle spielfelder [];
    Color farbe;
    Abfragen abfragenFenster;
 
    public static void main(String[] args){
    Game verweiß = new Game();
    }
 
    public Game(){
    gewonnen = false;
    farbe = Color.black;
    position = 0;
    abfragenFenster = new Abfragen(this);
 
    spielfelder = new Rectangle[24];
    spielfelderErzeugen();
 
    fenster = new JFrame("Babaras - Birthday - Game");
    fenster.setSize(1100, 900);
    fenster.setLocationRelativeTo(null);
    fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fenster.setResizable(false);
    fenster.setLayout(new BorderLayout());
    fenster.add(this, BorderLayout.CENTER);
 
    wuerfeln = new JButton("Würfeln");
    wuerfeln.addActionListener(this);
    fenster.add(wuerfeln, BorderLayout.SOUTH);
 
    fenster.setVisible(true);
    }
 
    public void spielfelderErzeugen(){
    int groesse = 50;
    for(int i = 0; i < 24; i++){
    spielfelder[i] = new Rectangle(groesse, groesse);
    }
    for(int i = 0; i < 24; i++){
    if(i < 10)
    spielfelder[i].setLocation(i*groesse, 0);
    else if(i < 20)
    spielfelder[i].setLocation((i-10)*groesse, 150);
    else if(i < 24)
    spielfelder[i].setLocation((i-20)*groesse, 300);
    }
    }
 
    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
 
    for(int i = 0; i < 24; i++){
    g2d.draw(spielfelder[i]);
    }
 
    g2d.setColor(farbe);
    g2d.fill(spielfelder[position]);
 
    if(gewonnen){
    g2d.drawString("Herzlichen Glückwunsch \nSie haben gewonnen!", 550, 450);
    }
    }
 
    public void spielerBewegen(int gewuerfelt) {
fenster.toFront();
if(position+gewuerfelt >= 23){
position = 23;
gewonnen = true;
}
else{
position = position + gewuerfelt;
}
 
if ((position == 6) || (position == 8) || (position == 10) || (position == 12) || 
(position == 16) || (position == 17) || (position == 20) || (position == 21) || (position == 22)){
farbe = Color.red;
}
 
repaint();
}
 
public void actionPerformed(ActionEvent e) {
farbe = Color.black;
int gewuerfelt =  (int) (Math.random()*6+1);
if ((position == 6) || (position == 8) || (position == 10) || (position == 12) || 
(position == 16) || (position == 17) || (position == 20) || (position == 21) || (position == 22)){
abfragenFenster.setGewuerfelt(gewuerfelt);
abfragenFenster.setVisible(true);
fenster.setEnabled(false);
}
else{
spielerBewegen(gewuerfelt);
}
}
}

 

Add A Comment: