-
Key/mouse movement
I'm trying to get some shapes to either follow the mouse or move when a key is pressed... So far this is what I have, and my System.out.println("..."); when moved or pressed works alright, but its not repainting or moving the graphics.
Code:
package Assignment04;
import java.util.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
public class animation extends JApplet implements KeyListener, MouseMotionListener, MouseListener, Runnable
{
public void init ()
{
setBackground(Color.BLACK);
setSize(400,400);
}
int x = 0, y = 0, mx=0, my=0;
public void paint(Graphics g)
{
g.setColor(Color.blue.darker());
g.fillOval(x, y, 50, 100);
addKeyListener(this);
addMouseMotionListener(this);
addMouseListener(this);
this.setFocusable(true);
g.fillRect(mx,my,40,40);
repaint();
}
public void run()
{
while(true)
{
try
{
Thread.sleep(20); // is this necessary and/or in the right spot to "animate" movement correctly?
}
catch(Exception e) {}
repaint();
}
}
public void keyTyped(KeyEvent e)
{
int key = e.KEY_PRESSED;
if(key!=0) //same issue as the snippet under this, this was just to test if input was being read. Would like to change so that certain keys do certain movement. i.e. a is left, d is right, w is up, s is down.
{
x = x+10;
System.out.println("Pressed key"+ x +"," + y);
}
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyChar();
if(key == KeyEvent.KEY_PRESSED) //initially here, I had KeyEvent.VK_D, but it wouldnt ever execute, I assume since "Pressed key" never printed....? How would I change that?
{
//x = x+10;
System.out.println("Pressed key");
}
}
public void keyReleased(KeyEvent e)
{
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
mx=this.getX();
my=this.getY();
System.out.println("Moved...");
repaint();
}
}
-
You're killing your app by adding all those listeners every time the applet repaints. This will overload you with listeners and will surely lead to a crash. A few recommendations:
1) painting methods such as paint and paintComponent should be sleek and fast and should concern themselves with painting and nothing but painting. Never add listeners in there, never read from files, don't set focus in there, avoid creating objects in these methods.
2) Your while loop is unnecessary and potentially dangerous.
3) Check out the tutorials on how to paint in Swing as you'll need to change some assumptions.
4) as the tutorials will show you, do your drawing in a paintComponent method of a JPanel subclass, not directly in a top-level window.
5) Class names should begin with a capital letter.
6) repaint in a paint method? Are you sure you want to do that?
Luck!
-
So if I want a listener to be added to a specific shape that I draw in the window, how do I do that? I guess either my teacher isn't very good at teaching this stuff or something, but I've been trying to figure this crap out all week and can't get any results, and his examples, unless I'm interpreting them wrong, are doing things that you're saying I shouldn't be..
Where are the swing paint tutorials? I googled, found very little that helps what I'm trying to do.
-