DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2005
    Posts
    11

    A sample, please

    could someone please just show me a code of an applet that would do nothing but have a small ball on the screen, and make it move left when the left arrow key is pushed? thanks in advance

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Like this ?

    This applet can be run as an application also.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    
    /**
     *
     */
    
    public class ButtonBall extends JApplet implements KeyListener {
      private boolean isStandalone = false;
      public final static int STEP=5;
      public final static Dimension DIM=new Dimension(20,20);
      private Point p=null;
      //Construct the applet
      public ButtonBall() {
      }
      //Initialize the applet
      public void init() {
        try {
          jbInit();
          addKeyListener(this);
          p=new Point(getWidth()/2 - DIM.width/2,
                      getHeight()/2 - DIM.height/2);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**
       * Get focus here to catch the key events
       */
      public void start() {
        requestFocus();
      }
      //Component initialization
      private void jbInit() throws Exception {
        this.setSize(new Dimension(400,300));
      }
      public void paint (Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(Color.black);
        g.drawString("("+p.x+","+p.y+")",10,15);
        g.setColor(Color.RED);
        g.fillOval(p.x,p.y,DIM.width, DIM.height);
        g.setColor(c);
      }
      //Main method
      public static void main(String[] args) {
        ButtonBall applet = new ButtonBall();
        applet.isStandalone = true;
        JFrame frame = new JFrame();
        //EXIT_ON_CLOSE == 3
        frame.setDefaultCloseOperation(3);
        frame.setTitle("ButtonBall");
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
      }
      /**
       * Handle keypressed
       * @param e
       */
      public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
          case KeyEvent.VK_LEFT:
            p.x -= STEP;
            repaint();
            break;
          case KeyEvent.VK_RIGHT:
            p.x += STEP;
            repaint();
            break;
          case KeyEvent.VK_UP:
            p.y -= STEP;
            repaint();
            break;
          case KeyEvent.VK_DOWN:
            p.y += STEP;
            repaint();
            break;
        }
      }
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
    }
    eschew obfuscation

  3. #3
    Join Date
    Apr 2005
    Posts
    11
    yes, exactly, and thanks very much

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links