Hab ein Problem !! iCH programmiere das Spiel Dame und ich hab die Graue Bausteine als 1 zugewiesen und wenn ich 1 umranden will also sprich Grau, umrander er die linke 3 spalten anstatt die oberen 3! woran liegt der fehler!hier der code!danke!
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 [][] 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){
x=e.getX();
y=e.getY();
System.out.println(x+" ; "+y);
xIndex(x);
yIndex(y);
umrandengrau();
for(int ui=0;ui<=7;ui++){
for(int u=0;u<=7;u++){
if(a[ui][u]==1){
System.out.println(ui+" ; "+u+"=1");
}if(a[ui][u]==2){
System.out.println(ui+" ; "+u+"=2");
}
}}
}
});
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[ui][u]=1;
//grauer Stein (1)
}if(ui>4) {
a[ui][u]=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[t][i]==1){ //grauer Stein zeichnen
g.setColor(Color.GRAY);
g.fillOval(i*50+8,t*50+8,44,44);
}
if(a[t][i]==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);
}
}
public static void main(String[] args) {
dame d=new dame();
d.setSize(700,500);
d.setVisible(true);
}
}