📄 simulationdisplay.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.InterruptedException;
import java.awt.geom.Line2D.Float;
/**
* A GUI object which illustrates the simulation.
* All SimulationDisplay objects must have the method init() used, after they have been pack()ed into their parent object, but before the are made visible.
*
* @author James M. Clarke
* @version 12/03/2007
*/
public class SimulationDisplay extends JPanel
{
private Image myImage;
private Graphics2D graphic;
private Simulation r;
private Dimension size;
/**
* Constructor for SimulationDisplay objects
* @param in the simulation which should be displayed
*/
public SimulationDisplay(Simulation in)
{
super();
r = in;
this.setPreferredSize(new Dimension(500, 500));
}
/**
* Initialize an object that has already been pack()ed into a parent object
*/
public void init()
{
size = this.getSize();
myImage = this.createImage(size.width, size.height);
graphic = (Graphics2D)myImage.getGraphics();
}
/**
* Draws the simulation. Runways and aircraft are drawn, and the display is refreshed.
* @param g the Graphics object to draw to
* @see JPanel#paint
*/
public void paint(Graphics g)
{
// Clear the screen to be all white
graphic.setColor(Color.black);
graphic.fillRect(0, 0, 500, 500);
graphic.setColor(Color.green);
graphic.draw( new java.awt.geom.Ellipse2D.Float(0, 0, 499, 499) );
graphic.draw( new java.awt.geom.Line2D.Float(10, 490, 35, 490) );
graphic.draw( new java.awt.geom.Line2D.Float(35, 487, 35, 490) );
graphic.draw( new java.awt.geom.Line2D.Float(10, 487, 10, 490) );
graphic.drawString("4 miles", 10, 480);
graphic.setColor(Color.green);
// Draw on the runways
for (Runway b : ((Simulation) r).getRunways())
{
b.draw(graphic);
}
// Draw on the aircraft
for (Aircraft b : ((Simulation) r).getAircraft())
{
b.draw(graphic);
}
// Draw this all onto the graphics we have been given
g.drawImage(myImage, 0, 0, null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -