Reputation:
I am developing this application, it is a ball bouncing around. But i want to add purpoise of the application. So I want the user to be able to move a pad around, to prevent the ball from touching the wall. So I have got this code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainframeTwo extends JPanel implements Runnable{
Color color = Color.red;
int dia = 60;
int diaPad = 120;
long delay = 20;
private int x = 120;
private int y = 340;
private int xx = 120;
private int yy = 670;
private int dx = 6;
private int dy = 7;
private int dxx = 6;
boolean leftKey = false;
boolean rightKey = false;
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,60,60);
g.setColor(Color.BLACK);
g.fillRect(xx,yy,120,15);
}
public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
moveBall();
repaint();
}
}
public void moveBall() {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColorOne();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColorOne();
}
x += dx;
y += dy;
}
public void movePadRight() {
if(xx + dxx < 0 || xx + diaPad + dxx > getWidth()) {
dxx *= -1;
}
xx += dxx;
}
public void movePadLeft() {
if(xx + dxx < 0 || xx + diaPad + dxx > getWidth()) {
dxx *= -1;
}
xx += dxx;
}
private Color getColorOne() {
int rval = (int)Math.floor(Math.random() * 256);
int gval = (int)Math.floor(Math.random() * 256);
int bval = (int)Math.floor(Math.random() * 256);
return new Color(rval, gval, bval);
}
private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
MainframeTwo test = new MainframeTwo();
frame.getContentPane().add(panel);
panel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("Released" + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed " + e.getKeyChar());
}
});
panel.setFocusable(true);
panel.requestFocusInWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(test);
frame.setSize(640, 960);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true);
test.start();
}
};
SwingUtilities.invokeLater(r);
}
}
Terrible formatting, I know. But anyways, I want to be able to have a if() function that will trigger if the key A and S is pressed. I dont know f that is possible. If it is not possible, please tell me a way to trigger movePadLeft and Right, when the keys A and S is pressed.
Upvotes: 0
Views: 595
Reputation: 2607
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
if (e.getKeyCode() == KeyEvent.VK_A) {
//A pressed...
}
Upvotes: 0
Reputation: 42174
Use a boolean to keep track of whether each key you care about is pressed. Set those booleans in the keyPressed() and keyReleased() methods. Check the value of those booleans in the movePad() functions and do the correct thing.
Upvotes: 1