1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
package demoanimation
;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicArrowButton;
public class DemoAnimationContinue extends JPanel {
private static final int SPEED = 5;
private final Image image;
private volatile Point location;
private volatile Point direction;
private final Timer timer = new Timer();
private final TimerTask timertask;
public DemoAnimationContinue(Image image) {
this.image=image;
this.location = new Point(0,0);
this.direction = new Point(0,0);
this.timertask = new TimerTask() {
@Override
public void run() {
moveImage();
}
};
}
public void startTimer() {
timer.scheduleAtFixedRate(timertask, 33, 33);
}
public void stopTimer() {
timertask.cancel();
}
public void resetLocation() {
System.out.println(image.getWidth(this));
int x=(getWidth()-image.getWidth(this))/2;
int y=(getHeight()-image.getHeight(this))/2;
direction = new Point(0,0);
location = new Point(x,y);
}
public void moveUp(int length) {
direction = new Point(direction.x,-length);
}
public void moveDown(int length) {
direction = new Point(direction.x,length);
}
public void moveRight(int length) {
direction = new Point(length,direction.y);
}
public void moveLeft(int length) {
direction = new Point(-length,direction.y);
}
private void moveImage() {
Point newLocation = new Point(location.x + direction.x, location.y + direction.y);
if ( newLocation.x+image.getWidth(this)>getWidth() ) {
newLocation.x = getWidth()-image.getWidth(this);
direction=new Point(-direction.x,direction.y);
}
if ( newLocation.x<0 ) {
newLocation.x = 0;
direction=new Point(-direction.x,direction.y);
}
if ( newLocation.y+image.getHeight(this)>getHeight() ) {
newLocation.y = getHeight()-image.getHeight(this);
direction=new Point(direction.x,-direction.y);
}
if ( newLocation.y<0 ) {
newLocation.y = 0;
direction=new Point(direction.x,-direction.x);
}
location = newLocation;
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
drawImage(g, location);
}
private void drawImage(Graphics g, Point location) {
g.drawImage(image, location.x, location.y, this);
}
public static void main(String[] args) {
run(new ImageIcon("ballon.gif").getImage());
}
public static void run(Image image) {
DemoAnimationContinue demoPanel = new DemoAnimationContinue(image);
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(e-> demoPanel.moveUp(SPEED));
JButton down = new BasicArrowButton(BasicArrowButton.SOUTH);
down.addActionListener(e-> demoPanel.moveDown(SPEED));
JButton right = new BasicArrowButton(BasicArrowButton.EAST);
right.addActionListener(e-> demoPanel.moveRight(SPEED));
JButton left = new BasicArrowButton(BasicArrowButton.WEST);
left.addActionListener(e-> demoPanel.moveLeft(SPEED));
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e-> {
switch(e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
demoPanel.moveRight(SPEED);
return false;
case KeyEvent.VK_LEFT:
demoPanel.moveLeft(SPEED);
return false;
case KeyEvent.VK_UP:
demoPanel.moveUp(SPEED);
return false;
case KeyEvent.VK_DOWN:
demoPanel.moveDown(SPEED);
return false;
}
return true;
});
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(demoPanel);
frame.add(up, BorderLayout.NORTH);
frame.add(left, BorderLayout.WEST);
frame.add(right, BorderLayout.EAST);
frame.add(down, BorderLayout.SOUTH);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent e) {
demoPanel.stopTimer();
}
});
frame.setVisible(true);
demoPanel.resetLocation();
demoPanel.startTimer();
}
} |
Partager