Пример #1
0
  /**
   * Constructs a new game.
   *
   * @param web <code>true</code> if this game is meant to be an applet (which can be played
   *     online), and <code>false</code> otherwise. Note: this doesn't work yet.
   */
  public Game() {
    Game.applet = false;
    canvas = new Canvas(this);
    solidShapes = Collections.newSetFromMap(new ConcurrentHashMap<Shape, Boolean>());
    allShapes = Collections.newSetFromMap(new ConcurrentHashMap<Shape, Boolean>());

    // TODO: sort out which data structures actually have to support concurrency
    layerContents = new ConcurrentHashMap<Integer, java.util.List<Shape>>();
    layers = new CopyOnWriteArrayList<Integer>();
    layerOf = new ConcurrentHashMap<Shape, Integer>();

    counters = new ArrayList<Counter>();

    Mouse mouse = new Mouse();
    if (applet) {
      addMouseMotionListener(mouse);
      addMouseListener(mouse);
      addKeyListener(new Keyboard());
    } else {
      frame = new JFrame();
      frame.addMouseMotionListener(mouse);
      frame.addMouseListener(mouse);
      frame.addKeyListener(new Keyboard());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    setDefaults();
  }
Пример #2
0
 public static void main(String[] args) {
   JFrame frame = new JFrame("Cwiczenie5_4");
   Container cp = frame.getContentPane();
   Cwiczenie5_4 Cwiczenie5_4 = new Cwiczenie5_4();
   cp.add(Cwiczenie5_4);
   frame.addKeyListener(Cwiczenie5_4.bar);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setResizable(false);
   frame.setLocation(300, 300);
   frame.pack();
   frame.show();
   Cwiczenie5_4.startGame();
 }
  public static void main(String s[]) {

    // Getting save directory
    String saveDir;
    if (s.length > 0) {
      saveDir = s[0];
    } else {
      saveDir =
          JOptionPane.showInputDialog(
              null,
              "Please enter directory where "
                  + "the images is/will be saved\n\n"
                  + "Also possible to specifiy as argument 1 when "
                  + "running this program.",
              "l:\\webcamtest");
    }

    String layout = "";
    if (s.length > 1) {
      layout = s[1];
    }

    // Move mouse to the point 5000,5000 px (out of the screen)
    Robot rob;
    try {
      rob = new Robot();
      rob.setAutoDelay(500); // 0,5 s
      rob.mouseMove(5000, 5000);
    } catch (AWTException e) {
      e.printStackTrace();
    }

    // Make the main window
    JFrame frame = new JFrame();
    frame.setAlwaysOnTop(true);
    frame.setTitle(
        "Webcam capture and imagefading - "
            + "Vitenfabrikken Jærmuseet - "
            + "made by Hallvard Nygård - "
            + "Vitenfabrikken.no / Jaermuseet.no");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setUndecorated(true);

    WebcamCaptureAndFadePanel panel = new WebcamCaptureAndFadePanel(saveDir, layout);
    frame.getContentPane().add(panel);
    frame.addKeyListener(panel);
    frame.pack();

    frame.setVisible(true);
  }
 public static void enableFullscreen(final JFrame window, final boolean useExclusiveFullscreen) {
   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   if (appleEawtAvailable()
       && appleOSVersion() >= 7
       && // lion and above
       javaVersion() >= 7) { // java 7 and above
     System.out.println("trying to apple fullscreen");
     enableAppleFullscreen(window);
   }
   window.addKeyListener(
       new KeyAdapter() {
         @Override
         public void keyReleased(KeyEvent e) {
           if (e.getKeyCode() == KeyEvent.VK_F11) {
             e.consume();
             GraphicsDevice gd = getScreen(window);
             window.setVisible(false); // hide, so we can do stuff
             if (window.isUndecorated()) { // we are fullscreen, we should become unfullscreen
               window.setUndecorated(false);
               if (window.equals(
                   gd.getFullScreenWindow())) { // we used exclusive mode to go fullscreen
                 gd.setFullScreenWindow(null);
               } else { // we just got really big
                 window.setExtendedState(JFrame.NORMAL);
               }
             } else { // we aren't fullscreen, we should become fullscreen
               if (javaVersion() >= 7) setAutoRequestFocus(window, true);
               window.setUndecorated(true);
               window.setBounds(gd.getDefaultConfiguration().getBounds());
               if (useExclusiveFullscreen) {
                 gd.setFullScreenWindow(window);
               } else {
                 window.setExtendedState(JFrame.MAXIMIZED_BOTH);
                 window.toFront();
               }
             }
             window.pack();
             window.setVisible(true);
           }
         }
       });
 }
Пример #5
0
  // init
  private static void init() {
    if (frame != null) frame.setVisible(false);
    frame = new JFrame();
    offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    offscreen = offscreenImage.createGraphics();
    onscreen = onscreenImage.createGraphics();
    setXscale();
    setYscale();
    offscreen.setColor(DEFAULT_CLEAR_COLOR);
    offscreen.fillRect(0, 0, width, height);
    setPenColor();
    setPenRadius();
    setFont();
    clear();

    // add antialiasing
    RenderingHints hints =
        new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    offscreen.addRenderingHints(hints);

    // frame stuff
    ImageIcon icon = new ImageIcon(onscreenImage);
    JLabel draw = new JLabel(icon);

    draw.addMouseListener(std);
    draw.addMouseMotionListener(std);

    frame.setContentPane(draw);
    frame.addKeyListener(std); // JLabel cannot get keyboard focus
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows
    // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      // closes only current window
    frame.setTitle("Standard Draw");
    frame.setJMenuBar(createMenuBar());
    frame.pack();
    frame.requestFocusInWindow();
    frame.setVisible(true);
  }
Пример #6
0
  public HotSMain() {
    // General
    setPreferredSize(new Dimension(800, 600));
    canvas.add(this);
    setBackground(Color.GRAY);
    frame.setResizable(false);
    frame.setTitle("Heroes of the Forest");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Overworld
    menuScreen.setPreferredSize(new Dimension(800, 150));
    menuScreen.setBackground(Color.DARK_GRAY);
    menuScreen.setVisible(false);

    menuButtons.add(itemsButton);
    menuButtons.add(equipButton);
    menuButtons.add(skillsButton);
    menuButtons.add(statusButton);
    menuButtons.add(settingsButton);
    menuButtons.add(dataButton);

    for (JButton button : menuButtons) {
      menuScreen.add(button);
      button.addActionListener(this);
    }

    canvas.add(menuScreen, BorderLayout.SOUTH);
    ImageIcon bg = new ImageIcon("OverworldBG.jpg");
    background = bg.getImage();

    frame.pack();
    frame.setVisible(true);
    player = new Player();
    enemies.add(new Stalker(375, 200));
    player.defaultPlayer();
    frame.addKeyListener(this);
    frame.addMouseListener(this);
    frame.addMouseMotionListener(this);
    moveTimer.start();
  }
Пример #7
0
  static void buildGUI() {
    // Need this size to balance axes.
    frame.setSize(520, 690);
    frame.setTitle("DrawTool");
    frame.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });

    Container cPane = frame.getContentPane();

    // Status label on top. Unused for now.
    statusLabel.setOpaque(true);
    statusLabel.setBackground(Color.white);
    cPane.add(statusLabel, BorderLayout.NORTH);

    // Build the input/output elements at the bottom.
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.black));
    panel.setBackground(inputPanelColor);
    panel.setLayout(new GridLayout(2, 1));
    panel.add(outputLabel);
    JPanel bottomPanel = new JPanel();
    bottomPanel.setBackground(inputPanelColor);
    bottomPanel.add(inputField);
    bottomPanel.add(new JLabel("   "));
    JButton enterButton = new JButton("Enter");
    enterButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent a) {
            hasEntered = true;
          }
        });
    bottomPanel.add(enterButton);
    panel.add(bottomPanel);
    if (!sequencingOn) {
      cPane.add(panel, BorderLayout.SOUTH);
    }

    // Drawing in the center.
    drawArea = new DrawTool();
    if (sequencingOn) {
      frame.addKeyListener(
          new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
              handleKeyTyped(e);
            }
          });
    }
    cPane.add(drawArea, BorderLayout.CENTER);

    drawArea.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            handleMouseClick(e);
          }

          public void mouseReleased(MouseEvent e) {
            handleMouseReleased(e);
          }
        });

    drawArea.addMouseMotionListener(
        new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
            handleMouseDragged(e);
          }
        });
  }
