java - 2D Shooter using JPanel/JFrame. drawImage (bullet) on screen with it's own velocity -
here have:
import java.awt.color; import java.awt.component; import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.image.imageobserver; import java.util.arraylist; import javax.swing.jpanel; public class gamepanel extends jpanel implements actionlistener, keylistener, imageobserver { public image ship; public image enemy; private int xloc; private int yloc = 180; private int xloc2 = 700; private int yloc2 = 180; private int xvel; private int yvel; private int xvel_en; private int xvel_sh = 6; private imageobserver observer = null; private arraylist<base> shield = new arraylist<base>(); private image shieldpiece; private shot shot; public gamepanel() { ship = toolkit.getdefaulttoolkit().createimage("ship3.png"); enemy = toolkit.getdefaulttoolkit().createimage("alien-ship-1.png"); shieldpiece = toolkit.getdefaulttoolkit().createimage("block2.png"); for(int x = 500; x < 680; x = x + 18) for(int y = 60; y < 370; y = y + 18) { base temp = new base(x,y); shield.add(temp); } } public void paint(graphics g){ super.paint(g); g.drawimage(ship, xloc, yloc, null); g.drawimage(enemy, xloc2, yloc2, null); for(base b: shield) g.drawimage(shieldpiece, b.getshieldx(), b.getshieldy(), null); } @override public void actionperformed(actionevent arg0) { // todo auto-generated method stub move(); setbackground(color.black); repaint(); } private void move() { int endofwindow = getwidth(); int endofwindow1 = getheight(); if(xloc>=(endofwindow - ship.getwidth(null))|| xloc<0) xvel = -xvel; xloc +=xvel; if(xloc>=(endofwindow - ship.getwidth(null))) xvel = -xvel; xloc +=xvel; if(yloc>=(endofwindow1 - ship.getheight(null)) || yloc<0) yvel = -yvel; yloc +=yvel; if(yloc>=(endofwindow1 - ship.getheight(null))) yvel = -yvel; yloc +=yvel; } @override public void keypressed(keyevent arg0) { // todo auto-generated method stub if(arg0.getkeycode() == keyevent.vk_d) xvel = 3; if(arg0.getkeycode() == keyevent.vk_a) xvel = -3; if(arg0.getkeycode() == keyevent.vk_w) yvel = -3; if(arg0.getkeycode() == keyevent.vk_s) yvel = 3; if(arg0.getkeycode() == keyevent.vk_right) xvel = 3; if(arg0.getkeycode() == keyevent.vk_left) xvel = -3; if(arg0.getkeycode() == keyevent.vk_up) yvel = -3; if(arg0.getkeycode() == keyevent.vk_down) yvel = 3; if(arg0.getkeycode() == keyevent.vk_space) { shot shoot = new shot(xvel_sh, xloc, yloc); shoot.paint(); } } @override public void keyreleased(keyevent arg0) { // todo auto-generated method stub if(arg0.getkeycode() == keyevent.vk_d || arg0.getkeycode() == keyevent.vk_a) xvel = 0; if(arg0.getkeycode() == keyevent.vk_w || arg0.getkeycode() == keyevent.vk_s) yvel = 0; if(arg0.getkeycode() == keyevent.vk_right || arg0.getkeycode() == keyevent.vk_left) xvel = 0; if(arg0.getkeycode() == keyevent.vk_up || arg0.getkeycode() == keyevent.vk_down) yvel = 0; } @override public void keytyped(keyevent arg0) { // todo auto-generated method stub } }
and shot class:
import java.awt.graphics; import java.awt.image; import java.awt.toolkit; public class shot{ private int xvel_sh; private int xcoord; private int ycoord; private image shot; private graphics g; public shot(int vel, int xloc, int yloc) { vel = xvel_sh; xcoord = xloc; ycoord = yloc; shot = toolkit.getdefaulttoolkit().createimage("shot4.png"); } public void setshotvel(int shotvel) { xvel_sh = shotvel; } public void paint() { g.drawimage(shot, xcoord, ycoord, null); } }
i have set when hit space, should create shot object, call paint() method in shot class. shot object created @ xloc , yloc (from gamepanel class - location of "ship" or player).
however, lot of errors, starting nullpointerexception when hit space. game not crash, keeps coming null pointer exception.' followed error:
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ shot.paint(shot.java:25) @ gamepanel.keypressed(gamepanel.java:104) @ java.awt.component.processkeyevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.window.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.keyboardfocusmanager.redispatchevent(unknown source) @ java.awt.defaultkeyboardfocusmanager.dispatchkeyevent(unknown source) @ java.awt.defaultkeyboardfocusmanager.predispatchkeyevent(unknown source) @ java.awt.defaultkeyboardfocusmanager.typeaheadassertions(unknown source) @ java.awt.defaultkeyboardfocusmanager.dispatchevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
not sure why not being drawn...can help? also, need implement object collisions (with shield class not shown here)...so if has direction towards tutorials on that? :p
g
never initialized in shot
, when call g.drawimage
throws nullpointerexception
.
Comments
Post a Comment