How do I add a keybinder to an applet

Hello everybody!
this 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 keybinder 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. 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 keybinder yet)
- could someone please add the keybinder so I can understand more how they work!?
thanks in advance to everybody!
post anything please, any help is appreciated
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;
}

Add a KeyAdapter like this:
addKeyListener(new KeyAdapter() {
  public void keyPressed(KeyEvent ke) {
    int key = ke.getKeyCode();
    if (key == KeyEvent.VK_LEFT) x1--;
    if (key == KeyEvent.VK_RIGHT) x1++;
});*Note that I added this just after the bouncing = true; ... which is on about line 33.
**Note that you will need to click in the Applet before the keys will function.
***Note that you will also likely want to slow down the balls.
~Bill

Similar Messages

  • How can I add an other pop up applet in a already running web-page?

    I have an applet embbed in html.
    I want to get another applet as a pop up (not embbed in html) when I press a button in the html.
    I have tried:
    if (e.getSource() == button {
    Applet2 applet2 = new Applet2();
    applet2 .init();
    but the applet haven't pop up, but I know that it's already called.
    Is it because I have
    "getContentPane" in the second applet?
    Can anyone know the reason and how to solve it?

    Maybe you should redesign your applet to have a JDesktopPane as the content pane, and design other subGUis as JInternalFrame and once action performed is called a new subGUI is created and JDesktopPane adds it to the main applet

  • How do i add a frame to an applet?

    This is the code i was doing during my java class ( skiping ahead a bit):
    package September;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Banner extends JApplet
      implements ActionListener
      private int xPos, yPos,xPos2, yPos2, xPos3, yPos3;
      public JButton b1;
      public JLabel label;
      public BannerFrame frame = new BannerFrame();
       *  Called automatically when the applet is initialized
      public void init()
        Container c = getContentPane();
        c.setBackground(Color.white);
        xPos = c.getWidth();
        yPos = c.getHeight() / 2;
        xPos2 = c.getWidth();
        yPos2 = c.getHeight();
        xPos3 = 0;
        yPos3 = 0;
        JLabel label = new JLabel("This is a label");
        b1 = new JButton("Stop String");  // haven't gotten to get the button to do stuff yet but will in future
        b1.addActionListener(this); // again will do something in the future
        c.add(b1,BorderLayout.SOUTH);
        c.add(label,BorderLayout.NORTH);
        Timer clock = new Timer(30, this);  // Fires every 30 milliseconds
        clock.start();
       *  Called automatically after a repaint request
      public void paint(Graphics g)
        super.paint(g);
        Container c = getContentPane();
    //    g.setColor(Color.green);
    //    g.fillRect(0,0,c.getWidth(),c.getHeight()); // i put this in BannerFrame
        c.add(frame);
        g.setColor(Color.BLACK);
        g.fillRect(xPos3,yPos3,10,10);
        g.drawString("This String goes up and down", 0,yPos2 + 10);
        g.drawString("String:"+ yPos2,0,10);
        g.drawString("PaneHeight: "+c.getHeight(),60,10);
        g.drawString("PaneWidth:" + c.getWidth(), 60, 25);
       *  Called automatically when the timer fires
      public void actionPerformed(ActionEvent e)
    //       JButton button = (JButton)e.getSource();
    //       if(button == b1){
        Container c = getContentPane();
        xPos--;
        yPos2++;
        xPos3++;
        yPos3++;
        if (xPos < -100) // if off screen
          xPos = c.getWidth();
        if (yPos2 > 200){
         // if off screen
          yPos2 = 0;
        if(yPos3 > 200 && yPos3 > 100){
         // if off screen
             yPos3=0;
             xPos3=0;
        yPos = c.getHeight() / 2;
        //xPos2 = c.getWidth();
        repaint();
    }I tried to put the "g.drawRect()" function in the paint method of the applet but it looks really messy (repaints to fast -> looks like its blinking on the screen). I tried to put it in a diffrent class so it wouldn't look this way but the class that i made doesn't work in the applet
    BTW, here the code for BannerFrame:
    package September;
    import javax.swing.*;
    import java.awt.*;
    * @author student
    public class BannerFrame extends JFrame{
         public int height = this.getContentPane().getHeight(), width = this.getContentPane().getWidth();
         public BannerFrame(){
              Container c = getContentPane();
              c.setBackground(Color.green);
         public void paint(Graphics g){
              g.drawRect(0,0,width,height);
    }

    Some might want to figure out from your code what you want to do and what the problem is, but I would prefer an explaination to go along with the code, if you would please.

  • How can I add an JEditorPane to an Applet?

    i saw it in Swing2 demo and its working in applet too.
    but I couldnt make it work . Any clue apriciated.

    Hi is this you are looking for...Check out...
    import java.awt.*;
    import javax.swing.JApplet;
    import javax.swing.*;
    import javax.swing.text.*;
    public class Example extends JApplet
         public void init()
         try {
               String url = "http://java.sun.com";
            JEditorPane editorPane = new JEditorPane(url);
            editorPane.setEditable(false);
            //JFrame frame = new JFrame();
            //frame.getContentPane().add(editorPane, BorderLayout.CENTER);
            //frame.setSize(100, 300);
            //frame.setVisible(true);
              } catch (Exception e) {
    }---Vidya

  • How do I add URI web link with custom tooltip like "CLICK HERE TO UPDATE" instead of URI web link in tooltip.

    How do I add URI web link with custom tooltip like "CLICK HERE TO UPDATE" instead of URI web link in tooltip.

    You've probably found an answer to this by now, but I think this has been addressed in another forum -- The link below suggested using a button and adding the tooltip to the button. 
    https://forums.adobe.com/thread/304974?start=0&tstart=0
    Sounds like it would work but I haven't actually tried it. 
    Good luck~!

  • How do i add type kit fonts to muse web site

    how do I +add type kit fonts to muse website

    Hi.
    Check this video, might be helpful
    Let me know if you have any questions

  • How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    Hi Nancy,
    This screenshot was only for imagination. A part of the code (not all) is below.  In the code there are some background images but they are not seem in live mode.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/my_site.css" rel="stylesheet" type="text/css" />
    <link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css"/>
    <link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
    <script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
    <script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
    <style>
    #CollapsiblePanel1 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-color: #003366;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel1 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel1 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/international.jpg);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel2 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelContent {
        background-color: blue;
    #CollapsiblePanel3 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel3 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel3 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel4 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel5 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    </style>

  • How do I add multiple apple id's to one account

    How do I add multiple Apple id's to one account for billing and Icloud use?

    You don't. You can't combine Apple IDs.

  • HT201342 How can I add an e-mail address to my existing icloud addresses?  I currently only use one and I would like to ad two more?

    How can I add an e-mail address to my existing icloud account?  I curently only use one of the three.

    Each iCloud account only has a single iCloud account.  If you want, you can add up to 3 alias addresses that will receive email in the same iCloud inbox, as explained here: http://support.apple.com/kb/PH2622.

  • How can I add amazon to Apple TV?

    I am a new Apple TV user. So far I am impressed and like it a lot. However I would like to use the Amazon Prime and I have yet to find it. How can I add it to my Apple TV?

    You cannot add apps to Apple TV. If you have an IOS device you can AirPlay to stream using the app from there

  • HT201269 How do I add another contact / recipient to an SMS. Easy in Android, but I can't see how on my 5s.  Have to create a group on my Macbook Pro (OS 9.1), but it's not getting synced via usb. I'd prefer not to use cloud.

    How do I add a contact / recipient to an SMS on iPhone 5s?  It's easy on Android, but I've not sussed it on the iPhone - can't create a group etc.  Tried to create a group on Mac, but it either doesn't exist or won't sync to the phone with usb, nor will other contact details created on Mac.  I'd rather not use the cloud.  I'm sure contacts used to sync to my old iPod touch in Snow Leopard.
    Is it not more logical to have the phone as the main source of this sort of data?  We're more likely to update on a device we carry all the time surely?
    Please advise. Thank you

    Hey Cornish wrinkly,
    It sounds like you want to create a group message. You can read about group messaging on the iPhone here:
    iOS: Understanding group messaging
    http://support.apple.com/kb/HT5760
    Welcome to Apple Support Communities!
    Regards,
    Delgadoh

  • How do I add multiple tickets under the same confirmation number from the American Airlines app to Passbook?

    I have a confirmation number from American Airlines for a flight with my family all under my name and for some reason I cannot add all of our tickets to Passbook. To clarify, I went to the American Airlines app and put in the confirmation number along with my name which pulled up my boarding pass only. Although I was able to add this boarding pass to Passbook, I was not able to add any of the other boarding passes for my children to Passbook that should have also been under the flight reservation. In fact I couldn't even see them in the American Airlines app. I tried to use their names and the same confirmation number but nothing happened, and nothing else seems to be working. How can I add all of these boarding passes to my Passbook?

    Fair enough.
    I started doing this before unlimited data and emails on phones was a common commodity, on my Samsung Blackjack.
    I then continued it due to its convenience. My only counter argument is this:
    Even when emails are set to immediately push, I am always notified earlier by this forwarding message (sometimes by several minutes/hours due to the fact that it is not dependent on my being connected to 4G or wifi to receive it.
    Also, to contradict snozdop's point that both methods use data and battery, I say this - when I use the forwarding method, the information comes in a pure text format and therefore uses considerably less information than an HTML and CSS rich email, with embedded images and such -also, unlimited text messages aids curve costs.
    I will, however, give this method a go. In any case, a solution to my original question would still be greatly appreciated.
    Thanks guys.

  • How do I add multiple contacts to a group?

    How do I add multiple contacts to a group on my Ipad?

    See if this thread helps.
    https://discussions.apple.com/thread/4114588
    Matt

  • How do i add a company logo to my email signature on my mac book pro

    how do i add a company logo to my email signature on my mac book pro

    I would like to know how to do this as well. All I'm able to do is add the link, but not the button. My colleague has done this on his Outlook on his HP. I'm not sure why this is so difficult!

  • How do I add a printer connected to another iMac on the network to my printer list?

    How do I add an Epson printer connected to an iMac on the network to the printer list on my MacBook Pro? I cannot get it to show up in the list for printers to add.

    That only works if the printer is itself a network printer. You can't do it if it is directly connected to another computer on the network unless it is configured to be a Shared Printer on the other computer.

Maybe you are looking for