Monday, 11 May 2015

Applet programming displaying chess board

Exported from Notepad++
import java.applet.*; import java.awt.*; public class Chess extends Applet { int x,y,dx,dy; public void paint(Graphics g) { x=1; y=1; dx=50; dy=50; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(i%2==0) { //this is for alternating colours if(j%2==0) { g.setColor(Color.red); } else { g.setColor(Color.BLACK); } } //the following conditions if and else makes the colours such that no colour //will have the same adjacent colour just like what chess board need else { if(j%2==0) { g.setColor(Color.BLACK); } else { g.setColor(Color.red); } } g.fillRect(x,y,dx,dy); //the below code makes to move right x=x+dx; } //here x=1 to bring the x coordinates of next shape to beginning of view y=y+dy; x=1; } } }


No comments:

Post a Comment