How to make an image move using the keyboard in java? -
i have maze need ball move through, don't know code need use move image of ball around maze. have been given hint need swap images round.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class cballmaze extends jframe implements actionlistener { //below have declared different objects have used throughout program private jbutton buttonright, buttonleft, buttonup, buttondown, buttontl, buttontr, buttonbl, buttonbr, buttoncenter, optionone, optiontwo, optionthree, optionexit, scenarioact, scenariorun, scenarioreset, compassph; private jbutton [] game = new jbutton [208]; private jpanel panelcentre, panelright, panelbottom, buttonpanel, compasspanel, optionspanel, selectionpanel, panelact, panelrun, panelreset, panelslider; private jtextfield optiontf, squaretf, directiontf; private jlabel option, square, direction, compassdirection; private jslider speedslider; private string firstoption = "1", secondoption = "2", thirdoption = "3", updirection = "north", rightdirection = "east", downdirection = "south", leftdirection = "west"; private int i; private int[] map = new int[] { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2, 3,1,3,3,3,1,3,3,3,1,3,3,3,3,3,3, 3,1,3,3,3,1,3,3,3,1,3,3,3,3,3,3, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 3,3,1,3,3,3,1,3,3,3,3,1,3,3,3,3, 3,3,1,3,3,3,1,3,3,3,3,1,3,3,3,3, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 3,1,3,3,3,1,3,3,3,1,3,3,3,3,3,3, 3,1,3,3,3,1,3,3,3,1,3,3,3,3,3,3, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 3,3,1,3,3,3,1,3,3,3,3,1,3,3,3,3, 3,3,1,3,3,3,1,3,3,3,3,1,3,3,3,3, 4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, }; final imageicon iconcompassnorth = new imageicon("north.jpg"); final imageicon iconcompasswest = new imageicon("west.jpg"); final imageicon iconcompasssouth = new imageicon("south.jpg"); final imageicon iconcompasseast = new imageicon("east.jpg"); imageicon iconreset = new imageicon("reset.jpg"); imageicon iconrun = new imageicon("run.jpg"); imageicon iconact = new imageicon("act.jpg"); imageicon iconsand, iconwhite, iconball, iconend; public cballmaze(string title) { super(title); } public static void main(string[] args) { cballmaze cballmaze = new cballmaze("cballmaze ball maze application"); cballmaze.setsize(775, 650); cballmaze.creategui(); cballmaze.setvisible(true); } private void creategui() { setdefaultcloseoperation(exit_on_close); container window = getcontentpane(); window.setlayout(new borderlayout() ); //panels panelcentre = new jpanel(); panelcentre.setpreferredsize(new dimension(625, 450)); panelcentre.setbackground(color.black); window.add(panelcentre); panelcentre.setlayout(new gridlayout(13, 16)); panelright = new jpanel(); panelright.setpreferredsize(new dimension(180, 450)); panelright.setbackground(color.white); window.add(panelright, borderlayout.east); optionspanel = new jpanel(); optionspanel.setpreferredsize(new dimension(150, 100)); optionspanel.setbackground(color.white); panelright.add(optionspanel, borderlayout.east); buttonpanel = new jpanel(); buttonpanel.setpreferredsize(new dimension(175, 100)); buttonpanel.setbackground(color.white); panelright.add(buttonpanel, borderlayout.east); selectionpanel = new jpanel(); selectionpanel.setpreferredsize(new dimension(175, 150)); selectionpanel.setbackground(color.white); panelright.add(selectionpanel, borderlayout.east); imageicon cw = new imageicon("west.jpg"); compasspanel = new jpanel(); compasspanel.setpreferredsize(new dimension(175, 300)); compasspanel.setbackground(color.white); panelright.add(compasspanel, borderlayout.east); panelbottom = new jpanel(); panelbottom.setpreferredsize(new dimension(875, 50)); panelbottom.setbackground(color.white); window.add(panelbottom, borderlayout.south); panelact = new jpanel(); panelact.setpreferredsize(new dimension(125, 40)); panelact.setbackground(color.white); panelbottom.add(panelact, borderlayout.south); panelrun = new jpanel(); panelrun.setpreferredsize(new dimension(125, 40)); panelrun.setbackground(color.white); panelbottom.add(panelrun, borderlayout.south); panelreset = new jpanel(); panelreset.setpreferredsize(new dimension(125, 40)); panelreset.setbackground(color.white); panelbottom.add(panelreset, borderlayout.south); panelslider = new jpanel(); panelslider.setpreferredsize(new dimension(200, 40)); panelslider.setbackground(color.white); panelbottom.add(panelslider, borderlayout.south); //displays option = new jlabel("option: "); optionspanel.add(option, borderlayout.line_start); option.setenabled(true); option.setforeground(color.black); option.sethorizontalalignment(jlabel.left); optiontf = new jtextfield("1"); optionspanel.add(optiontf, borderlayout.line_end); optiontf.setenabled(true); optiontf.setpreferredsize(new dimension(50, 25)); optiontf.sethorizontalalignment(jtextfield.center); square = new jlabel("square: "); optionspanel.add(square, borderlayout.line_start); square.setenabled(true); square.setforeground(color.black); square.sethorizontalalignment(jlabel.left); squaretf = new jtextfield(""); optionspanel.add(squaretf, borderlayout.line_end); squaretf.setenabled(true); squaretf.setpreferredsize(new dimension(50, 25)); squaretf.sethorizontalalignment(jtextfield.center); direction = new jlabel("direction: "); optionspanel.add(direction, borderlayout.line_start); direction.setenabled(true); direction.setforeground(color.black); direction.sethorizontalalignment(jlabel.left); directiontf = new jtextfield("still"); optionspanel.add(directiontf, borderlayout.line_end); directiontf.setenabled(true); directiontf.setpreferredsize(new dimension(50, 25)); directiontf.sethorizontalalignment(jtextfield.center); //buttons buttontl = new jbutton(""); buttonpanel.add(buttontl); buttontl.setpreferredsize(new dimension(45, 25)); buttontl.setenabled(false); buttonup = new jbutton("^"); buttonpanel.add(buttonup); buttonup.setpreferredsize(new dimension(45, 25)); buttonup.addactionlistener(this); buttonup.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { directiontf.settext(updirection); compassdirection.seticon(iconcompassnorth); } }); buttontr = new jbutton(""); buttonpanel.add(buttontr); buttontr.setpreferredsize(new dimension(45, 25)); buttontr.setenabled(false); buttonleft = new jbutton("<"); buttonpanel.add(buttonleft); buttonleft.setpreferredsize(new dimension(45, 25)); buttonleft.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { directiontf.settext(leftdirection); compassdirection.seticon(iconcompasswest); } }); buttoncenter = new jbutton(""); buttonpanel.add(buttoncenter); buttoncenter.setpreferredsize(new dimension(45, 25)); buttoncenter.setenabled(false); buttonright = new jbutton(">"); buttonpanel.add(buttonright); buttonright.setpreferredsize(new dimension(45, 25)); buttonright.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { directiontf.settext(rightdirection); compassdirection.seticon(iconcompasseast); } }); buttonbl = new jbutton(""); buttonpanel.add(buttonbl); buttonbl.setpreferredsize(new dimension(45, 25)); buttonbl.setenabled(false); buttondown = new jbutton("v"); buttonpanel.add(buttondown); buttondown.setpreferredsize(new dimension(45, 25)); buttondown.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { directiontf.settext(downdirection); compassdirection.seticon(iconcompasssouth); } }); buttonbr = new jbutton(""); buttonpanel.add(buttonbr); buttonbr.setpreferredsize(new dimension(45, 25)); buttonbr.setenabled(false); optionone = new jbutton("option one"); selectionpanel.add(optionone); optionone.setpreferredsize(new dimension(125, 25)); optionone.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { optiontf.settext(firstoption); } }); optiontwo = new jbutton("option two"); selectionpanel.add(optiontwo); optiontwo.setpreferredsize(new dimension(125, 25)); optiontwo.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { optiontf.settext(secondoption); } }); optionthree = new jbutton("option three"); selectionpanel.add(optionthree); optionthree.setpreferredsize(new dimension(125, 25)); optionthree.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { optiontf.settext(thirdoption); } }); optionexit = new jbutton("exit"); selectionpanel.add(optionexit); optionexit.setpreferredsize(new dimension(125, 25)); scenarioact = new jbutton("act"); scenarioact.seticon(iconact); panelact.add(scenarioact); scenarioact.addactionlistener(this); scenariorun = new jbutton("run"); scenariorun.seticon(iconrun); panelrun.add(scenariorun); scenariorun.addactionlistener(this); scenarioreset = new jbutton("reset"); scenarioreset.seticon(iconreset); panelreset.add(scenarioreset); scenarioreset.addactionlistener(this); jslider speedslider = new jslider(jslider.horizontal, 0, 50, 25); speedslider.setmajortickspacing(10); speedslider.setminortickspacing(5); speedslider.setpaintticks(true); speedslider.setbackground(color.white); speedslider.setlabeltable(speedslider.createstandardlabels(10)); panelslider.add(speedslider); compassdirection = new jlabel(); compasspanel.add(compassdirection); try { iconsand = new imageicon("sand.jpg"); } catch (exception e) { system.err.println("sand icon "+e); } try { iconball = new imageicon("sand60x60.png"); } catch (exception e) { system.err.println("ball icon "+e); } try { iconwhite = new imageicon("white32x32.jpg"); } catch (exception e) { system.err.println("white icon "+e); } try { iconend = new imageicon("sandstone.jpg"); } catch (exception e) { system.err.println("end icon"+e); } (i=0;i<208;i++) { game[i] = new jbutton (); if(map[i]==1) { game[i].seticon(iconsand); } if(map[i]==2) { game[i].seticon(iconball); } if(map[i]==3) { game[i].seticon(iconwhite); } if(map[i]==4) { game[i].seticon(iconend); } game[i].setborder(null); game[i].setpreferredsize(new dimension(32, 32)); game[i].addactionlistener(this); panelcentre.add(game[i]); } } public void actionperformed(actionevent arg0) { } }
try (works me):
after line panelcentre.add(game[i]);
enter this:
game[i].addkeylistener(new keylistener() { @override public void keytyped(keyevent e) { } @override public void keypressed(keyevent e) { } @override public void keyreleased(keyevent e) { system.out.println(e.getkeycode()); switch (e.getkeycode()) { case keyevent.vk_right: system.out.println("right..."); break; case keyevent.vk_left: system.out.println("left..."); break; case keyevent.vk_down: system.out.println("down..."); break; case keyevent.vk_up: system.out.println("up..."); } } });
this should print out direction typed arrow keys.
Comments
Post a Comment