Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Pedro Gilbert   on May 16 In Java Category.

  
Question Answered By: Shiv Patel   on May 16

i have been working on it for a
few hours now, however I still cant get it to work!! What im trying
to make is a snake/tron type game, that will eventually have an AI
computer opponent! (hopefully!) heres the code  I have so far, I'd
appreciate it if anyone has the time to look over it for me to see
where im going wrong. I just cant see why the color  values dont
change as the "snake" moves in and out of the red section?! anyway,
heres the JAVA code:

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class snake extends Applet implements Runnable
{
int p1SpeedX = 0;
int p1SpeedY = 0;
int p1X = 10;
int p1Y = 10;
int appletWidth; // size of the applet - x
int appletHeight; // size of the applet - y
Image Buffer;
Graphics gBuffer;

public void init()
{
appletWidth = size().width;
appletHeight = size().height;

Buffer=createImage(appletWidth,appletHeight);
gBuffer=Buffer.getGraphics();

gBuffer.setColor(Color.black);
gBuffer.fillRect(0,0,100,size().height);


gBuffer.setColor(Color.red);
gBuffer.fillRect(100,0,size().width,size().height);

}

public void start ()
{
// creates a new thread
Thread th = new Thread (this);
// Starts the thread
th.start ();
}

public void stop()
{

}

public void destroy()
{

}

public boolean keyDown (Event e, int key)
{

if ((key == Event.LEFT)&&(p1SpeedX != 1))
{
p1SpeedX = -1;
p1SpeedY = 0;
}

else if ((key == Event.RIGHT)&&(p1SpeedX != -1))
{
p1SpeedX = 1;
p1SpeedY = 0;
}

else if ((key == Event.UP)&&(p1SpeedY != 1))
{
p1SpeedY = -1;
p1SpeedX = 0;
}
else if((key == Event.DOWN)&&(p1SpeedY != -1))
{
p1SpeedY = 1;
p1SpeedX = 0;
}
else
{
System.out.println ("Charakter: " + (char)
key + " Integer Value: " + key);
}

return true;
}

public void run ()
{

//Thread.currentThread().setPriority
(Thread.MIN_PRIORITY);


while (true)
{
try {Thread.sleep(5);}
catch (Exception e) { }


if (p1X > appletWidth)
{

p1X = 0;
}

else if (p1X < 0)
{

p1X = appletWidth;
}

if (p1Y > appletHeight)
{

p1Y = 0;
}

else if (p1Y < 0)
{

p1Y = appletHeight;
}

p1X += p1SpeedX;
p1Y += p1SpeedY;

repaint();

// ---- THIS SECTION BELOW IS THE BIT IM
HAVING PROBLEMS WITH
int[] pixel = new int[1];

PixelGrabber pg = new PixelGrabber(Buffer,
p1X, p1Y, 1, 1, pixel, 0, 1);
try
{
pg.grabPixels();
} catch (InterruptedException e)
{
System.err.println("interrupted
waiting for pixels!");
return;
}
ColorModel cm = pg.getColorModel();

gBuffer.setColor(Color.black);
gBuffer.fillRect(5,90,size().width,110);
gBuffer.setColor(Color.yellow);
gBuffer.drawString(" R: "+cm.getRed(0)+"
G: "+cm.getGreen(0)+" B: "+cm.getBlue(0)+"\n"+cm.toString(),10,100);

gBuffer.setColor(Color.yellow);
gBuffer.fillOval(p1X, p1Y, 1, 1); //the line
that the player controls


//Thread.currentThread().setPriority
(Thread.MAX_PRIORITY);
}
}

public void update(Graphics g)
{
paint(g);
}


public void paint(Graphics g)
{
g.drawImage (Buffer,0,0, this);
}

}




and heres the HTML code to run the applet:

<applet code="snake.class" width=500 height=500>
</applet>

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Image/Graphics color Or get search suggestion and latest updates.


Tagged: