Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Focus problems

801176Oct 5 2010 — edited Oct 5 2010
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?

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 2 2010
Added on Oct 5 2010
4 comments
2,325 views