use of java.awt.event.ComponentEvent in project Universal-G-Code-Sender by winder.
the class ExperimentalWindow method main.
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// Fix look and feel to use CMD+C/X/V/A instead of CTRL
if (SystemUtils.IS_OS_MAC) {
Collection<InputMap> ims = new ArrayList<>();
ims.add((InputMap) UIManager.get("TextField.focusInputMap"));
ims.add((InputMap) UIManager.get("TextArea.focusInputMap"));
ims.add((InputMap) UIManager.get("EditorPane.focusInputMap"));
ims.add((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
ims.add((InputMap) UIManager.get("PasswordField.focusInputMap"));
ims.add((InputMap) UIManager.get("TextPane.focusInputMap"));
int c = KeyEvent.VK_C;
int v = KeyEvent.VK_V;
int x = KeyEvent.VK_X;
int a = KeyEvent.VK_A;
int meta = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
for (InputMap im : ims) {
im.put(KeyStroke.getKeyStroke(c, meta), DefaultEditorKit.copyAction);
im.put(KeyStroke.getKeyStroke(v, meta), DefaultEditorKit.pasteAction);
im.put(KeyStroke.getKeyStroke(x, meta), DefaultEditorKit.cutAction);
im.put(KeyStroke.getKeyStroke(a, meta), DefaultEditorKit.selectAllAction);
}
}
/* Create the form */
final ExperimentalWindow mw = new ExperimentalWindow();
/* Apply the settings to the ExperimentalWindow bofore showing it */
mw.setSize(mw.backend.getSettings().getMainWindowSettings().width, mw.backend.getSettings().getMainWindowSettings().height);
mw.setLocation(mw.backend.getSettings().getMainWindowSettings().xLocation, mw.backend.getSettings().getMainWindowSettings().yLocation);
mw.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent ce) {
mw.backend.getSettings().getMainWindowSettings().height = ce.getComponent().getSize().height;
mw.backend.getSettings().getMainWindowSettings().width = ce.getComponent().getSize().width;
}
@Override
public void componentMoved(ComponentEvent ce) {
mw.backend.getSettings().getMainWindowSettings().xLocation = ce.getComponent().getLocation().x;
mw.backend.getSettings().getMainWindowSettings().yLocation = ce.getComponent().getLocation().y;
}
@Override
public void componentShown(ComponentEvent ce) {
}
@Override
public void componentHidden(ComponentEvent ce) {
}
});
/* Display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
mw.setVisible(true);
}
});
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
mw.connectionPanel.saveSettings();
mw.commandPanel.saveSettings();
if (mw.pendantUI != null) {
mw.pendantUI.stop();
}
}
});
// Check command line for a file to open.
boolean open = false;
for (String arg : args) {
if (open) {
try {
mw.backend.setGcodeFile(new File(arg));
open = false;
} catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
System.exit(1);
}
}
if (arg.equals("--open") || arg.equals("-o")) {
open = true;
}
}
}
use of java.awt.event.ComponentEvent in project javacv by bytedeco.
the class CanvasFrame method tile.
// This should not be called from the event dispatch thread (EDT),
// but if it is, it should not totally crash... In the worst case,
// it will simply timeout waiting for the moved events.
public static void tile(final CanvasFrame[] frames) {
class MovedListener extends ComponentAdapter {
boolean moved = false;
@Override
public void componentMoved(ComponentEvent e) {
moved = true;
Component c = e.getComponent();
synchronized (c) {
c.notify();
}
}
}
final MovedListener movedListener = new MovedListener();
// layout the canvas frames for the cameras in tiles
int canvasCols = (int) Math.round(Math.sqrt(frames.length));
if (canvasCols * canvasCols < frames.length) {
// if we don't get a square, favor horizontal layouts
// since screens are usually wider than cameras...
// and we also have title bars, tasks bar, menus, etc that
// takes up vertical space
canvasCols++;
}
int canvasX = 0, canvasY = 0;
int canvasMaxY = 0;
for (int i = 0; i < frames.length; i++) {
final int n = i;
final int x = canvasX;
final int y = canvasY;
try {
movedListener.moved = false;
EventQueue.invokeLater(new Runnable() {
public void run() {
frames[n].addComponentListener(movedListener);
frames[n].setLocation(x, y);
}
});
int count = 0;
while (!movedListener.moved && count < 5) {
// managers like Windows do not always send us the event...
synchronized (frames[n]) {
frames[n].wait(100);
}
count++;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
frames[n].removeComponentListener(movedListener);
}
});
} catch (java.lang.Exception ex) {
}
canvasX = frames[i].getX() + frames[i].getWidth();
canvasMaxY = Math.max(canvasMaxY, frames[i].getY() + frames[i].getHeight());
if ((i + 1) % canvasCols == 0) {
canvasX = 0;
canvasY = canvasMaxY;
}
}
}
use of java.awt.event.ComponentEvent in project OpenTripPlanner by opentripplanner.
the class ShowGraph method setup.
/*
* Setup Processing applet
*/
public void setup() {
size(getSize().width, getSize().height, P2D);
/* Build spatial index of vertices and edges */
buildSpatialIndex();
/* Set model bounds to encompass all vertices in the index, and then some */
modelBounds = (Envelope) (vertexIndex.getRoot().getBounds());
modelBounds.expandBy(0.02);
matchAspect();
/* save this zoom level to allow returning to default later */
modelOuterBounds = new Envelope(modelBounds);
/* find and set up the appropriate font */
String[] fonts = PFont.list();
String[] preferredFonts = { "Mono", "Courier" };
PFont font = null;
for (String preferredFontName : preferredFonts) {
for (String fontName : fonts) {
if (fontName.contains(preferredFontName)) {
font = createFont(fontName, 16);
break;
}
}
if (font != null) {
break;
}
}
textFont(font);
textMode(SCREEN);
addMouseWheelListener(this);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
Point p = e.getPoint();
mouseModelX = toModelX(p.x);
mouseModelY = toModelY(p.y);
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
matchAspect();
drawLevel = DRAW_PARTIAL;
}
});
frameRate(FRAME_RATE);
}
use of java.awt.event.ComponentEvent in project MindsEye by SimiaCryptus.
the class TestUtil method monitorImage.
/**
* Monitor ui.
*
* @param input the input
* @param exitOnClose the exit on close
* @param period the period
* @param normalize the normalize
*/
public static void monitorImage(final Tensor input, final boolean exitOnClose, final int period, final boolean normalize) {
JLabel label = new JLabel(new ImageIcon(input.toImage()));
WeakReference<JLabel> labelWeakReference = new WeakReference<>(label);
ScheduledFuture<?> updater = scheduledThreadPool.scheduleAtFixedRate(() -> {
try {
JLabel jLabel = labelWeakReference.get();
if (null != jLabel) {
BufferedImage image = (normalize ? normalizeBands(input) : input).toImage();
int width = jLabel.getWidth();
if (width > 0)
TestUtil.resize(image, width, jLabel.getHeight());
jLabel.setIcon(new ImageIcon(image));
}
} catch (Throwable e) {
log.warn("Error updating image", e);
}
}, 0, period, TimeUnit.SECONDS);
new Thread(() -> {
final JDialog dialog;
Window window = JOptionPane.getRootFrame();
String title = "Image: " + Arrays.toString(input.getDimensions());
if (window instanceof Frame) {
dialog = new JDialog((Frame) window, title, true);
} else {
dialog = new JDialog((Dialog) window, title, true);
}
dialog.setResizable(false);
dialog.setSize(input.getDimensions()[0], input.getDimensions()[1]);
JMenuBar menu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveAction = new JMenuItem("Save");
fileMenu.add(saveAction);
saveAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(final File f) {
return f.getName().toUpperCase().endsWith(".PNG");
}
@Override
public String getDescription() {
return "*.png";
}
});
int result = fileChooser.showSaveDialog(dialog);
if (JFileChooser.APPROVE_OPTION == result) {
try {
File selectedFile = fileChooser.getSelectedFile();
if (!selectedFile.getName().toUpperCase().endsWith(".PNG"))
selectedFile = new File(selectedFile.getParent(), selectedFile.getName() + ".png");
BufferedImage image = (normalize ? normalizeBands(input) : input).toImage();
if (!ImageIO.write(image, "PNG", selectedFile))
throw new IllegalArgumentException();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
}
});
menu.add(fileMenu);
dialog.setJMenuBar(menu);
Container contentPane = dialog.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(label, BorderLayout.CENTER);
// contentPane.add(dialog, BorderLayout.CENTER);
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
dialog.setUndecorated(true);
SwingUtilities.getRootPane(dialog).setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
}
}
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(final ComponentEvent e) {
// dialog.pack();
super.componentResized(e);
BufferedImage image = input.toImage();
int width = e.getComponent().getWidth();
if (width > 0)
TestUtil.resize(image, width, e.getComponent().getHeight());
label.setIcon(new ImageIcon(image));
dialog.pack();
}
});
dialog.addWindowListener(new WindowAdapter() {
private boolean gotFocus = false;
public void windowClosed(WindowEvent e) {
dialog.getContentPane().removeAll();
updater.cancel(false);
if (exitOnClose)
System.exit(0);
}
public void windowGainedFocus(WindowEvent we) {
// Once window gets focus, set initial focus
if (!gotFocus) {
gotFocus = true;
}
}
});
dialog.show();
dialog.dispose();
}).start();
}
use of java.awt.event.ComponentEvent in project Botnak by Gocnak.
the class BaseToolTipUI method installListeners.
protected void installListeners(JComponent c) {
super.installListeners(c);
// We must set the popup window to opaque because it is cached and reused within the PopupFactory
popupWindowListener = new ComponentAdapter() {
public void componentHidden(ComponentEvent e) {
Window window = (Window) e.getComponent();
DecorationHelper.setTranslucentWindow(window, false);
window.removeComponentListener(popupWindowListener);
}
};
}
Aggregations