Пример #8
0
  // The constructor which sets up all the JFrame, Canvas, JPanels, and sets the buffer strategy.
  public Bomberman() {
    // makes the Canvas visible
    setVisible(true);

    // Creates window and sets it up
    window = new JFrame("BOMBERMAN");
    window.setBounds(0, 0, WIDTH, HEIGHT);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.addKeyListener(this);
    setBounds(0, 0, WIDTH, HEIGHT);
    window.getContentPane().setLayout(new CardLayout());

    // Creates the menu bar
    window.setJMenuBar(new Menu());

    // Creates an option panel that has all the options
    optionsPanel = new Options();
    window.getContentPane().add(optionsPanel, "OptionsPanel");

    // Creates a title panel which is the title screen
    titlePanel = new JPanel();
    titlePanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    titlePanel.setLayout(new GridLayout(1, 1));
    titlePanel.add(
        new JLabel(new ImageIcon(getClass().getClassLoader().getResource("titleScreen.png"))));
    window.getContentPane().add(titlePanel, "TitlePanel");

    // Creates and sets up the game panel where the game is played on
    gamePanel = new JPanel();
    gamePanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    gamePanel.setLayout(null);
    gamePanel.add(this);
    window.getContentPane().add(gamePanel, "GamePanel");

    // Creates a rules screen panel which is the rules screen
    rulesPanel = new JPanel();
    rulesPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    rulesPanel.setLayout(new GridLayout(1, 1));
    rulesPanel.add(new JLabel(new ImageIcon(getClass().getClassLoader().getResource("rules.png"))));
    window.getContentPane().add(rulesPanel, "RulesPanel");

    // Creates a controls screen panel which is the controls screen
    controlsPanel = new JPanel();
    controlsPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    controlsPanel.setLayout(new GridLayout(1, 1));
    controlsPanel.add(
        new JLabel(new ImageIcon(getClass().getClassLoader().getResource("controls.png"))));
    window.getContentPane().add(controlsPanel, "ControlsPanel");

    // Creates a controls screen panel which is the controls screen
    creditsPanel = new JPanel();
    creditsPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    creditsPanel.setLayout(new GridLayout(1, 1));
    creditsPanel.add(
        new JLabel(new ImageIcon(getClass().getClassLoader().getResource("credits.png"))));
    window.getContentPane().add(creditsPanel, "CreditsPanel");

    // Makes certain panels visible/invisible
    gamePanel.setVisible(false);
    optionsPanel.setVisible(false);
    rulesPanel.setVisible(false);
    controlsPanel.setVisible(false);
    creditsPanel.setVisible(false);
    titlePanel.setVisible(true);
    window.setVisible(true);

    // double buffering
    createBufferStrategy(2);
    bs = getBufferStrategy();
  }
  public WebcamCaptureAndFadePanel(String saveDir, String layout) {

    System.out.println("Using " + saveDir + " as directory for the images.");
    saveDirectory = saveDir;

    getImages();
    images_used = new ArrayList<Integer>();
    images_lastadded = new ArrayList<Integer>();
    images_nevershown = new ArrayList<Integer>();

    Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone();
    Enumeration enumeration = devices.elements();
    System.out.println("- Available cameras -");
    ArrayList<String> names = new ArrayList<String>();
    while (enumeration.hasMoreElements()) {
      CaptureDeviceInfo cdi = (CaptureDeviceInfo) enumeration.nextElement();
      String name = cdi.getName();
      if (name.startsWith("vfw:")) {
        names.add(name);
        System.out.println(name);
      }
    }

    // String str1 = "vfw:Logitech USB Video Camera:0";
    // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    if (names.size() == 0) {
      JOptionPane.showMessageDialog(
          null,
          "Ingen kamera funnet. " + "Du må koble til et kamera for å kjøre programmet.",
          "Feil",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } else if (names.size() > 1) {

      JOptionPane.showMessageDialog(
          null,
          "Fant mer enn 1 kamera. " + "Velger da:\n" + names.get(0),
          "Advarsel",
          JOptionPane.WARNING_MESSAGE);
    }

    String str2 = names.get(0);
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();

    try {
      player = Manager.createRealizedPlayer(ml);
      formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl");

      /*
      Format[] formats = formatControl.getSupportedFormats();
      for (int i=0; i<formats.length; i++)
      	System.out.println(formats[i].toString());
      */

      player.start();
    } catch (javax.media.NoPlayerException e) {
      JOptionPane.showMessageDialog(
          null,
          "Klarer ikke å starte" + " programmet pga. feil med kamera. Sjekk at det er koblet til.",
          "IOException",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    /*
     * Layout
     *
     * Add
     * - comp
     * - imagepanels
     */

    if (layout.equals("1024v2")) {
      layout1024v2();
    } else if (layout.equals("1280")) {
      layout1280();
    } else {
      layout1024();
    }

    // Capture Window
    if (captureWindow) {
      cw = new JFrame("Capture from webcam");
      cw.setAlwaysOnTop(true);
      cw.setSize(sizeCaptureWindow_x, sizeCaptureWindow_y);
      cw.addKeyListener(new captureWindowKeyListner());
      cw.setUndecorated(true);

      // Add webcam
      if ((comp = player.getVisualComponent()) != null) {
        cw.add(comp);
      }

      // Add panel to window and set location of window
      cw.setLocation(cwLocation_x, cwLocation_y);
    }

    // Text window
    cwText = new rotatedText("");

    /*
     * Timer for update
     */
    Timer thread = new Timer();
    thread.schedule(new frameUpdateTask(), 0, (1000 / fps));
  }