Java Code Examples for javax.swing.JFrame#addKeyListener()

The following examples show how to use javax.swing.JFrame#addKeyListener() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Lunar   File: Game.java    License: MIT License 6 votes vote down vote up
/**
 * Initialize the project.
 *
 * @param title    The string on the window's title bar.
 * @param width    Width of the window
 * @param height   Height of the window
 * @param tickRate Determines how fast the game loop is.
 */
public Game(String title, int width, int height, int tickRate) {
    this.width = width;
    this.height = height;

    stack = new ArrayList<>();

    this.maxTPS = tickRate;

    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setFocusable(true);

    frame.addKeyListener(new InputListener());
    frame.addMouseListener(new MouseInput());
}
 
Example 2
Source Project: Lunar   File: Game.java    License: MIT License 6 votes vote down vote up
/**
 * Initialize the game.
 *
 * @param title    The string on the window's title bar.
 * @param width    Width of the window
 * @param height   Height of the window
 * @param pref     The window preferences.
 * @param tickRate Determines how fast the game loop is.
 */
public Game(String title, int width, int height, FramePreferences pref, int tickRate) {
    this.width = width;
    this.height = height;

    stack = new ArrayList<>();

    maxTPS = tickRate;

    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(pref.getCloseOperation());
    frame.setLocationRelativeTo(pref.getRelativeLocation());
    frame.setResizable(pref.isResizable());
    frame.setFocusable(pref.isFocusable());

    frame.addKeyListener(new InputListener());
    frame.addMouseListener(new MouseInput());
}
 
Example 3
@CalledOnlyBy(AmidstThread.EDT)
private JFrame createFrame() {
	JFrame frame = new JFrame("Profile Selector");
	frame.setIconImages(metadata.getIcons());
	frame.getContentPane().setLayout(new MigLayout());
	frame.add(createTitleLabel(), "h 20!,w :400:, growx, pushx, wrap");
	frame.add(createScrollPanel(), getScrollPaneLayoutString());
	frame.pack();
	frame.addKeyListener(profileSelectPanel.createKeyListener());
	frame.addWindowListener(new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent e) {
			application.exitGracefully();
		}
	});
	frame.setLocation(200, 200);
	frame.setVisible(true);
	return frame;
}
 
Example 4
@CalledOnlyBy(AmidstThread.EDT)
private JFrame createFrame() {
	JFrame frame = new JFrame("Profile Selector");
	frame.setIconImages(metadata.getIcons());
	frame.getContentPane().setLayout(new MigLayout());
	frame.add(createTitleLabel(), "h 20!,w :400:, growx, pushx, wrap");
	frame.add(createScrollPanel(), getScrollPaneLayoutString());
	frame.pack();
	frame.addKeyListener(profileSelectPanel.createKeyListener());
	frame.addWindowListener(new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent e) {
			application.exitGracefully();
		}
	});
	frame.setLocation(200, 200);
	frame.setVisible(true);
	return frame;
}
 
Example 5
/**
 * Spawns the editor visualisation window.
 * @param startGoalPoints 
 */
private static void setupMainFrame(GridGraph gridGraph, String mazeName, StartGoalPoints startGoalPoints) {
    AlgoFunction algoFunction = AnyAnglePathfinding.setDefaultAlgoFunction();
    ArrayList<ArrayList<Point>> connectedSets = MazeAnalysis.findConnectedSetsFast(gridGraph);
    
    EditorUI editorUI = new EditorUI(gridGraph, algoFunction, connectedSets, mazeName, startGoalPoints);
    VisualiserMouseControls mouseControls =
            new VisualiserMouseControls(gridGraph, editorUI);
    VisualiserKeyboardControls keyboardControls =
            new VisualiserKeyboardControls(editorUI);
    
    JFrame mainFrame = new JFrame();
    mainFrame.setTitle(mazeName);
    mainFrame.add(editorUI);
    mainFrame.addWindowListener(new CloseOnExitWindowListener());
    mainFrame.getContentPane().addMouseListener(mouseControls);
    mainFrame.getContentPane().addMouseMotionListener(mouseControls);
    mainFrame.addKeyListener(keyboardControls);
    mainFrame.setResizable(false);
    mainFrame.pack();
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
}
 
Example 6
private static void createAndShowGUI() {
JFrame f = new JFrame("Smooth Moves");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(moveMaxX + imageW + 50, 300);
SmoothMoves component = new SmoothMoves();
f.add(component);
f.setVisible(true);
       f.addKeyListener(component);
   }
 
Example 7
private static void createAndShowGUI() {
    JFrame f = new JFrame("CopyAreaPerformance");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(600, 600);
    CopyAreaPerformance component = new CopyAreaPerformance();
    f.add(component);
    f.addKeyListener(component);
    f.setVisible(true);
}
 
