/*
By "reverts back" I mean that the green lines which are drawn at the beginning are drawn again,
after the if statement completes. Which is strange because the if statement is clearing the screen
and then drawing something new and yet as soon as it completes the whole scene which I draw before
the if statement is drawn again. Almost all the variables I'm using (at least all the ones I'm sure
would affect the if statement) are declared locally.
<applet code=Drawing4Applet width=600 height=600>
</applet>
*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Drawing4Applet extends Applet implements KeyListener{
int PlayerX = 250; // start here vs the corner
int PlayerY = 200;
public void init() {
this.addKeyListener(this);
resize(501, 502);
}
public void paint(Graphics g) {
System.out.println(">>>Entered paint at " + System.currentTimeMillis());
int GrassX = 5;
int GrassY = 5;
int TransX = 200;
int TransY = 200;
int TransX2 = 50;
int TransY2 = 50;
int BoardsX = 5;
int BoardsY = 5;
int GrassHere = 0;
g.setColor(Color.green);
while(GrassX < 500)
{
g.drawLine(GrassX, 0, GrassX, 500);
GrassX = GrassX + 5;
}
while(GrassY < 500)
{
g.drawLine(0, GrassY, 500, GrassY);
GrassY = GrassY + 5;
}
g.setColor(Color.black);
g.drawLine(0,0,500,0);
g.drawLine(500,500,500,0);
g.drawLine(500,500,0,500);
g.drawLine(0,500,0,0);
g.fillRect(225, 0, 50, 200);
g.setColor(Color.white);
g.fillRect(240, 180, 20, 20);
g.setColor(Color.red);
g.setFont(new Font(g.getFont().getName(),Font.PLAIN,25));
g.drawString("B",242,20);
g.drawString("L",242,40);
g.drawString("D",242,60);
g.drawString("G",242,80);
g.drawString("1",242,100);
g.setColor(Color.red);
g.fillRect(PlayerX, PlayerY, 10, 10);
if(PlayerX == 240 && PlayerY == 180 || PlayerX == 240 && PlayerY == 190
|| PlayerX == 250 && PlayerY == 180 || PlayerX == 250 && PlayerY == 190) {
System.out.println("in if x=" + PlayerX + ", y=" + PlayerY + " at " + System.currentTimeMillis());
g.setColor(Color.white);
try{
while(TransX > -1) {
Thread.sleep(75);
g.fillRect(TransX, TransY, TransX2, TransY2);
TransX = TransX - 10;
TransY = TransY - 10;
TransX2 = TransX2 + 25;
TransY2 = TransY2 + 25;
}
}catch(Exception e){};
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);
g.drawLine(0,0,500,0);
g.drawLine(500,500,500,0);
g.drawLine(500,500,0,500);
g.drawLine(0,500,0,0);
g.setColor(new Color( 192, 128, 64));
while(BoardsX < 500) {
g.drawLine(BoardsX, 0, BoardsX, 500);
BoardsX = BoardsX + 5;
}
while(BoardsY < 500) {
g.drawLine(0, BoardsY, 500, BoardsY);
BoardsY = BoardsY + 5;
}
g.setColor(Color.red);
g.fillRect(PlayerX, PlayerY, 10, 10);
System.out.println(" >>Exiting if at " + System.currentTimeMillis());
} // end if()
System.out.println("after end of if at " + System.currentTimeMillis());
}
public void keyPressed(KeyEvent arg0){}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased at " + System.currentTimeMillis());
char key = e.getKeyChar();
if(key == 'w')
{
PlayerY = PlayerY - 10;
}
if(key == 'a')
{
PlayerX = PlayerX - 10;
}
if(key == 's')
{
PlayerY = PlayerY + 10;
}
if(key == 'd')
{
PlayerX = PlayerX + 10;
}
repaint();
}
public void keyTyped(KeyEvent arg0){}
}