Does anyone know where I could learn how to make a basic graphic with animations? I don't really understand the coordinates of the animations.
Printable View
Does anyone know where I could learn how to make a basic graphic with animations? I don't really understand the coordinates of the animations.
Code:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationVariables extends JPanel implements Runnable {
int x = 100;
int y = 100;
int s = 100;
int dx = 3;
int dy = 2;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillRect(x, y, s, s);
}
public void run() {
boolean runMore = true;
while(runMore) {
try {
Thread.sleep(50);
} catch(InterruptedException e) {
runMore = false;
}
checkBoundries();
x += dx;
y += dy;
repaint();
}
}
private void checkBoundries() {
if(x + dx < 0 || x + s + dx > getWidth())
dx *= -1;
if(y + dy < 0 || y + s + dy > getHeight())
dy *= -1;
}
private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
break;
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
public static void main(String[] args) {
AnimationVariables test = new AnimationVariables();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
test.start();
}
}
awesome man thanks! okay now I want to make it a little more complicated with some user interaction. I know the basic code for the user moving the arrow keys left to right. So how can I make that template more "fun," or into a "game?"
thank you SO much for all your help, you are a life saver!
this is what I have in another java file for moving text from left to right with the arrow keys:
how would I import that to the red animation?Quote:
// handles KeyEvents
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)// left arrow
xdir = -5;// move left
if (e.getKeyCode() == KeyEvent.VK_RIGHT)// right arrow
xdir = 1;// move right
if (e.getKeyChar() == ' ') {// space bar
beep.play();
xdir = 0;// stop moving
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
If you want to use keyboard input I would recommend keybinding which you can find out about here How to Use Key Bindings. You bind them to the JFrames JRootPane and (you may need to) add a call to setFocusable on your JPanel/graphic component.
hmmm okay, I don't really understand the site or what you're saying
so I could just move the key events code into the red one?
Or I have a better idea.
On another file, I have a yellow oval, and I want to have text moving (left to right) saying move the eyes (attached under the text) to the center,
and while the user moves the eyes left or right, eventually to the center,
the eyes will change color and maybe a "YOU WIN!" could come up, if possible. I just want a simple "game" with some user interaction
or to keep the red box from hitting the walls, and if it does, maybe it turns black
is that possible?
with user interaction though
I GOT IT! I made an overlap game, I am going to go in early so I can make them turn white when they overlap, thanks for all your help!