示例#1
0
  // Main program -- create and start GUI
  public Mesh(boolean debug) {
    // Create drawing area for shape
    worldDraw = new WorldView(this, shape, debug);
    worldDraw.setSize(500, 500);

    // Create menubar
    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);

    JMenu menu = new JMenu("File");
    menu.getPopupMenu().setLightWeightPopupEnabled(false);
    menubar.add(menu);

    // Exit when quit selected
    JMenuItem resetm = menu.add("Reset");
    resetm.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Parameter.blockAction(true);

            // Reset all parameters
            shape.reset();

            Parameter.blockAction(false);

            Parameter.onUserAction();
          }
        });

    // Exit when quit selected
    JMenuItem quitm = menu.add("Quit");
    quitm.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.exit(0);
          }
        });

    // ------------------------------------------------------

    // Lay out main window
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints con = new GridBagConstraints();

    Container c = getContentPane();
    c.setLayout(layout);

    // World viewport
    con.gridwidth = 1;
    con.weightx = 0.2;
    con.weighty = 1.0;
    con.fill = GridBagConstraints.BOTH;
    con.insets = new Insets(4, 4, 4, 2);
    layout.setConstraints(worldDraw, con);
    c.add(worldDraw);

    // -- Parameter controls

    // Make container for controls
    Container cc = new Container();

    con.gridwidth = GridBagConstraints.REMAINDER;
    con.weightx = 0.2;
    con.weighty = 1.0;
    con.fill = GridBagConstraints.HORIZONTAL;

    layout.setConstraints(cc, con);
    c.add(cc);

    // Fill compartment
    GridBagLayout clayout = new GridBagLayout();
    cc.setLayout(clayout);
    GridBagConstraints ccon = new GridBagConstraints();

    ccon.weightx = 1.0;
    ccon.weighty = 1.0;
    ccon.anchor = GridBagConstraints.NORTH;
    ccon.insets = new Insets(10, 10, 0, 10);
    ccon.fill = GridBagConstraints.BOTH;
    ccon.gridwidth = GridBagConstraints.REMAINDER;
    makeControls(cc, clayout, ccon, shape.getParams(), shape.getOptions(), "Object parameters");

    // ------------------------------------------------------

    // Exit when window closes
    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });

    // Placement of window on screen
    setLocation(100, 50);

    pack();
    setVisible(true);
  }
示例#2
0
  // Method to refresh entire display
  public static void refresh() {
    if (!SwingUtilities.isEventDispatchThread()) return;

    worldDraw.display();
  }
示例#3
0
public class Example1 extends Component {

  private static final long serialVersionUID = -5770594287721220257L;
  static Logger log = LoggerFactory.getLogger(Example1.class);

  static SimulationHelper helper;
  BufferedImage background = WorldView.getImage();
  BufferedImage person = AgentView.getImage();

  public void paint(Graphics g) {
    if (helper != null) helper.drawWorld();
  }

  public Example1() {
    setPreferredSize(getPreferredSize());
    setVisible(true);
  }

  public Dimension getPreferredSize() {
    if (background == null) return new Dimension(100, 100);
    else return new Dimension(background.getWidth(null), background.getHeight(null));
  }

  /** @param args */
  public static void main(String[] args) {
    JPanel boxPane = new JPanel(new BorderLayout());
    // boxPane.setLayout(new BoxLayout(boxPane, BoxLayout.LINE_AXIS));
    // boxPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    // boxPane.add(Box.createVerticalGlue());
    Example1 ex = new Example1();
    // boxPane.add(ex);

    boxPane.add(Box.createRigidArea(new Dimension(10, 0)));
    Button btnStartStop = new Button("Start / Stop");
    btnStartStop.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent event) {
            log.info("Suspending / Resuming the simulation thread, current state = " + helper.stop);
            if (helper.stop == true) helper.unpause();
            else helper.pause();
          }
        });
    // btnStartStop.setMaximumSize(new Dimension(100, 100));
    boxPane.add(btnStartStop, BorderLayout.PAGE_START);

    JScrollPane worldScrollPane = new JScrollPane(ex);

    final JSplitPane splitPane =
        new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, worldScrollPane, boxPane);
    splitPane.setResizeWeight(0.5);
    splitPane.setOneTouchExpandable(true);
    splitPane.setContinuousLayout(true);

    worldScrollPane.addAncestorListener(
        new AncestorListener() {
          @Override
          public void ancestorRemoved(AncestorEvent arg0) {
            splitPane.repaint();
          }

          @Override
          public void ancestorMoved(AncestorEvent arg0) {
            splitPane.repaint();
          }

          @Override
          public void ancestorAdded(AncestorEvent arg0) {
            splitPane.repaint();
          }
        });

    JFrame f = new JFrame();
    f.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
    f.add(splitPane, BorderLayout.CENTER);
    // f.add(boxPane);
    f.pack();
    f.setVisible(true);

    helper = new SimulationHelper(ex.getGraphics());

    helper.start();

    // layeredPane.setBorder(BorderFactory.createTitledBorder("Move the Mouse to Move Duke"));

  }
}