// process a mouseClicked event // select the loop if the mouse was clicked within the edges of // the border public void mouseClicked(MouseEvent mouseEvent) { int x = mouseEvent.getX(); int y = mouseEvent.getY(); Rectangle bounds = getBounds(); // left, top, bottom, right edge if ((x >= 0 && x <= SELECT_BORDER_SIZE) || (y >= 0 && y <= SELECT_BORDER_SIZE) || (y >= bounds.height - SELECT_BORDER_SIZE && y <= bounds.height) || (x >= bounds.width - SELECT_BORDER_SIZE && x <= bounds.width)) { if (isSelected()) program.setAllSelected(false); else { program.setAllSelected(false); setSelected(true); program.updateLoopDialog(); } program.updateState(); repaint(); } else super.mouseClicked(mouseEvent); }
void place(int mx, int my) { if (drme) { drme = false; return; } int n = data.a.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (((mx - data.a[i][j].x) * (mx - data.a[i][j].x) + (my - data.a[i][j].y) * (my - data.a[i][j].y) < bst.scale * bst.scale * .75) && data.a[i][j].getState() == 0) { if (count == 1 && gst.swap) { int ch = JOptionPane.showConfirmDialog( this, "Swap moves?", "Swap", JOptionPane.YES_NO_OPTION); if (ch == JOptionPane.YES_OPTION) { lmv.setState(cst); switch (cst) { case 1: data.a[i][j].setState(2); break; case 2: data.a[i][j].setState(1); break; } repaint( (int) (lmv.x - 0.8 * bst.scale), (int) (lmv.y - 0.8 * bst.scale), (int) (1.6 * bst.scale), (int) (1.6 * bst.scale)); if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) { newGame(); } if (gst.ai != null) gst.ai.updateAI(lmv.i, lmv.j, lmv.getState(), count); } else data.a[i][j].setState(cst); } else data.a[i][j].setState(cst); lmv = data.a[i][j]; repaint( (int) (data.a[i][j].x - 0.8 * bst.scale), (int) (data.a[i][j].y - 0.8 * bst.scale), (int) (1.6 * bst.scale), (int) (1.6 * bst.scale)); switch (cst) { case 1: cst = 2; break; case 2: cst = 1; break; } if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) { System.out.println("Game Over"); firstrun = true; String win = ""; if (lmv.getState() == Block.RED_BEAD) { switch (gst.theme) { case 0: win = "Red"; break; case 1: win = "Black"; break; case 2: win = "Gold"; break; } } else { switch (gst.theme) { case 0: win = "Blue"; break; case 1: win = "Green"; break; case 2: win = "Silver"; break; } } if (gst.audio) AePlayWave.play(AePlayWave.END); JOptionPane.showMessageDialog( this, win + " wins", "Game Over", JOptionPane.INFORMATION_MESSAGE); newGame(); return; } if (gst.audio) { if (gst.ai == null) AePlayWave.play(AePlayWave.PLACE); else if (userMove) AePlayWave.play(AePlayWave.AIMOVE); } count++; if (gst.ai != null && userMove) { userMove = false; gst.ai.nextMove(lmv.i, lmv.j, lmv.getState(), count); } else userMove = true; return; } } } if (gst.audio) AePlayWave.play(AePlayWave.BLOCK); }
/** Called when run from the command line. The train module will run (mostly) individually. */ public static void main(String[] args) { try { isSolo = true; // Get the number of trains. if (args.length != 1) { System.out.println("Invalid number of arguments."); System.exit(1); } soloNumTrains = Integer.parseInt(args[0], 10); if ((soloNumTrains < 1) || (soloNumTrains > 9)) { System.out.println( "Invalid argument. The number of trains must be greater than 0 and less than 10."); System.exit(1); } // Create the test block loop. Block bYard = new Block( 0, "A", "I", 0.0, 0.0, 5.0, true, false, true, false, false, "", false, false, false); Block b01 = new Block( 1, "B", "II", 500.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b02 = new Block( 2, "C", "II", 500.0, -22.2, 1.0, true, false, false, false, false, "", false, false, false); Block b03 = new Block( 3, "D", "II", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b04 = new Block( 4, "E", "II", 500.0, 11.1, 30.0, true, true, false, false, false, "", false, false, false); Block b05 = new Block( 5, "F", "III", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b06 = new Block( 6, "G", "III", 100.0, 0.0, 5.0, true, false, false, true, false, "Alpha", false, false, false); Block b07 = new Block( 7, "H", "III", 500.0, -11.1, 30.0, true, false, false, false, false, "", false, false, false); Block b08 = new Block( 8, "I", "III", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b09 = new Block( 9, "J", "IV", 500.0, 22.2, 1.0, true, false, false, false, false, "", false, false, false); Block b10 = new Block( 10, "K", "IV", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b11 = new Block( 11, "L", "IV", 100.0, 0.0, 5.0, true, false, false, true, false, "Beta", false, false, false); Block b12 = new Block( 12, "M", "IV", 50.0, 11.1, 15.0, true, false, false, false, false, "", false, false, false); Block b13 = new Block( 13, "N", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b14 = new Block( 14, "O", "V", 50.0, -11.1, 15.0, true, false, false, false, false, "", false, false, false); Block b15 = new Block( 15, "P", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b16 = new Block( 16, "Q", "V", 100.0, 0.0, 5.0, true, false, false, true, false, "Gamma", false, false, false); Block b17 = new Block( 17, "R", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); bYard.connect(b17, b01); b01.connect(bYard, b02); b02.connect(b01, b03); b03.connect(b02, b04); b04.connect(b03, b05); b05.connect(b04, b06); b06.connect(b05, b07); b07.connect(b08, b06); b08.connect(b09, b07); b09.connect(b10, b08); b10.connect(b11, b09); b11.connect(b12, b10); b12.connect(b11, b13); b13.connect(b14, b12); b14.connect(b13, b15); b15.connect(b16, b14); b16.connect(b15, b17); b17.connect(bYard, b16); ArrayList<Block> route = new ArrayList<Block>(); route.add(bYard); route.add(b01); route.add(b02); route.add(b03); route.add(b04); route.add(b05); route.add(b06); route.add(b07); route.add(b08); route.add(b09); route.add(b10); route.add(b11); route.add(b12); route.add(b13); route.add(b14); route.add(b15); route.add(b16); route.add(b17); route.add(bYard); route.add(b01); route.add(b02); route.add(b03); route.add(b04); route.add(b05); route.add(b06); route.add(b07); route.add(b08); route.add(b09); route.add(b10); route.add(b11); route.add(b12); route.add(b13); route.add(b14); route.add(b15); route.add(b16); route.add(b17); route.add(bYard); /* * Create the trains. * The first will depart at 8:00 AM, the second at 8:30 AM, the third at 9:00 AM, etc. * The first will depart at 8:00 AM, the second at 8:15 AM, the third at 8:30 AM, etc. * All will have break time set to 4 hours after their departure times. */ trainList = new ArrayList<Train>(); idArray = new String[soloNumTrains]; for (int i = 0; i < soloNumTrains; i++) { trainList.add( new Train( i + 1, "T" + (i + 1), "Test", (8 * 60 * 60 + i * 15 * 60) % (24 * 60 * 60), route, new Engineer( true, false, 0.0, (8 * 60 * 60 + i * 15 * 60 + 5 * 60) % (24 * 60 * 60)), bYard)); idArray[i] = new String("T" + (i + 1)); } // Create the UI. TrainModelUI tnmUI = new TrainModelUI(); tnmUI.setTrainList(trainList); // Setup the timer. soloTime = 7 * 60 * 60 + 59 * 60 + 55; soloDate = new Date(93, 2, 2, 7, 59, 55); ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { timeTick(soloDate, soloDelta); } }; new javax.swing.Timer(20, taskPerformer).start(); // Disable all "Toggle" buttons that are associated with a manual entry. btnToggleManRecPower.setEnabled(false); btnToggleManDesSpdLmt.setEnabled(false); btnToggleManLights.setEnabled(false); btnToggleManDoors.setEnabled(false); btnToggleManTarTemperature.setEnabled(false); // Make it so only manual values are used. for (int i = 0; i < soloNumTrains; i++) { Train t = trainList.get(i); t.issetManualPower = true; t.issetManualSpeedLimit = true; t.issetLightsOnUseManual = true; t.issetDoorsOpenUseManual = true; t.issetTargetTemperatureManual = true; } btnPauseResume.setEnabled(true); tnmUI.setSelectedId(trainList.get(0).id); tnmUI.setIsPaused(tnmUI.getIsPaused()); tnmUI.setIsVisible(true); } catch (Exception e) { e.printStackTrace(System.err); JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); } }
// set the selection state and update the GUI and loop dialog public void setSelected(boolean flag) { super.setSelected(flag); program.updateState(); program.updateLoopDialog(); }
// set the selection state of all sub-components public void setAllSelected(boolean flag) { setSelected(flag); super.setAllSelected(flag); }
public void setProgram(Program program) { this.program = program; super.setProgram(program); }