Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with program
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nickster




PostPosted: Fri Jan 14, 2011 1:58 pm   Post subject: Need help with program

Hi guys i need help with my program for my school assignment please can you help me fix it. i have 3 files called shooter, bullets, and player.

package shooter;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Image.*;




public class shooter extends JFrame implements KeyListener{
static Object shooter;


static class img1 {
Image img1;

public img1() {
}
}
Image img1;
Graphics dbi;

boolean w,s;
int S;

private Graphics g;

player p1=new player(5, 150, 10, 50, "Images/LEFT.gif");
ArrayList<Bullets>b=new ArrayList<Bullets>();
public shooter (){
setTitle("Arcade shooting game");
setSize(600, 400);
setResizable(false);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
w=s=false;
S=0;

setVisible(true);
}
public void paint(){
img1=createImage(getWidth(), getHeight());
dbi=img1.getGraphics();


paintComponent(dbi);
g.drawImage(img1, 0, 0, this);
repaint();

}

public void paintComponent(Graphics g){
if (p1.health>0){
for(Bullets b1:b){
b1.draw(g);
}
update();
}
else{
if(p1.health<=0){
g.setColor(p1.col);
g.drawString("You have just lost the game, you suck!" , 250, 190);
}
}
p1.draw(g);
}

public void update(){
if(w&&p1.y>24)p1.moveUp();
if(s&&p1.y<347)p1.moveDown();
if(S==1){
Bullets add=p1.getBullets();
add.xVel=3;
b.add(add);
S++;
}
for (int x=0; x<b.size();x++){
b.get(x).move();
if(b.get(x).rect.intersects(p1.rect)&&b.get(x).xVel<0){
p1.health--;
b.remove(x);
x--;
continue;
}
}
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_W:w=true;break;
case KeyEvent.VK_S:s=true;break;
case KeyEvent.VK_SPACE:S++;break;

}
}
public void keyReleased(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_W:w=false;
case KeyEvent.VK_S:s=false;
case KeyEvent.VK_SPACE:S=0;break;
}
}

ImageIcon image = new ImageIcon("jpg");



public static void main(String[]beans){
KeyListener s = new shooter();
}

}



package shooter;


import java.awt.*;

public class Bullets {
int x, y;
int xVel;
int height, width;
Rectangle rect;
Color col;

public Bullets(){
x=y=height=width=0;
col=Color.WHITE;
rect=new Rectangle(x, y, width, height);
}
public Bullets(int x, int y, int wd, int ht, Color c){
this.x=x;
this.y=y;
this.xVel=0;
height=ht;
width=wd;
col=c;
rect=new Rectangle(x, y, width, height);
}
public Bullets(int x, int y, int wd, int ht, int xVel, Color c){
this.x=x;
this.y=y;
this.xVel=xVel;
height=ht;
width=wd;
col=c;
rect=new Rectangle(x, y, width, height);
}
public void draw(Graphics g){
g.setColor(col);
g.fillOval(x, y, width, height);
}
public void move(){
x+=xVel;
rect.setLocation(x, y);
}
}




package shooter;

import java.awt.*;
import java.awt.Image.*;

public class player {
int x,y;
int height,width;
int health;
Image img1;
Rectangle rect;
Color col;
private Object Shipfighter_copy;


public player(){
x=y=height=width=0;
img1=null;
col=Color.WHITE;
health=5;
rect=new Rectangle(x, y, width, height);

}

public player(int x, int y){
this.x=x;
this.y=y;
height=width=0;
img1=null;
col=Color.WHITE;
health=5;
rect=new Rectangle(x, y, width, height);
}

public player(int x, int y, int ht, int wd){
this.x=x;
this.y=y;
height=ht;
width=wd;
img1=null;
col=Color.WHITE;
health=5;
rect=new Rectangle(x, y, width, height);
}

public player(int x, int y, int ht, int wd,String s){
this.x=x;
this.y=y;
height=ht;
width=wd;
health=5;
col=Color.WHITE;
img1=Toolkit.getDefaultToolkit().getImage(s);
rect=new Rectangle(x, y, width, height);
}

public void draw(Graphics g){
g.drawImage(img1,x,y,null);

}
public void setImage(String s){
img1=Toolkit.getDefaultToolkit().getImage(s);
}
public void moveUp(){
y-=3;
rect.setLocation(x, y);
}
public void moveDown(){
y+=3;
rect.setLocation(x, y);
}
public Bullets getBullets(){
return new Bullets(x+8, y+23, 3, 3, col);
}

public class image {
img1 = new Image("image/Shipfighter_copy.jpg") {};

shooter.shooter.img = img1.getImage();

}
}

Please help me with this code and thx Very Happy
Sponsor
Sponsor
Sponsor
sponsor
2goto1




PostPosted: Fri Jan 14, 2011 2:02 pm   Post subject: RE:Need help with program

Help how?
Tony




PostPosted: Fri Jan 14, 2011 2:02 pm   Post subject: RE:Need help with program

What do you need fixed, and why is that a problem?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nickster




PostPosted: Wed Jan 19, 2011 8:14 am   Post subject: RE:Need help with program

i need help imorting a jpg into the program so it will work cause right now all i get when i run it is a grey screen
Tony




PostPosted: Wed Jan 19, 2011 12:01 pm   Post subject: RE:Need help with program

you should probably be reading the documentation on java.awt.Image
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: