// Method to refresh entire display
  public static void refresh() {
    if (!SwingUtilities.isEventDispatchThread()) return;

    worldDraw.display();
  }
Beispiel #2
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"));

  }
}
  // 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);
  }
Beispiel #4
0
  @Override
  public void run() {
    if (EditorResourceManager.getUFODirectory() == null) {
      JOptionPane.showMessageDialog(
          null,
          "No UFO directory has been set.  The preferences window will now be opened so that you can select one.  Saved game and image data will be loaded from this directory.",
          "Unknown UFO Directory - XCOMSGE",
          JOptionPane.PLAIN_MESSAGE);
      PreferencesDialog prefsDialog = new PreferencesDialog(null, true);
      prefsDialog.setVisible(true);

      if (EditorResourceManager.getUFODirectory() == null
          || prefsDialog.getResult() == PreferencesDialog.PREFERENCES_CANCELLED) {
        System.exit(0);
      }
    }

    final LoadingDialog loadingDialog = new LoadingDialog(null, true);
    /*
    loadingDialog.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowDeactivated(java.awt.event.WindowEvent e) {
            System.out.println("Detected lost focus, exiting.");
            // System.exit(0);
            loadingDialog.setVisible(false);
        }
    });
    */
    loadingDialog.setVisible(true);

    String ufodir = EditorResourceManager.getUFODirectory();
    ArrayList<ShortSaveInfo> info = new ArrayList<>();

    try {
      for (int i = 1; i <= 10; i++) {
        File savedir = new File(ufodir, "GAME_" + i);
        System.out.println("Loading save " + i + "...");
        ShortSaveInfo ssi = ShortSaveInfo.loadShortSaveInfo(savedir);
        info.add(ssi);

        if (ssi == null) {
          System.out.println("  No save in slot " + i + ".");
        } else {
          ssi.slot = i;
          System.out.println("  " + ssi);
        }
      }
    } catch (IOException ioe) {
      System.out.println("IO exception: " + ioe.getMessage());
    }

    System.out.println("Num saved games loaded: " + info.size());

    final JDialog selectDialog = new JDialog((JFrame) null, true);
    final SaveSelectionPanel ssp = new SaveSelectionPanel(info);
    ssp.addTableMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            System.out.println("Table clicked.");
            if (e.getClickCount() == 2) {
              loadSavedGame(ssp, selectDialog);
            }
          }
        });
    ssp.addLoadListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.out.println("Load button clicked.");

            loadSavedGame(ssp, selectDialog);
          }
        });
    ssp.addQuitListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.out.println("Quit button clicked.");
            selectDialog.dispose();
          }
        });

    /*
    JFrame letterFrame = new JFrame("Test");
    BoxLayout box = new BoxLayout(letterFrame.getContentPane(), BoxLayout.Y_AXIS);
    letterFrame.getContentPane().setLayout(box);
    letterFrame.getContentPane().add(new JLabel(new ImageIcon(XCOMImageResources.yellowBigLetters.get(32))));
    letterFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    letterFrame.pack();
    letterFrame.setVisible(true);
    */

    // Note that valid radii taken from OpenXCOM appear to be (in a 320x200 display):
    // 90, 120, 180, 280, 250, 720

    /*
     * Testing code
    double ww = 640.0;
    double wh = 400.0;
    double radius = 180.0;
    double cenlon = Math.PI / 4.0;
    double cenlat = 0.0;

    JFrame bufferTestFrame = new JFrame("World");
    BufferedImage worldImage = new BufferedImage(640, 400, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = worldImage.createGraphics();

    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, (int)ww, (int)wh);

    g.setColor(XCOMImageResources.palettes.get("GEOSCAPE").colors[12*16]);
    g.fillOval((int)(ww / 2.0 - radius), (int)(wh / 2.0 - radius), (int)(radius * 2.0), (int)(radius * 2.0));

    Rectangle2D texRect = new Rectangle2D.Double(0.0, 0.0, WorldTextureFile.WORLD_TEXTURE_WIDTH, WorldTextureFile.WORLD_TEXTURE_HEIGHT);

    for (WorldPolygon p : XCOMImageResources.worldPolygons) {
        p.backface = true;
        for (Vertex v : p.vertices) {
            // Orthographic projection code and backface detection taken from OpenXCOM project (Globe.cpp)
            v.x = (int)(ww / 2.0) + (int)Math.floor(radius * Math.cos(v.latitude) * Math.sin(v.longitude - cenlon));
            v.y = (int)(wh / 2.0) + (int)Math.floor(radius * (Math.cos(cenlat) * Math.sin(v.latitude) - Math.sin(cenlat) * Math.cos(v.latitude) * Math.cos(v.longitude - cenlon)));
            p.backface = p.backface && (Math.cos(cenlat) * Math.cos(v.latitude) * Math.cos(v.longitude - cenlon) + Math.sin(cenlat) * Math.sin(v.latitude)) < 0;

            if (v.x > worldImage.getWidth() || v.y > worldImage.getHeight()) {
                // p.backface = true;
                // break;
                // System.out.println("Coordinate " + v.x + ", " + v.y + " is out of bounds.");
                // System.exit(1);
            }

            // worldImage.setRGB(v.x, v.y, Color.BLACK.getRGB());
        }

        if (!p.backface) {
            g.setColor(Color.GREEN);
            g.setPaint(new TexturePaint(XCOMImageResources.smallWorldTextures.get(p.terrainType), texRect));
            g.fillPolygon(p.getAWTPolygon());
        }
    }

    JLabel worldLabel = new JLabel(new ImageIcon(worldImage));
    worldLabel.setMinimumSize(new Dimension(640, 400));
    bufferTestFrame.getContentPane().add(worldLabel);
    bufferTestFrame.pack();
    bufferTestFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    bufferTestFrame.setVisible(true);
    */

    /*
     * Background image load verification test
    JFrame frame = new JFrame("Background Test");
    JLabel background = new JLabel(new ImageIcon(XCOMImageResources.geoscapeBackground));
    background.setMinimumSize(new Dimension(XCOMImageResources.geoscapeBackground.getWidth(), XCOMImageResources.geoscapeBackground.getHeight()));
    background.setPreferredSize(new Dimension(XCOMImageResources.geoscapeBackground.getWidth(), XCOMImageResources.geoscapeBackground.getHeight()));
    frame.getContentPane().add(background);
    frame.pack();
    frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    */

    /*
     * World view verification test
     */

    JFrame frame = new JFrame("Background Test");
    frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout());
    // frame.getContentPane().add(new JLabel(new
    // ImageIcon(XCOMImageResources.geoscapeBackground.getSubimage(0, 0, 256,
    // XCOMImageResources.geoscapeBackground.getHeight()))));
    final WorldView wv = new WorldView(3);
    wv.setZoom(zooms[zoomIndex]);
    wv.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            System.out.println(
                "Mouse clicked: " + e.getX() + ", " + e.getY() + " (" + e.getButton() + ")");
            if (e.getButton() == e.BUTTON1) {
              wv.recenter(e.getX(), e.getY());
            } else if (e.getButton() == e.BUTTON3) {
              zoomIndex = (zoomIndex + 1) % zooms.length;
              wv.setZoom(zooms[zoomIndex]);
            }
          }
        });
    frame.getContentPane().add(wv);
    frame.pack();
    frame.setVisible(true);

    selectDialog.setTitle("Select Saved Game - XCOMSGE");
    selectDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    selectDialog.add(ssp);
    selectDialog.pack();
    selectDialog.dispose();
    // selectDialog.setVisible(true);

    System.out.println("Initial thread closed.");
  }