N consigo usar keyEvent em applets

4 respostas
W

tipo, eu to fazendo um gamezinho, pra rodar como software td funciona legal, mas qdo eu passo pra applet n funciona… o keyListener ta no JPanel, eu crio o applet e adiciono esse JPanel, mas ele n ta pegando nenhum evento de teclado…

como eu faco pra ele pegar os eventos de teclas em applets??

4 Respostas

T

Não pega nem se você clicar no applet primeiro, para que ele receba o foco?

W

nem clicando nele… n pego de geito nenhum…

W

vo cola aki o codigo .java da parte principal (q inclui o JPanel e o JFrame)

bom, funciona, entao por favor, alguem me diga a forma correta pra eu converter isso em uma applet

BrickGame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Canvas extends JPanel {
	private GameBall[] bolas = new GameBall[5];
	private Block[] blocos = new Block[100];
	private Barra barra = new Barra(30, 430, 80, 5, this);
	private Thread t;
	
	public Canvas() {
		setFocusable(true);
		
		startBall();
		
		Timer timer = new Timer(10, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(bolas[0].isDead())
					startBall();
				
				if(bolas[0] != null) {
					if(!t.isAlive()) {
						bolas[0].setPos(barra.getX() + barra.getWidth() / 2 - 5, 420);
						repaint();
					}
				}
			}
		});
		
		timer.start();
		
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int code = e.getKeyCode();
				
				if(code == KeyEvent.VK_ENTER)
					if(!t.isAlive())
						t.start();
				
				if(code == KeyEvent.VK_SPACE) 
					generateBlocs();
				
				if(code == KeyEvent.VK_LEFT)
						barra.startMoveLeft();
				
				if(code == KeyEvent.VK_RIGHT)
						barra.startMoveRight();
			}
			
			public void keyReleased(KeyEvent e) {
				int code = e.getKeyCode();
				
				if(code == KeyEvent.VK_LEFT)
					barra.stopMoveLeft();
				
				if(code == KeyEvent.VK_RIGHT)
					barra.stopMoveRight();
			}
		});
	}
	
	public void startBall() {
		int xVel = (int) Math.round(Math.random() * 6) - 3;
		bolas[0] = new GameBall(barra.getX() + barra.getWidth() / 2 - 5, 420, 10, 10, xVel, -2, 10, blocos, this, barra);
		bolas[0].setColor(new Color(0, 0, 255));
		t = new Thread(bolas[0]);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		bolas[0].fill(g, "oval");
		barra.fill(g, "rect");

		for(int i = 0; i < blocos.length; i++) {
			if(blocos[i] != null)
				blocos[i].fill(g, "rect");
		}
	}
	
	public void generateBlocs() {
		if(getSize().width < 1 || getSize().height < 1)
			return;
		
		int colunas = 10;
		int linhas = 5;
		int w = getSize().width / colunas;
		int h = 30;
		int posX, posY;
		
		for(int l = 0; l < linhas; l++) {
			posY = l * h + l;
			for(int c = 0; c < colunas; c++) {
				posX = c * w + c;
				int i = l * colunas + c;
				
				blocos[i] = new Block(posX, posY, w, h, 3);
				blocos[i].setColor(new Color(0, 255, 0));
			}
		}
		
		repaint();
	}
}

public class BrickGame extends JFrame {
	public BrickGame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(700, 500);;
		Canvas painel = new Canvas();
		getContentPane().add(painel, "Center");
	}
	
	public static void main(String[] args) {
		new BrickGame().show();
	}
}
T

http://matrix.netsoc.tcd.ie/~morgan/out/applet/code/BreakOut.java

Criado 24 de março de 2005
Ultima resposta 25 de mar. de 2005
Respostas 4
Participantes 2