/** @param args the command line arguments */ public static void main(String[] args) { // creates a windows to show my game JFrame frame = new JFrame("My Game"); // creates an instance of my game FinalGame game = new FinalGame(); // sets the size of my game game.setPreferredSize(new Dimension(WIDTH, HEIGHT)); // adds the game to the window frame.add(game); // sets some options and size of the window automatically frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); // shows the window to the user frame.setVisible(true); // add the listeners frame.addKeyListener(game); // keyboard game.addMouseListener(game); // mouse game.addMouseMotionListener(game); // mouse // starts my game loop game.run(); }
public void addPanel() { addMouseListener(this); addMouseMotionListener(this); frame.add(this); frame.addKeyListener(this); addKeyListener(this); }
public void openInAFrame(boolean on) { if (on) { if (frame == null) { frame = new JFrame("GraphStream"); frame.setLayout(new BorderLayout()); frame.add(this, BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.addWindowListener(this); frame.addComponentListener(this); frame.addKeyListener(shortcuts); } else { frame.setVisible(true); } } else { if (frame != null) { frame.removeComponentListener(this); frame.removeWindowListener(this); frame.removeKeyListener(shortcuts); frame.remove(this); frame.setVisible(false); frame.dispose(); } } }
private void showImage() { // 5. initialize your frame to a new JFrame() frame = new JFrame(); // 6. set the frame to visible frame.setVisible(true); frame.setSize(500, 500); frame.add(getNextRandomImage()); frame.setSize(500, 500); frame.addKeyListener(this); // 7. rename to the name of your frame // 8. set the size of the frame // 9. add a key listener to the frame // 10. Use the speak method to either say "Simon says press this key" or "Press this key" // Hint: use the simonSays int and a random number Random random = new Random(); simonSays = random.nextInt(1); if (simonSays == 0) { speak("Simon says press this key."); } else if (simonSays == 1) { speak("Press this key."); } }
public UserTrackerApplication(JFrame frame) { this.frame = frame; frame.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent arg0) {} @Override public void keyReleased(KeyEvent arg0) {} @Override public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) { shouldRun = false; } else if (arg0.getKeyCode() == KeyEvent.VK_I) { if (viewer.isDrawRGB()) { viewer.setDrawRGB(false); } } else if (arg0.getKeyCode() == KeyEvent.VK_R) { if (!viewer.isDrawRGB()) { viewer.setDrawRGB(true); } } } }); }
/** * 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(); }
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(1000, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Collision Effects Test - press any key to start"); frame.setVisible(true); frame.setFocusable(true); frame.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent e) { start(); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} }); frame.add(new CollisionEffectsTest()); Ball ball = new Ball(200, 300, 3); ArrayList<Body> bodies = new ArrayList<Body>(); bodies.add(new Body(500, 300, 150, Color.blue)); level = new Level(ball, bodies, null, null, null, 5.0, 1.0); level.generateLevelData(); }
// 建立元件、將元件加入視窗、顯示視窗的方法 public void init() { myframe.addKeyListener(this); // 設定按鍵事件的傾聽者 myframe.getContentPane().add(whatkey); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myframe.setSize(240, 120); myframe.setVisible(true); }
public FrameRenderer(final JFrame target, boolean isFullscreen, RenderFitMode renderFitMode) { m_renderTarget = target; m_renderFitMode = renderFitMode; m_renderTarget.setSize(target.getSize()); m_canvasRenderWidth = target.getWidth(); m_canvasRenderHeight = target.getHeight(); target.setIgnoreRepaint(true); target.createBufferStrategy(2); m_bufferStrategy = target.getBufferStrategy(); BufferCapabilities cap = m_bufferStrategy.getCapabilities(); if (!cap.getBackBufferCapabilities().isAccelerated()) m_logger.warn("Backbuffer is not accelerated, may result in poorer render performance."); if (!cap.isPageFlipping()) m_logger.warn( "Page flipping is not supported by this device. This may result in increased image flicker or tearing."); if (!cap.isMultiBufferAvailable()) m_logger.warn( "Multiple buffering for page flipping is not supported by this device. This may result in poorer performance."); if (cap.isFullScreenRequired() && !isFullscreen) m_logger.warn( "Full screen required for hardware acceleration - not running in fullscreen may cause performance issues."); if (isFullscreen) { enterFullscreen(); target.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { GraphicsDevice device = target.getGraphicsConfiguration().getDevice(); Window fullscreen = device.getFullScreenWindow(); if (fullscreen != target) fullscreen = null; if (fullscreen == null) enterFullscreen(); else exitFullscreen(); } } }); } }
public RoadEnvironment() { mainFrame = new JFrame("Traffic Simulation App."); mainFrame.setVisible(true); mainFrame.setSize(800, 800); mainFrame.setResizable(false); mainFrame.add(dw = new DisplayWindow()); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.addKeyListener(this); start(); }
public static void setGui(GameGui newGui, JFrame frame, JPanel panel) { if (gui != null) { frame.removeMouseListener(gui); frame.removeKeyListener(gui); frame.removeMouseMotionListener(gui); } frame.addMouseListener(newGui); frame.addKeyListener(newGui); frame.addMouseMotionListener(newGui); RenderingUtil.gui = newGui; }
public static void main(String[] args) { frame.setSize(300, 400); Container pane = frame.getContentPane(); pane.add(bP); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addKeyListener(bP); bP.advance.start(); time = System.currentTimeMillis(); bP.init(); }
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 Title() throws Exception { time = new Timer(30, this); frame = new JFrame(); makeTitle(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.add(this); frame.addMouseListener(this); frame.addKeyListener(this); frame.setVisible(true); time.start(); // start the timer repaint(); }
// ---------- Draw Window Thread Run & Main Program Run Loop ---------- @Override public void run() { // Set Settings For JFrame frame.add(buildMenuBar()); frame.setVisible(true); height = (int) frame.getSize().getHeight(); width = (int) frame.getSize().getWidth(); render = new Render(this, width, height); // Set Up Buffer Strategy For Direct Pixel Access img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); setPixels(((DataBufferInt) img.getRaster().getDataBuffer()).getData()); // Mouse Setup dwm = new DrawWindowMouse(render); frame.addMouseListener(dwm); // Keyboard Setup dwk = new DrawWindowKeyboard(render); frame.addKeyListener(dwk); // Draw Loop do { // Starts Recording Render Time frameTime = System.currentTimeMillis(); if (System.currentTimeMillis() - startRecodTime >= 1000) { startRecodTime = System.currentTimeMillis(); lastFPS = frames; frames = 0; } // Renders Screen render(); // Calculates FPS (Try - Catch for catching start divide by zero) try { if (frameTime < maxWaitTime && fpsCap) Thread.sleep(maxWaitTime - frameTime); // lastFPS = 1000 / (System.currentTimeMillis() - startRecodTime); } catch (Exception e) { if (TreeGen.isDebug()) e.printStackTrace(); } frames++; } while (TreeGen.running); if (!TreeGen.running) System.exit(0); }
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 main(String[] args) { // cria o painel e adiciona um ouvinte GL GLJPanel panel = new GLJPanel(); TransfGeom3D pp = new TransfGeom3D(panel); panel.addGLEventListener(pp); // cria uma janela e adiciona o painel JFrame frame = new JFrame("Aplicação JOGL Simples"); frame.addKeyListener(pp); frame.add(panel); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); }
/** * Sets up the event handler mechanics, including invoking the initGUIHandlers method, which would * be provided by the game developer using this framework, and would presumably be different for * each game. */ private void initHandlers() { // SETUP THE LOW-LEVEL HANDLER WHO WILL // RELAY EVERYTHING MiniGameEventRelayer mger = new MiniGameEventRelayer(this); canvas.addMouseListener(mger); canvas.addMouseMotionListener(mger); window.setFocusable(true); window.addKeyListener(mger); canvas.addKeyListener(mger); // AND NOW LET THE GAME DEVELOPER PROVIDE // CUSTOM HANDLERS initGUIHandlers(); }
public static void main(String[] args) { JFrame window = new JFrame(); SpaceDodger game = new SpaceDodger(); window.add(game); window.addKeyListener(game); window.addMouseListener(game); window.addMouseMotionListener(game); window.pack(); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); Timer t = new Timer(100, game); t.start(); }
public Game() { Timer timer = new Timer(20, this); JFrame frame = new JFrame("Mario Breakout"); renderer = new Renderer(this); frame.setSize(width + 16, height + 39); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(renderer); frame.setLocationRelativeTo(null); frame.addKeyListener(this); start(); timer.start(); }
/** @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here JFrame window = new JFrame("FloatingIce"); FloatingIce game = new FloatingIce(); window.add(game); window.addKeyListener(game); window.pack(); window.setVisible(true); window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setBackground(Color.gray); Timer t = new Timer(100, game); t.start(); }
public Game() { window = new JFrame("Pong Game"); player1 = new Player(30, 200, 10, 60); player2 = new Player(440, 200, 10, 60); input = new GameInput(this); border = new Border(); ball = new Ball(30, 90, 15, 15, this); scene = new Scene(this); loop = new GameLoop(scene); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setPreferredSize(new Dimension(500, 500)); window.addKeyListener(input); window.add(scene); window.pack(); window.setVisible(true); }
/** Constructor. Take a KeyListener that wants to know about keys pressed in the frame. */ public GameFrame(KeyListener kl) { super(); // Set up the frame theFrame = new JFrame("BUNKER 1916"); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the keylistener to the frame theFrame.addKeyListener(kl); this.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); // Add ourselves to the screen. theFrame.getContentPane().add(this); this.setDoubleBuffered(true); // Set frame attributes theFrame.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); theFrame.setResizable(false); theFrame.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); } } }); }
public static void main(String[] args) { Snake snake = new Snake(); Food food = new Food(); Ground ground = new Ground(); mainPanel mainPanel = new mainPanel(); controller controller = new controller(snake, food, ground, mainPanel); JFrame jf = new JFrame(); jf.setSize(650, 650); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.add(mainPanel, BorderLayout.CENTER); mainPanel.addKeyListener(controller); jf.addKeyListener(controller); snake.addSnakeListener(controller); jf.setVisible(true); controller.newGame(); }
public static void main(String[] args) { final BoidKDTree<Vec2d> tree = new BoidKDTree<Vec2d>(); Random rnd = new Random(System.currentTimeMillis()); for (int i = 0; i < 10; i++) { final double x = rnd.nextDouble() * MODEL_WIDTH; final double y = rnd.nextDouble() * MODEL_HEIGHT; tree.add(x, y, new Vec2d(x, y)); } final MyPanel panel = new MyPanel(tree); panel.setPreferredSize(new Dimension((int) MODEL_WIDTH * 2, (int) MODEL_HEIGHT * 2)); panel.addMouseListener( new MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { final Vec2d p = panel.viewToModel(e.getX(), e.getY()); panel.mark(p.x, p.y, 25); }; }); final JFrame frame = new JFrame("KDTreeTest"); frame.addKeyListener( new KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent e) {}; }); frame.getContentPane().setLayout(new GridBagLayout()); final GridBagConstraints cnstrs = new GridBagConstraints(); cnstrs.fill = GridBagConstraints.BOTH; cnstrs.gridx = GridBagConstraints.REMAINDER; cnstrs.gridy = GridBagConstraints.REMAINDER; cnstrs.weightx = 1.0; cnstrs.weighty = 1.0; frame.getContentPane().add(panel, cnstrs); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }
// 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 anti-aliasing 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); }
public static void main(String args[]) { System.out.println("----- Client Start -----"); Console in = System.console(); // get a name String username = in.readLine("Enter your name: "); // get server hostname String hostname = in.readLine("Server hostname: "); // c or j String mode = in.readLine("[C]reate or [J]oin?: "); // get room name String roomname = in.readLine("Name of game-room: "); // set up the demopanel and its enclosing frame DemoPanel ui = new DemoPanel(); JFrame frame = new JFrame("ateam-tanks"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setLayout(new BorderLayout()); frame.add(ui, BorderLayout.CENTER); frame.addKeyListener(ui); frame.setVisible(true); // init and start game client GameClient c1 = new GameClient(username, 8887); // create RealWindow RealWindow win = new RealWindow(c1, ui); // start server communications try { sleep(1000); System.out.println("Connecting to server.."); c1.push(new event.client.JoinServerEvent(hostname)); sleep(1000); if (mode.equals("C") || mode.equals("c")) { c1.push(new event.client.FwdUserEvent(new event.user.CreateRoomEvent(roomname, mkList()))); in.readLine("Press Enter to start the game"); c1.push(new event.client.FwdUserEvent(new event.user.StartGameEvent())); } else if (mode.equals("J") || mode.equals("j")) { c1.push(new event.client.FwdUserEvent(new event.user.JoinRoomEvent(roomname))); } } catch (InterruptedException e) { } }
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(); }
/** * Begins listening to key commands and sending robot data. * * @throws IOException */ public void run() throws IOException { try { tcpClient = new TCPClient(robotID); tcpClient.connect(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Couldn't connect to the relay."); return; } JFrame frame = new JFrame("control"); frame.setBounds(500, 500, 200, 50); frame.setAlwaysOnTop(true); JLabel label = new JLabel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label.setText("Select and Pess a Key"); frame.add(label); frame.addKeyListener(this); frame.setVisible(true); }