java - Using an ArrayList to draw images (JPanel, JFrame) -
i having trouble using arraylist draw multiple images within jpanel/jframe. project create yar's revenge type game. far, no near 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; public image shot; 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; private imageobserver observer; private arraylist<base> shield = new arraylist<base>(); private image shieldpiece; public gamepanel() { ship = toolkit.getdefaulttoolkit().createimage("ship3.png"); enemy = toolkit.getdefaulttoolkit().createimage("alien-ship-1.png"); shot = toolkit.getdefaulttoolkit().createimage("shot4.png"); shieldpiece = toolkit.getdefaulttoolkit().createimage("block1.png"); for(int x = 300; x < 420; x = x + 30) for(int y = 100; y < 220; y = y + 30) { 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) } @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 here base class:
import java.awt.image; import java.awt.toolkit; public class base { private image shieldpiece; private int x; private int y; public base(int xcoord, int ycoord) { xcoord = x; ycoord = y; } public int getshieldx() { return x; } public int getshieldy() { return y; } }
gamewindow class reference:
import java.awt.color; import java.awt.event.keylistener; import javax.swing.jframe; import javax.swing.timer; public class gamewindow extends jframe { public gamewindow() { setsize(800, 500); gamepanel pan = new gamepanel(); timer t = new timer(40, pan); add(pan); this.addkeylistener(pan); setdefaultcloseoperation(jframe.exit_on_close); t.start(); } /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub } }
annnnd pops up:
http://i.imgur.com/4bmf3ab.png
it should draw multiple shield pieces (the red rectangle) right of ship somewhere (starting @ x=300, y=100). window 800x500. i'm not sure why it's not looping through arraylist draw each piece. me?
the reason have found till this:
public base(int xcoord, int ycoord) { xcoord = x; ycoord = y; }
you assigning xcoord
value of x
, ycoord
value of y
0
default.it should changed to:
public base(int xcoord, int ycoord) { x = xcoord ; y = ycoord ; }
Comments
Post a Comment