📄 originalsimulationgui.txt
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.InterruptedException;
import java.awt.geom.Line2D.Float;
/**
* An simple prototype GUI for the simulation.
*
* @author James M. Clarke
* @version 02/03/2007
*/
public class SimulationGUI implements ActionListener, Runnable
{
// *******************************************************************************************************
// CODE TO DRAW THE GUI - STOLEN FROM ELSEWHERE - DO NOT REUSE
private JFrame frame;
private JPanel mainPanel, bottom;
private Simulation r;
private SimulationDisplay display;
private Color backgroundColour;
private JList dataList;
private JList runwayList;
private JButton omgButton;
private JButton leftButton;
private JButton rightButton;
private JButton landButton;
private JButton queueButton;
private JButton takeOffButton;
private DefaultListModel dataModel;
private DefaultListModel runwayModel;
/**
* Constructor for objects of class Display
* DO NOT REUSE CODE IN THIS CONSTRUCTOR. Much of the code in this constructor has been plaguarized from elsewhere.
*/
public SimulationGUI(Level l)
{
frame = new JFrame();
r = new Simulation(l);
display = new SimulationDisplay(r);
mainPanel = new JPanel();
bottom = new JPanel();
omgButton = new JButton("Add plane");
leftButton = new JButton("Turn left");
rightButton = new JButton("Turn right");
landButton = new JButton("Land");
queueButton = new JButton("Queue");
takeOffButton = new JButton("Take Off");
dataModel = new DefaultListModel();
dataList = new JList(dataModel);
runwayModel = new DefaultListModel();
runwayList = new JList(runwayModel);
JScrollPane scrollPane = new JScrollPane(dataList);
JScrollPane scrollPane2 = new JScrollPane(runwayList);
(dataList.getSelectionModel()).setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
(runwayList.getSelectionModel()).setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
for(Runway aRunway : (r.getRunways()))
{
runwayModel.addElement(aRunway);
}
bottom.setLayout(new BoxLayout(bottom, BoxLayout.PAGE_AXIS));
bottom.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
bottom.add(scrollPane);
bottom.add(scrollPane2);
bottom.add(omgButton);
bottom.add(leftButton);
bottom.add(rightButton);
bottom.add(landButton);
bottom.add(queueButton);
bottom.add(takeOffButton);
omgButton.addActionListener(this);
leftButton.addActionListener(this);
rightButton.addActionListener(this);
landButton.addActionListener(this);
queueButton.addActionListener(this);
takeOffButton.addActionListener(this);
mainPanel.setLayout(new FlowLayout());
mainPanel.add(display);
mainPanel.add(bottom);
frame.setContentPane(mainPanel);
frame.setTitle("Walandablap");
frame.pack();
display.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//END STOLEN CODE
//******************************************************************************************************
// Now start the simulation
// Make a new Runnable object
// Make a new Thread for that Runnable
Thread t = new Thread(r);
// Start the thread
t.start();
Thread s = new Thread(this);
s.start();
}
// The main loop
public void refresh()
{
// redraw the screen
frame.repaint();
//update the list of planes
//look at our Simulation's queue of planes to add, if it contains anything, remove it from the queue and add it to our JList
while (!r.getToAdd().isEmpty())
{
((DefaultListModel) dataList.getModel()).addElement(r.getToAdd().poll());
}
//look at our Simulation's queue of planes to remove, if it contains anything, remove it from the queue and remove it from our JList
while (!r.getToRemove().isEmpty())
{
((DefaultListModel) dataList.getModel()).removeElement(r.getToRemove().poll());
}
}
public void run()
{
//Every 10 millisec, refresh the GUI
while(true)
{
// Redraw the display
refresh();
try
{
Thread.sleep(10);
}
//If anything goes wrong, we are fucked
catch (InterruptedException e)
{
}
}
}
// main() for this, so we can run it outside BlueJ
// If the button is pressed, have a look at what was pressed, and add an AircraftEvent to the simulation's queue
public void actionPerformed(ActionEvent event) {
// (null, 999) means add a plane
if (event.getSource().equals(omgButton)) { r.getAircraftEvents().add( new AircraftEvent(null, AircraftEvent.ADD, null) ); }
if (!dataList.getSelectionModel().isSelectionEmpty()) {
if (event.getSource().equals(landButton)) {r.getAircraftEvents().add( new AircraftEvent(((Aircraft) dataList.getSelectedValue()), AircraftEvent.LAND, ((Runway) runwayList.getSelectedValue())) ); }
if (event.getSource().equals(leftButton)) {r.getAircraftEvents().add( new AircraftEvent(((Aircraft) dataList.getSelectedValue()), AircraftEvent.TURNLEFT, ((Runway) runwayList.getSelectedValue())) ); }
if (event.getSource().equals(rightButton)) {r.getAircraftEvents().add( new AircraftEvent(((Aircraft) dataList.getSelectedValue()), AircraftEvent.TURNRIGHT, ((Runway) runwayList.getSelectedValue())) ); }
if (event.getSource().equals(queueButton)) {r.getAircraftEvents().add( new AircraftEvent(((Aircraft) dataList.getSelectedValue()), AircraftEvent.GOTOQUEUE, ((Runway) runwayList.getSelectedValue())) ); }
if (event.getSource().equals(takeOffButton)) {r.getAircraftEvents().add( new AircraftEvent(((Aircraft) dataList.getSelectedValue()), AircraftEvent.LEAVE, ((Runway) runwayList.getSelectedValue())) ); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -