Hi: I'm working on a mini-game ish sort of applet using Eclipse. I am trying to get a picture I have in my folder of a dot to show on my applet. and be able to move it freely. Since I have other parts of the game planned out, parts of my code may seem not to make any sense. But I am fairly new to java, so I really after trying for days still have yet to figure out what to do. I have no more errors showing when I compile, but it's just a plan screen when I run.
I have 2 main parts to the project:
first is "Bullet.java" which contains a class designed to create an Ellipse2D and some things it will need to do in the game.
import java.awt.*;
import java.awt.geom.*;
public class Bullet {
private Image picture;
private int xpos, ypos, height, width, speed, xdir, ydir;
private Ellipse2D.Double c;
public Bullet(Image p, int x, int y, int w, int h, int s, int xd)
{
picture = p;
xpos = x;
ypos = y;
width = w;
height = h;
speed = s;
xdir = xd;
ydir = 0;
c = new Ellipse2D.Double (xpos, ypos, width, height);
}
public void move()
{
xpos = xpos + speed*xdir;
ypos = ypos + speed*ydir;
c.setFrame(xpos, ypos, width, height);
}
public int getX() { return xpos;}
public int getY() { return ypos ;}
public int getHeight() { return height; }
public int getWidth() { return width ;}
public int getXDir() { return xdir ;}
public int getYDir() { return ydir ;}
public void setPos(int a, int b) {xpos = a; ypos = b;}
public void setDir(int x, int y)
{
xdir = x;
ydir = y;
}
public boolean intersects(int x, int y, int w, int h)
{
return c.intersects(x, y, w, h);
}
public void displayBullet(Graphics2D g)
{
g.drawImage(picture, xpos, ypos, width, height, null);
}
public void paint(Graphics2D g)
{
displayBullet(g);
}
}
Next is myApplet.java which contains the actual painting, KeyListener, etc for Bullet.java.
import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class myApplet extends Applet implements KeyListener
{
BufferedImage imageBuffer;
Graphics2D graphicsBuffer;
private Bullet s;
private Timer t;
Image p;
public void init()
{
imageBuffer = (BufferedImage)createImage(getWidth(), getHeight());
graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();
p= getImage(getCodeBase(), "dot.jpg");
s=new Bullet(p, 20, 250, 50, 200, 20, 0);
addKeyListener(this);
setFocusable(true);
for(int j=0;j<5;j++)
{
for(int k=0;k<16;k++)
{
p= getImage(getCodeBase(), "dot.jpg");
}
}
ActionListener z = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
s.move();
}
};
paint(graphicsBuffer);
t=new Timer(10, z);
t.start();
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
s.setDir(0,1); //change direction of the paddle to positive
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{
s.setDir(0,-1);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
s.setDir(-1,0);
}
if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
{
s.setDir(1,0);
}
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
s.setDir(0,0);
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{
s.setDir(0,0);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
s.setDir(0,0);
}
if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
{
s.setDir(0,0);
}
}
public void keyTyped(KeyEvent e) {}
public int width = 3000;
public int height= 300;
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
graphicsBuffer.setColor(Color.white);
graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());
s.paint(graphicsBuffer);
g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
My Images are in the right folders for my project yet still, I can't get anything other then a blank screen. Your help is greatly appreciated.
Thanks