Results 1 to 7 of 7
- 12-18-2010, 06:27 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
keylistener + applet isn't working
Hello everybody!
ths is a project that i have been working on for the past week or soo! it has been going pretty well, until i tried to add in the keylistener to look for the up, down, left , and right direction keys to make an object on the screen move! I am trying to make the BLUE box move.:confused: the point of the game is to have a square that you control, that is to avoid the 2 bouncing balls.
(the code I added with this doesn't have any sort of keylistener yet)
thanks in advance to everybody!:p
post anything please, any help is appreciated
Java Code:// //brandon witt //paddle game //dec 6 2010 // import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class paddlegame extends Applet implements Runnable { Graphics g; //font declaration Font font1 = new Font("Helvetica", Font.BOLD, 90); Font font2 = new Font("Helvetica", Font.PLAIN, 30); //speed/trajectory int x_shift = 1; int y_shift = 1; int x1_shift = 2; int y1_shift = 2; int x1 = 500; int y1 = 500; //scorer double counter = 0; //etc. boolean bouncing; boolean testOutput = true; public void init() { r = new Rectangle(30,50,20,20); b = new Rectangle (40, 60, 20, 20); Thread t = new Thread(this); t.start();// behaviour of thread controlled by run method bouncing = true; } public void paint(Graphics g) { //square g.setColor(Color.BLUE); g.fillRect(x1, y1, 80, 80); //score display g.setColor(Color.BLACK); g.setFont(font2); g.drawString("Score:" + counter, 500, 25 ); //bouncing balls if (bouncing) g.setColor(Color.RED); g.fillOval(r.x,r.y,r.width,r.height); if (bouncing) g.setColor(Color.BLUE); g.fillOval(b.x,b.y,b.width,b.height); if (testOutput) System.out.println ("x = " + r.x + " y = " + r.y + " bouncing = " + bouncing); if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80 ){ //explosion scene g.setColor(Color.BLACK); g.fillOval(r.x - 100,r.y - 100,100, 100); g.setColor(Color.YELLOW); g.fillOval(r.x - 20,r.y - 40,100, 100); g.setColor(Color.RED); g.fillOval(r.x - 30,r.y - 15,100, 100); g.setColor(Color.ORANGE); g.fillOval(r.x - 100,r.y - 55,100, 100); g.setColor(Color.BLUE); g.fillOval(r.x - 5,r.y - 30,100, 100); g.setColor(Color.PINK); g.fillOval(r.x - 10,r.y - 40,100, 100); g.setColor(Color.GREEN); g.fillOval(r.x - 50,r.y - 50,100, 100); g.setColor(Color.RED); g.fillOval(r.x - 150,r.y - 150,250, 300); g.setColor(Color.orange); g.fillOval(r.x - 50,r.y - 50,40, 100); g.setColor(Color.orange); g.fillOval(r.x - 100,r.y - 80,100, 200); g.setColor(Color.yellow); g.fillOval(r.x - 50,r.y - 50,50, 100); g.setColor(Color.red); g.fillOval(r.x - 100,r.y - 40,40, 10); g.setColor(Color.ORANGE); g.fillOval(r.x - 10,r.y - 30,100, 100); g.setColor(Color.ORANGE); g.fillOval(r.x - 60,r.y - 80,20, 10); g.setColor(Color.BLUE); g.setFont(font1); g.drawString("YOU LOSE!!!", 300, 300); g.setColor(Color.BLACK); g.setFont(font2); g.drawString("Your Final Score:" + counter, 300, 400 ); } if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80){ //explosion scene 2 g.setColor(Color.BLACK); g.fillOval(b.x - 100,b.y - 100,100, 100); g.setColor(Color.YELLOW); g.fillOval(b.x - 20,b.y - 40,100, 100); g.setColor(Color.RED); g.fillOval(b.x - 30,b.y - 15,100, 100); g.setColor(Color.ORANGE); g.fillOval(b.x - 100,b.y - 55,100, 100); g.setColor(Color.BLUE); g.fillOval(b.x - 5,b.y - 30,100, 100); g.setColor(Color.PINK); g.fillOval(b.x - 10,b.y - 40,100, 100); g.setColor(Color.GREEN); g.fillOval(b.x - 50,b.y - 50,100, 100); g.setColor(Color.RED); g.fillOval(b.x - 150,b.y - 150,250, 300); g.setColor(Color.orange); g.fillOval(b.x - 50,b.y - 50,40, 100); g.setColor(Color.orange); g.fillOval(b.x - 100,b.y - 80,100, 200); g.setColor(Color.yellow); g.fillOval(b.x - 50,b.y - 50,50, 100); g.setColor(Color.red); g.fillOval(b.x - 100,b.y - 40,40, 10); g.setColor(Color.ORANGE); g.fillOval(b.x - 10,b.y - 30,100, 100); g.setColor(Color.ORANGE); g.fillOval(b.x - 60,b.y - 80,20, 10); g.setColor(Color.BLUE); g.setFont(font1); //final score text g.drawString("YOU LOSE!!!", 300, 300); g.setColor(Color.BLACK); g.setFont(font2); g.drawString("Your Final Score:" + counter, 300, 400 ); } } public void update(Graphics g) { Image offScreenImage = createImage(getSize().width, getSize().height); paint(offScreenImage.getGraphics()); g.drawImage(offScreenImage,0,0, null); } public void run() { while (true) { // Thread performs endless loop r.x += x_shift; r.y += y_shift; if (r.x>= getSize().width || r.x < 0) { x_shift *= -1; } if (r.y>= getSize().height || r.y < 40) { y_shift *= -1; } b.x += x1_shift; b.y += y1_shift; if (b.x>= getSize().width || b.x < 0) { x1_shift *= -1; } if (b.y>= getSize().height || b.y < 40) { // Why 40? y1_shift *= -1; } try { //speed of balls (long) Thread.currentThread().sleep((long) 0.9 ); } catch (Exception ke) { } //loop break if ball 1 or 2 hits square if (r.x > x1 && r.x < x1 + 80 && r.y > y1 && r.y < y1 + 80 ){ break; } if (b.x > x1 && b.x < x1 + 80 && b.y > y1 && b.y < y1 + 80 ){ break; } //repaints the balls at their current location repaint(); //increases the score counter += 0.01; } } private Rectangle r; private Rectangle b; public boolean mouseDown(Event e) { return true; } public boolean mouseDrag(Event e ) { return true; } public KeyEvent events(KeyEvent ke){ return ke; } public boolean mouseUp(Event e) { return true; } }Last edited by brandon95; 12-18-2010 at 11:16 PM.
-
Hello and welcome!
I'm no applet pro, but I believe that its' the HTML code that decides the applet's size.( p.s. i am also trying to figure out how to make the applet full screen as soon as it opens.):)
You may wish to show us your code attempt with KeyListeners, else, we'll have a tough time to know what you're doing wrong, or if you even should be using KeyListeners. Often it is better to use key binding instead.(the code I added with this doesn't have any sort of keylistener yet)
You're welcome.thanks in advance to everybody!:p
You will learn soon that you have little control over what people post here. The best tack to take is to thank them for considering your problem, even if you don't find the post helpful, to ignore posts that you don't like, and to report posts that are abusive.post anything please, any help is appreciated (no unnecessary comments)!!!
Finally, when posting code here, you'll want to use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 12-18-2010, 06:40 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
how might i go about editing my post so that i can post what i have done with the keylistener????
-
-
Also, you may be better off coding this in Swing and not AWT and also separating out your program logic from the GUI.
- 12-18-2010, 11:19 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
hey fudarable, can you do me a quick favour and add in the keylistener functions and add in a note as to where i should add in the keylistener, im not an expert in java so that help would be greatly appreciated. This isn't a school project or anything, so its not like its cheating.
ps do you mean separate where i placed the if statements, initiating the explosion graphic from "paint class"??? because i tried putting the if statements into my "run class" and it halted the ball movement for some reason(i made sure my syntax were correct)Last edited by brandon95; 12-18-2010 at 11:22 PM.
- 01-03-2011, 02:10 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
hey fudarable, can you do me a quick favour and add in the keylistener functions and add in a note as to where i should add in the keylistener, im not an expert in java so that help would be greatly appreciated. This isn't a school project or anything, so its not like its cheating.
ps do you mean separate where i placed the if statements, initiating the explosion graphic from "paint class"??? because i tried putting the if statements into my "run class" and it halted the ball movement for some reason(i made sure my syntax were correct)
Similar Threads
-
Applet not working in browser
By gkr1989 in forum Java AppletsReplies: 3Last Post: 07-03-2009, 09:43 AM -
Applet not working
By jyothi.priyanka in forum New To JavaReplies: 5Last Post: 04-16-2009, 08:18 AM -
keyListener isn't working for me
By lost_in_java in forum AWT / SwingReplies: 7Last Post: 12-05-2008, 04:24 AM -
Applet button not working
By letmeoutofhere in forum Java AppletsReplies: 9Last Post: 11-14-2007, 12:15 PM -
Help with KeyListener in applet
By mathias in forum Java AppletsReplies: 1Last Post: 08-06-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks