4 Replies Latest reply: Oct 5, 2010 2:44 PM by 801176 RSS

    Focus problems

    801176
      Hy everyone!

      I have some focusing problems. (I hope it's focusing...)

      I have 3 java files:
      package testproject;
      
      import java.applet.Applet;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseMotionListener;
      
      /**
       *
       * @author Laxika
       */
      public class Main extends Applet implements MouseMotionListener, KeyListener {
      //Added these variables just to test the DoubleBuffering.
      
          public static int posX = 0;
          public static int posY = 0;
      
          @Override
          public void init() {
              setSize(300, 300);
              this.add(new DoubleBufferTest());
              addMouseMotionListener(this);
              addKeyListener(this);
          }
      
          public void mouseDragged(MouseEvent e) {
          }
      
          public void mouseMoved(MouseEvent e) {
              posX = e.getX();
              posY = e.getY();
              this.getComponent(0).repaint();
          }
      
          public void keyTyped(KeyEvent e) {
              System.out.println("Typed");
          }
      
          public void keyPressed(KeyEvent e) {
              System.out.println("Pressed");
          }
      
          public void keyReleased(KeyEvent e) {
               System.out.println("Released");
          }
      }
      package testproject;
      
      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.Rectangle;
      
      /**
       *
       * @author Rendszergazda
       */
      public class DoubleBufferTest extends DoubleBufferedCanvas {
      
          public DoubleBufferTest() {
              super.setBounds(new Rectangle(300, 300));
          }
      
          @Override
          public void paint(Graphics g) {
              g.clearRect(0, 0, 300, 300);
              try {
                  g.setColor(Color.red);
                  g.fillRect(Main.posX, Main.posY, 40, 40);
              } finally {
                  //Best to dispose graphics object after finish.
                  g.dispose();
              }
          }
      }
      package testproject;
      
      import java.awt.Canvas;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Rectangle;
      import java.awt.Toolkit;
      import java.awt.image.BufferStrategy;
      
      /**
       *
       * @author Laxika
       * @version 1.0
       */
      public abstract class DoubleBufferedCanvas extends Canvas {
      
          private int width;
          private int height;
      
          /**
           * Set the width and the height of the canvas.
           *
           * @param width The width of the canvas.
           * @param height The height of the canvas.
           */
          @Override
          public void setBounds(Rectangle r) {
              this.width = (int) r.getWidth();
              this.height = (int) r.getHeight();
          }
      
          @Override
          public void addNotify() {
              super.addNotify();
              this.createBufferStrategy(2);
              //The main Applet will handle every event.
              this.setEnabled(false);
          }
      
          @Override
          public void repaint() {
              BufferStrategy bf = this.getBufferStrategy();
      
              paint(bf.getDrawGraphics());
      
              bf.show();
              Toolkit.getDefaultToolkit().sync();
          }
      
          @Override
          public abstract void paint(Graphics g);
      
          @Override
          public Dimension getMinimumSize() {
              return new Dimension(width, height);
          }
      
          @Override
          public Dimension getPreferredSize() {
              return new Dimension(width, height);
          }
      
          @Override
          public Dimension getMaximumSize() {
              return new Dimension(width, height);
          }
      }
      My problem is that I can't get the Key events on main.java after I add the DoubleBufferTest class to the applet. ( this.add(new DoubleBufferTest()); )

      If I try hasFocus test after added the Canvas, I get false.

      Someone can helm me with this?
        • 1. Re: Focus problems
          Darryl Burke
          Moderator action: Moved from Java Programming.

          db
          • 2. Re: Focus problems
            Darryl Burke
            You add the <tt>KeyListener</tt> to the <tt>Applet</tt>, so why do you expect to get <tt>KeyEvent</tt>s in the <tt>Canvas</tt>?

            Do you have a reason for using an <tt>Applet</tt> (AWT) and not a <tt>JApplet</tt> (Swing)? With a Swing <tt>JApplet</tt> and a <tt>JPanel</tt>, you get double buffering for free and you can use Key Bindings.

            db
            • 3. Re: Focus problems
              801176
              Uhh thanks!

              I want they Key event s in the Applet. But I don't get them.:S

              EDIT:

              I found the solution, if someone have the same problem:
              KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
              
                          public boolean dispatchKeyEvent(KeyEvent e) { // This example converts all typed keys to upper case
                              if (e.getID() == KeyEvent.KEY_TYPED) {
                                  e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
                              } // If the key should not be dispatched to the
                              // focused component, set discardEvent to true
                              System.out.println(e.getKeyChar());
                              boolean discardEvent = false;
                              return discardEvent;
                          }
                      });
              Edited by: Teh Heavenly on 2010.10.05. 12:43
              • 4. Re: Focus problems
                801176
                Took me 3 hours googling, but finally...