hey!!hab ein problem!! Ich programmiere ein Dame spiel und ich will beim ersten klick dass er den weißen stein umrandet und beim zweiten klick dass er den neuen stein setzt! er macht aber alles zusammen gleichzeitg! kommisch ich habs mit boolean variablen versucht aber kreigs net hin kannst mir gelfen hier der code!
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class dame extends Frame {
Canvas cv;
int x,y;
int xi,yi;
int aus = 1;
boolean sos = true;
int [][] a= new int [8][8];
public dame(){
setLayout (new FlowLayout());
Button bt= new Button("Spiel los");
cv=new Canvas();
cv.setSize(450,450);
add(cv);
add(bt);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
aufstellung();
}
});
cv.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent e){
boolean possi=true;
x=e.getX();
y=e.getY();
xIndex(x);
yIndex(y);
if(possi==true){
aufstellung();
umrandenweiss();
possi=false;
}if(possi==false){
setzen();
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
})
;
}
private void aufstellung(){
boolean auf =false;
Graphics g;
g=cv.getGraphics();
for(int ui=0;ui<=7;ui++){
for(int u=0;u<=7;u++){
//Steine auf Anfang setzen
if(auf==true){
if(ui<3){
a[u][ui]=1;
//grauer Stein (1)
}if(ui>4) {
a[u][ui]=2;
//weißer Stein (2)
}
auf=false;
}else{
auf=true;
}
}if(ui%2 != 0){
auf=false;
}else{auf=true;}
}
//Kästchen zeichnen
for(int s=0;s<=7;s++){
for(int z=0;z<=7;z++){
g.drawRect(5+s*50,5+z*50,50,50);
if(auf==true){
g.fillRect(5+s*50,5+z*50,50,50);
auf=false;
}else {
auf=true;
}}
if(s%2==0){
auf=true;
}else{
auf=false;
}
}
for(int t=0;t<=7;t++){
for(int i=0;i<=7;i++){
if(a[i][t]==1){ //grauer Stein zeichnen
g.setColor(Color.GRAY);
g.fillOval(i*50+8,t*50+8,44,44);
}
if(a[i][t]==2){ //weißer Stein zeichnen
g.setColor(Color.WHITE);
g.fillOval(i*50+8,t*50+8,44,44);
;
}}
}
}
private int xIndex(int x){
System.out.println("x="+(x-5)/50);
return (x-5)/50;
}
private int yIndex(int y){
System.out.println("y="+(y-5)/50);
return (y-5)/50;
}
private int xPos(int xi){
return (xi*50)+5;
}
private int yPos(int yi){
return (yi*50)+5;
}
private void umrandengrau(){
Graphics g;
g=cv.getGraphics();
g.setColor(Color.RED);
xi=xIndex(x);
yi=yIndex(y);
if(a[xi][yi]==1){
g.drawRect(xPos(xi),yPos(yi),50,50);
}
}
private void umrandenweiss(){
Graphics g;
g=cv.getGraphics();
g.setColor(Color.RED);
xi=xIndex(x);
yi=yIndex(y);
aus=0;
if(a[xi][yi]==2){
g.drawRect(xPos(xi),yPos(yi),50,50);
}
}
private void setzen(){
Graphics g;
g=cv.getGraphics();
xi=xIndex(x);
yi=yIndex(y);
g.setColor(Color.GRAY);
g.fillOval(xPos(xi)+3,yPos(yi)+3,44,44);
}
public static void main(String[] args) {
dame d=new dame();
d.setSize(700,500);
d.setVisible(true);
d.aufstellung();
}
}