/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jgrow; import java.awt.Graphics; import java.awt.Color; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JPanel; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; /** * * @author maryam */ public final class PanelSimulation extends JPanel implements MouseListener, MouseMotionListener, ActionListener { private int inTop, inLeft; public AnimationController nip; public PanelInformation inf; int magnifyBoxDim = 54, magnifyBoxX = 0, magnifyBoxY = 0, magnifyOffset = magnifyBoxDim / 3, magnifyInitX = 0, magnifyInitY = 0, magnifyFactor = 3, magnifyAway = 2; int zoomFactor = 1; boolean magnifyMoving = false; boolean magnify = false; public int seedXX, seedYY; public PanelSimulation(boolean doubleBuffer, InfoTiles t, InfoBonds b) { super(doubleBuffer); inTop = getInsets().top; inLeft = getInsets().left; // setBackground(Color.red); setVisible(true); repaint(); addMouseMotionListener(this); addMouseListener(this); inf = new PanelInformation(t, b); nip = new AnimationController(this, t, b, inf); //mainWin = win; } public java.awt.Image createSimulationImage(java.awt.image.ImageProducer source) { return createImage(source); } /* public void roundCompleted() { mainWin.roundCompleted(); } */ @Override public void update(Graphics g) { paint(g); } @Override public void paint(Graphics g) { super.paint(g); if (nip != null && nip.animationImage != null) { // g.drawImage(nip.animationImage, inLeft + magnifyOffset * zoomFactor, inTop + magnifyOffset * zoomFactor, boardSize * zoomFactor, boardSize * zoomFactor, this); g.drawImage(nip.animationImage, 0,0, getSize().width, getSize().height, this); } if (magnify) { magnifyDrawBox(g); g.setColor(Color.red); g.drawRect(magnifyBoxX * zoomFactor, magnifyBoxY * zoomFactor, zoomFactor * magnifyBoxDim - 1, zoomFactor * magnifyBoxDim - 1); } } public void commandSetZoom(int newZoom) { zoomFactor = newZoom; } public void setMagnifiable(boolean in) { if (in) { magnifyBoxX = 0; magnifyBoxY = 0; } magnify = in; repaint(); } void magnifyDrawBox(Graphics g) { g.clipRect(magnifyBoxX * zoomFactor, magnifyBoxY * zoomFactor, magnifyBoxDim * zoomFactor, magnifyBoxDim * zoomFactor); g.drawImage(nip.animationImage, -magnifyAway * magnifyBoxX * zoomFactor, -magnifyAway * magnifyBoxY * zoomFactor, nip.boardSize * magnifyFactor * zoomFactor, nip.boardSize * magnifyFactor * zoomFactor, null); } public void mouseDragged(MouseEvent e) { if (magnifyMoving) { if (e.getX() - magnifyInitX > 0 && e.getX() - magnifyInitX < nip.boardSize * zoomFactor) { magnifyBoxX = (e.getX() - magnifyInitX) / zoomFactor; } if (e.getY() - magnifyInitY > 0 && e.getY() - magnifyInitY < nip.boardSize * zoomFactor) { magnifyBoxY = (e.getY() - magnifyInitY) / zoomFactor; } } repaint(); } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && magnify) { int xx = e.getX(); int yy = e.getY(); if (xx > magnifyBoxX * zoomFactor && xx < (magnifyBoxX + magnifyBoxDim) * zoomFactor && yy > magnifyBoxY * zoomFactor && yy < (magnifyBoxY + magnifyBoxDim) * zoomFactor) { if (magnifyFactor == 3) { magnifyFactor = 5; magnifyAway = 4; } else { magnifyFactor = 3; magnifyAway = 2; } repaint(); } } else if (e.getButton() == MouseEvent.BUTTON3) { int x = e.getX(), y = e.getY(); if (!nip.isRunning) { JPopupMenu seedMenu = new JPopupMenu("Placing Tiles"); JMenuItem eachTile = new JMenuItem("Simulation not started"); seedMenu.add(eachTile); add(seedMenu); seedMenu.show(this, x, y); return; } seedXX = (int) ( (x * 1.0 / getSize().width) * nip.boardSize); seedYY = (int) ( (y * 1.0 / getSize().height) * nip.boardSize); JPopupMenu seedMenu; if (nip.hasTile(seedXX, seedYY)) { seedMenu = new JPopupMenu("Remove or Change"); } else { seedMenu = new JPopupMenu("Place Tile"); } JMenuItem eachTile; InfoTiles win = nip.tiles; int num; if (win != null && (num = win.size()) > 0) { for (int i = 0; i < num; i++) { eachTile = new JMenuItem(win.names[i]); eachTile.setActionCommand("addSeed" + i); eachTile.addActionListener(this); seedMenu.add(eachTile); } } else { eachTile = new JMenuItem("No Tiles"); eachTile.setEnabled(false); seedMenu.add(eachTile); } if (nip.hasTile(seedXX, seedYY)) { eachTile = new JMenuItem("Remove"); eachTile.setActionCommand("removeSeed"); eachTile.addActionListener(this); seedMenu.add(eachTile); } eachTile = new JMenuItem("Info"); eachTile.setActionCommand("infoTile"); eachTile.addActionListener(this); seedMenu.add(eachTile); add(seedMenu); seedMenu.show(this, x, y); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && magnify) { int xx = e.getX(); int yy = e.getY(); if (xx > magnifyBoxX * zoomFactor && xx < (magnifyBoxX + magnifyBoxDim) * zoomFactor && yy > magnifyBoxY * zoomFactor && yy < (magnifyBoxY + magnifyBoxDim) * zoomFactor) { magnifyInitX = xx - magnifyBoxX * zoomFactor; magnifyInitY = yy - magnifyBoxY * zoomFactor; magnifyMoving = true; } } else if (e.getButton() == MouseEvent.BUTTON2) { } } public void mouseReleased(MouseEvent e) { magnifyMoving = false; } public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String sourceName = source.getText(); String actionCommand = e.getActionCommand(); if ((actionCommand = e.getActionCommand()).indexOf("addSeed") > -1) { if (!nip.isRunning) { return; } try { nip.placeSeedTile(seedXX, seedYY, Integer.parseInt(actionCommand.substring(7))); repaint(); } catch (Exception ex) { ex.printStackTrace(); } } else if (actionCommand.equals("removeSeed")) { if (!nip.isRunning) { return; } nip.removeSeedTile(seedXX, seedYY); repaint(); } else if (actionCommand.equals("infoTile")) { if (!nip.isRunning) { return; } JPopupMenu infoPop = new JPopupMenu("Info"); infoPop.add(new JMenuItem(nip.infoBoard(seedXX, seedYY))); add(infoPop); infoPop.show(this, seedXX, seedYY); repaint(); } } public void loadExample(int ver) { nip.stopSimulation(); nip.tiles.reset(); nip.bonds.reset(); switch(ver) { case 1: commandLoadFile(new java.io.BufferedReader(new java.io.StringReader(Examples.sierpinski))); break; case 2: commandLoadFile(new java.io.BufferedReader(new java.io.StringReader(Examples.proof))); break; default: } } public void commandLoadFile(java.io.BufferedReader toLoad) { try { if(!toLoad.readLine().equals("bonds")) return; int toLoadN = Integer.parseInt(toLoad.readLine()); for(int i=1; i<toLoadN+1; i++) { double [] toLoadStrengths = new double[i]; for(int j=0; j<i; j++) { toLoadStrengths[j] = Double.parseDouble(toLoad.readLine()); } String toLoadName = toLoad.readLine(); if(!nip.bonds.addBond(toLoadStrengths,toLoadName)) return; } nip.bonds.gse = Double.parseDouble(toLoad.readLine()); if(!toLoad.readLine().equals("tiles")) return; toLoadN = Integer.parseInt(toLoad.readLine()); for(int i=0; i<toLoadN; i++) { int [] toLoadBonds = new int[4]; for(int j=0; j<4; j++) { toLoadBonds[j] = Integer.parseInt(toLoad.readLine()); } int toLoadColR = Integer.parseInt(toLoad.readLine()); int toLoadColG = Integer.parseInt(toLoad.readLine()); int toLoadColB = Integer.parseInt(toLoad.readLine()); String toLoadConc = toLoad.readLine(); String toLoadName = toLoad.readLine(); if(!nip.tiles.addTile(toLoadBonds,toLoadColR,toLoadColG,toLoadColB,toLoadConc,toLoadName)) return; } nip.tiles.gmc = Double.parseDouble(toLoad.readLine()); nip.updateVars(); } catch(Exception e1) { e1.printStackTrace(); System.out.println("File format incorrect."); } } }