Example 8
Source Project: Any-Angle-Pathfinding   File: Visualisation.java    License: The Unlicense 5 votes vote down vote up
/**
 * Spawns the visualisation window for the algorithm.
 */
protected static void setupMainFrame(DrawCanvas drawCanvas, ArrayList<GridObjects> gridObjectsList) {
    KeyToggler keyToggler = new KeyToggler(drawCanvas, gridObjectsList);
    
    JFrame mainFrame = new JFrame();
    mainFrame.add(drawCanvas);
    mainFrame.addKeyListener(keyToggler);
    mainFrame.addWindowListener(new CloseOnExitWindowListener());
    mainFrame.setResizable(false);
    mainFrame.pack();
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
}
 
Example 9
public ProcessingRasteriser(DisplayType type, final int displayWidth, final int displayHeight)
	{
		this.type = type;
		this.displayWidth = displayWidth;
		this.displayHeight = displayHeight;
		
		frame = new JFrame("Title here!");
		frame.setLayout(new BorderLayout());
		
		processingApplet = new Embedded(displayWidth, displayHeight);
	//	frame.add(processingApplet, BorderLayout.CENTER);
		processingApplet.init();
        
		graphics = (PGraphics3D)processingApplet.createGraphics(displayWidth, displayHeight, PGraphics3D.P3D);
        
/*		From docs:
		
		big = createGraphics(3000, 3000, P3D);
		big.beginDraw();
		big.background(128);
		big.line(20, 1800, 1800, 900);
		// etc..
		big.endDraw();
		// make sure the file is written to the sketch folder
		big.save("big.tif");
*/
		
		if (type == DisplayType.Window)
		{
			canvas = new Canvas();
			canvas.setPreferredSize(new Dimension(displayWidth, displayHeight));
			canvas.setMinimumSize(new Dimension(displayWidth, displayHeight));
			
			frame.add(canvas);
			
			frame.pack();
			frame.setVisible(true);
			
			keyHandler = new KeyHandler();
			frame.addKeyListener(keyHandler);
			processingApplet.addKeyListener(keyHandler);
			canvas.addKeyListener(keyHandler);
			
			frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			
			windowHandler = new WindowHandler();
			frame.addWindowListener(windowHandler);
		}
		else if (type == DisplayType.Offscreen)
		{
			// http://wiki.processing.org/w/Draw_to_off-screen_buffer
			
		//	buffer = createGraphics(500, 500, JAVA2D);
		}
		else
		{
			throw new RuntimeException("Unknown display type:"+type);
		}
	}
 
Example 10
Source Project: Spark   File: VCardEditor.java    License: Apache License 2.0 4 votes vote down vote up
/**
    * Displays a users profile.
    * 
    * @param jid
    *            the jid of the user.
    * @param vcard
    *            the users vcard.
    * @param parent
    *            the parent component, used for location handling.
    */
   public void displayProfile(final BareJid jid, VCard vcard, JComponent parent) {
VCardViewer viewer = new VCardViewer(jid);

final JFrame dlg = new JFrame(Res.getString("title.view.profile.for",
	jid));

avatarLabel = new JLabel();
avatarLabel.setHorizontalAlignment(JButton.RIGHT);
avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white,
	Color.lightGray));

// The user should only be able to close this dialog.
Object[] options = { Res.getString("button.view.profile"),
	Res.getString("close") };
final JOptionPane pane = new JOptionPane(viewer,
	JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,
	options, options[0]);

// mainPanel.add(pane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
// GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5,
// 5, 5), 0, 0));

dlg.setIconImage(SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16)
	.getImage());

dlg.pack();
dlg.setSize(350, 250);
dlg.setResizable(true);
dlg.setContentPane(pane);
dlg.setLocationRelativeTo(parent);

PropertyChangeListener changeListener = new PropertyChangeListener() {
    @Override
	public void propertyChange(PropertyChangeEvent e) {
	if (pane.getValue() instanceof Integer) {
	    pane.removePropertyChangeListener(this);
	    dlg.dispose();
	    return;
	}
	String value = (String) pane.getValue();
	if (Res.getString("close").equals(value)) {
	    pane.removePropertyChangeListener(this);
	    dlg.dispose();
	} else if (Res.getString("button.view.profile").equals(value)) {
	    pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
	    SparkManager.getVCardManager().viewFullProfile(jid, pane);
	}
    }
};

pane.addPropertyChangeListener(changeListener);

dlg.addKeyListener(new KeyAdapter() {
    @Override
	public void keyPressed(KeyEvent keyEvent) {
	if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) {
	    dlg.dispose();
	}
    }
});

dlg.setVisible(true);
dlg.toFront();
dlg.requestFocus();
   }