📄 animationwindow.java
字号:
package gizmoball_demo;
import gizmos.*;
import gizmos.SquareBumper;
import java.util.Vector; //大小可扩展的存储空间
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.Timer;
/**
*
* @author Administrator
*/
public class AnimationWindow extends JComponent {
private static final long serialVersionUID = 3257281448464364082L;
// Controls how often we redraw
private static int FRAMES_PER_SECOND = 100;
private AnimationEventListener eventListener;
private BouncingBall ball;
private Vector circleBumper = new Vector(20);
private Vector squareBumper = new Vector(20);
private RightFlipper rflipper;
private TriangleBumper triangleBumper;
private Timer timer;
private boolean mode;
private int cCount = 0;//当前地图中圆形障碍物的个数
private int sCount = 0;//当前地图中方形障碍物的个数
private boolean cPress = false;//验证当前是否有一个圆形障碍物被选中
private boolean sPress = false;//验证当前是否有一个方形障碍物被选中
private int index = 0;//被选中的障碍物的编号
/**
* @effects initializes this to be in the off mode.
*/
public AnimationWindow() {
super(); // do the standard JPanel setup stuff
ball = new BouncingBall(this);
rflipper = new RightFlipper(this,"r1",300,300,90);
//triangleBumper = new TriangleBumper(this,"t1",10,10,180);
// this only initializes the timer, we actually start and stop the
// timer in the setMode() method
eventListener = new AnimationEventListener();
addMouseListener(eventListener);
addMouseMotionListener(eventListener);
addKeyListener(eventListener);
// The first parameter is how often (in milliseconds) the timer
// should call us back.
timer = new Timer(1000 / FRAMES_PER_SECOND, eventListener);
mode = false;
}
/**
* @modifies g
* @effects Repaints the Graphics area g. Swing will then send the newly painted g to the screen.
* @param g Graphics context received by either system or app calling repaint()
*/
@Override public void paintComponent(Graphics g) {
// first repaint the proper background color (controlled by
// the windowing system)
//super.paintComponent(g);
ball.paint(g);
for(int i=0; i<cCount; i++){
((CircleBumper)circleBumper.get(i)).draw((Graphics2D)g);
}
for(int j=0; j<sCount; j++){
((SquareBumper)squareBumper.get(j)).draw((Graphics2D)g);
}
rflipper.draw((Graphics2D)g);
/*try{
triangleBumper.draw((Graphics2D)g);
}catch(java.io.IOException e){
System.out.println("Exception");
}*/
//rflipper.drawBB((Graphics2D)g,new Color(0,255,255));
}
/**
* This method is called when the Timer goes off and we
* need to move and repaint the ball.
* @modifies both the ball and the window that this listener owns
* @effects causes the ball to move and the window to be updated
* to show the new position of the ball.
*/
private void update() {
Rectangle oldPos = ball.boundingBox();
ball.move(); // make changes to the logical animation state
Rectangle repaintArea = oldPos.union(ball.boundingBox());
// Have Swing tell the AnimationWindow to run its paint()
// method. One could also call repaint(), but this would
// repaint the entire window as opposed to only the portion that
// has changed.
repaint(repaintArea.x, repaintArea.y, repaintArea.width,
repaintArea.height);
}
/**
* @modifies this
* @effects Turns the animation on/off.
* @param m Boolean indicating if animation is on/off
*/
public void setMode(boolean m) {
if (mode == m) {
// Nothing to do.
return;
}
if (mode == true) {
// we're about to change mode: turn off all the old listeners
removeMouseListener(eventListener);
removeMouseMotionListener(eventListener);
removeKeyListener(eventListener);
}
mode = m;
if (mode == true) {
// the mode is true: turn on the listeners
addMouseListener(eventListener);
addMouseMotionListener(eventListener);
addKeyListener(eventListener);
requestFocus(); // make sure keyboard is directed to us
timer.start();
}
else {
timer.stop();
}
}
/*
*此方法用于在绘图区域中增加一个圆形障碍物
*/
public void addC(){
circleBumper.add(cCount,new CircleBumper(this,"c",100,400));
cCount++;
}
public CircleBumper getC(int index){
//CircleBumper.setRadius(18);
return (CircleBumper)circleBumper.get(index);
}
public int getCCount(){
return cCount;
}
//此方法用于在绘图区域中增加一个方形障碍物
public void addS(){
squareBumper.add(sCount,new SquareBumper(this,"s",200,400));
sCount++;
}
public SquareBumper getS(int index){
return (SquareBumper)squareBumper.get(index);
}
public int getSCount(){
return sCount;
}
class AnimationEventListener extends MouseAdapter implements
MouseMotionListener, KeyListener, ActionListener {
// MouseAdapter gives us empty methods for the MouseListener
// interface: mouseClicked, mouseEntered, mouseExited, mousePressed,
// and mouseReleased.
/**
* For this example we only need to override mouseClicked
* @modifes the ball that this listener owns
* @effects causes the ball to be bumped in a random direction
* @param e Detected MouseEvent
*/
@Override public void mouseClicked(MouseEvent e) {
//ball.randomBump();
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
int pressx = e.getX();
int pressy = e.getY();
for(int i=0; i<cCount; i++){
int distance = (pressx-((CircleBumper)circleBumper.get(i)).getX()) * (pressx-((CircleBumper)circleBumper.get(i)).getX());
if (distance <= ((CircleBumper)circleBumper.get(i)).getDis()*((CircleBumper)circleBumper.get(i)).getDis()){
cPress = true;
index = i;
break;
}
}
for(int j=0; j<sCount; j++){
int distance = (pressx -((SquareBumper)squareBumper.get(j)).getX()) * (pressx-((SquareBumper)squareBumper.get(j)).getX());
if (distance <= ((SquareBumper)squareBumper.get(j)).getDis()*((SquareBumper)squareBumper.get(j)).getDis()){
sPress = true;
index = j;
break;
}
}
}
public void mouseReleased(MouseEvent e){
int releasex = e.getX();
int releasey = e.getY();
if(cPress == true){
((CircleBumper)circleBumper.get(index)).setPosition(releasex,releasey);
repaint();
cPress = false;
}
if(sPress == true){
((SquareBumper)squareBumper.get(index)).setPosition(releasex,releasey);
repaint();
sPress = false;
}
}
/**
* MouseMotionListener interface
* Override this method to act on mouse drag events.
* @param e Detected MouseEvent
*/
public void mouseDragged(MouseEvent e) {
}
/**
* MouseMotionListener interface
* Override this method to act on mouse move events.
* @param e Detected MouseEvent
*/
public void mouseMoved(MouseEvent e) {
}
/**
* We implement the KeyListener interface so that we can bump the ball in a
* random direction if keys A-J is presse.
* @modifies the ball that this listener owns
* @effects causes the ball to be bumped in a random direction but
* only if one of the keys A-J is pressed.
* @param e Detected Key Press Event
*/
public void keyPressed(KeyEvent e) {
//
int keynum = e.getKeyCode();
if ((keynum >= 65) && (keynum <= 74)) {
System.out.println("keypress " + e.getKeyCode());
ball.randomBump();
}
}
/**
* Do nothing.
* @param e Detected Key Released Event
*/
public void keyReleased(KeyEvent e) {
}
/**
* Do nothing.
* @param e Detected Key Typed Event
*/
public void keyTyped(KeyEvent e) {
}
/**
* This is the callback for the timer
* @param e ActionEvent generated by timer
*/
public void actionPerformed(ActionEvent e) {
update();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -