i have constructed maze , made player class. when compile it, see maze , not player. have tried different techniques , still doesn't work me.
what have in order see player , able move it?
also, there way adjust maze , make bigger or same size frame?
import java.awt.*; import javax.swing.*; import java.awt.event.*; // needed actionlistener import javax.swing.event.*; // needed actionlistener import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.actionlistener; import javax.swing.jpanel; import javax.swing.timer; class www extends jframe { static maze maze = new maze (); static timer t; //======================================================== constructor public www () { // 1... create/initialize components // 2... create content pane, set layout jpanel content = new jpanel (); // create content pane content.setlayout (new borderlayout ()); // use borderlayout panel jpanel north = new jpanel (); north.setlayout (new flowlayout ()); // use flowlayout input area drawarea board = new drawarea (500, 500); // 3... add components input area. content.add (north, "north"); // input area content.add (board, "south"); // output area // 4... set window's attributes. setcontentpane (content); pack (); settitle ("maze"); setsize (490, 500); setdefaultcloseoperation (jframe.exit_on_close); setlocationrelativeto (null); // center window. } public static void main (string[] args) { www window = new www (); window.setvisible (true); player p = new player (); jframe jf = new jframe (); //jf.settitle("tutorial"); //jf.setsize(600,400); jf.setvisible(true); } class drawarea extends jpanel { public drawarea (int width, int height) { this.setpreferredsize (new dimension (width, height)); // size } public void paintcomponent (graphics g) { maze.show (g); // display current state of colony } } } class maze { private int grid [][]; public maze () { int [][] maze = { {1,0,1,1,1,1,1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,0,0,0,0,1}, {1,0,1,0,0,0,1,0,1,1,1,0,1}, {1,0,0,0,1,1,1,0,0,0,0,0,1}, {1,0,1,0,0,0,0,0,1,1,1,0,1}, {1,0,1,0,1,1,1,0,1,0,0,0,1}, {1,0,1,0,1,0,0,0,1,1,1,0,1}, {1,0,1,0,1,1,1,0,1,0,1,0,1}, {1,0,0,0,0,0,0,0,0,0,1,0,1}, {1,1,1,1,1,1,1,1,1,1,1,0,1}}; grid = maze; } public void show (graphics g) { (int row = 0 ; row < grid.length ; row++) (int col = 0 ; col < grid [0].length ; col++) { if (grid [row] [col] == 1) // life g.setcolor (color.black); else g.setcolor (color.white); g.fillrect (col * 30 + 30, row * 30 + 30, 30, 30); // draw life form } } public class player extends jpanel implements actionlistener,keylistener { timer tm = new timer(5,this); private int x = 0; private int y = 0; private int velx = 0 ; private int vely = 0; public void player () { tm.start(); addkeylistener(this); setfocusable(true); setfocustraversalkeysenabled(false); } public void paintcomponent (graphics g) { super.paintcomponent(g); g.setcolor(color.red); g.fillrect(20,30,50,50); } public void actionperformed(actionevent e) { x = x+velx; y = y +vely; repaint(); } public void keypressed(keyevent e) { int c = e.getkeycode(); if(c == keyevent.vk_left) { velx = -1; vely = 0; } if (c == keyevent.vk_up) { velx = 0; vely = -1; } if( c==keyevent.vk_right) { velx = 1; vely = 0; } if(c==keyevent.vk_down) { velx = 0; vely = 1; } } public void keytyped(keyevent e){} public void keyreleased(keyevent e){} } }
Comments
Post a Comment