📄 airportbuilderanimation.java.svn-base
字号:
package gui;import java.awt.*;import java.awt.image.*;import java.awt.event.*;import java.util.*;import java.net.*;import java.io.*;import javax.swing.*;import javax.imageio.*;import world.*;import base.*;public class AirportBuilderAnimation extends JPanel implements MouseListener, MouseMotionListener { final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR); final Cursor CROSSHAIR_CURSOR = new Cursor(Cursor.CROSSHAIR_CURSOR); AirportBuilder ab; Animation animation; BuilderObject newBuilderObject; public static final int runwayWidth = 600, runwayHeight = 40, gateWidth = 50, gateHeight = 25, towerWidth = 50, towerHeight = 25; public static final String runwayTexture = "resources/textures/runway.jpg"; public static final Color runwayColor = Color.BLACK, gateColor = Color.CYAN, towerColor = Color.ORANGE; public AirportBuilderAnimation(AirportBuilder ab) { this.ab = ab; animation = new Animation(); setBackground(Color.darkGray); addMouseListener(this); addMouseListener(ab); addMouseMotionListener(this); } public void makeNewTower() { newBuilderObject = new BuilderObject("Tower1", new GuiObject("Tower"), new WorldObjectState(), towerWidth, towerHeight); newBuilderObject.setBGColor(towerColor); //generate the vertex offsets ArrayList<WorldVector> vertices = new ArrayList<WorldVector>(); vertices.add(new WorldVector(-(towerWidth / 2), -(towerHeight / 2), 0)); vertices.add(new WorldVector((towerWidth / 2), -(towerHeight / 2), 0)); vertices.add(new WorldVector((towerWidth / 2), (towerHeight / 2), 0)); vertices.add(new WorldVector(-(towerWidth / 2), (towerHeight / 2), 0)); newBuilderObject.setVertices(vertices); } public void makeNewRunway(double angle) { newBuilderObject = new BuilderObject("Runway1", new GuiObject("Runway"), new WorldObjectState(), runwayWidth, runwayHeight); newBuilderObject.setBGColor(runwayColor); newBuilderObject.setTexture(runwayTexture); newBuilderObject.angle = -angle; //calculate angularPosition based on the passed in angle //WorldVector angularPosition = new WorldVector((runwayWidth * Math.cos(Math.toRadians(angle)) / 2), (runwayWidth * Math.sin(Math.toRadians(angle)) / 2), 0); WorldVector angularPosition = new WorldVector(0, 0, 0); //generate the vertex offsets ArrayList<WorldVector> vertices = new ArrayList<WorldVector>(); vertices.add(new WorldVector(-(runwayWidth / 2), -(runwayHeight / 2), 0)); vertices.add(new WorldVector((runwayWidth / 2), -(runwayHeight / 2), 0)); vertices.add(new WorldVector((runwayWidth / 2), (runwayHeight / 2), 0)); vertices.add(new WorldVector(-(runwayWidth / 2), (runwayHeight / 2), 0)); newBuilderObject.setVertices(vertices); newBuilderObject.setangularPosition(angularPosition); } public void makeNewGate() { newBuilderObject = new BuilderObject("Gate1", new GuiObject("Gate"), new WorldObjectState(), gateWidth, gateHeight); newBuilderObject.setBGColor(gateColor); //generate the vertex offsets ArrayList<WorldVector> vertices = new ArrayList<WorldVector>(); vertices.add(new WorldVector(-(gateWidth / 2), -(gateHeight / 2), 0)); vertices.add(new WorldVector((gateWidth / 2), -(gateHeight / 2), 0)); vertices.add(new WorldVector((gateWidth / 2), (gateHeight / 2), 0)); vertices.add(new WorldVector(-(gateWidth / 2), (gateHeight / 2), 0)); newBuilderObject.setVertices(vertices); } public void updateNewGuiObjectPosition(Point p) { //draw the builder object according to the mouses position newBuilderObject.setPosition(p); repaint(); } public void paintComponent(Graphics g) { super.paintComponents(g); Animation.setGraphics(g); g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); //if the user is adding something at the moment, draw it if(ab.isAddingTower() || ab.isAddingRunway() || ab.isAddingGate()) { animation.drawObject(newBuilderObject); } //if there are saved builder objects, draw them if(!ab.getBuilderObjects().isEmpty()) { animation.drawBuilderObjects(ab.getBuilderObjects()); } } public void mouseMoved(MouseEvent e) { if(ab.isAddingRunway() || ab.isAddingTower() || ab.isAddingGate()) { //if drawing, update object's new position updateNewGuiObjectPosition(e.getPoint()); } } public void mouseClicked(MouseEvent e) { if(ab.isAddingTower()) { //add the tower ab.finishAddingTower(newBuilderObject); } else if(ab.isAddingRunway()) { //add the runway GuiObject runwayGuiObject = ((GuiObject) newBuilderObject.getGuiObject()); WorldObjectState runwayObjectState = ((WorldObjectState) newBuilderObject.getObjectState()); BuilderObject runwayBuilderObject = new BuilderObject(newBuilderObject.getName(), runwayGuiObject, runwayObjectState, runwayWidth, runwayHeight); ab.finishAddingRunway(runwayBuilderObject); } else if(ab.isAddingGate()) { //add the gate ab.finishAddingGate(newBuilderObject); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -