Example usage for java.awt Toolkit getDefaultToolkit

List of usage examples for java.awt Toolkit getDefaultToolkit

Introduction

In this page you can find the example usage for java.awt Toolkit getDefaultToolkit.

Prototype

public static synchronized Toolkit getDefaultToolkit() 

Source Link

Document

Gets the default toolkit.

Usage

From source file:Main.java

ImagePanel() {
    MediaTracker mt = new MediaTracker(this);
    for (int i = 0; i < images.length; i++) {
        imgs[i] = Toolkit.getDefaultToolkit().getImage(images[i]);
        mt.addImage(imgs[i], i);//from  w  w  w  . ja  v  a 2s  .  c  o  m
    }
    try {
        mt.waitForAll();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Clipboard.java

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents./*from  ww  w  .  j a v  a  2  s  .  co m*/
*/
public void setClipboardContents(String aString) {
    StringSelection stringSelection = new StringSelection(aString);
    java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, this);
}

From source file:Win.java

public static void position(Window frame, Component ref, double xfrac, double yfrac) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = frame.getSize();
    Dimension refSize = (ref != null) ? ref.getSize() : screenSize;
    Point origin = (ref != null) ? ref.getLocationOnScreen() : new Point(0, 0);

    int x = origin.x + relativePoint(xfrac, refSize.width, size.width);
    int y = origin.y + relativePoint(yfrac, refSize.height, size.height);

    // make sure frame is entirely on screen
    x = Math.max(0, Math.min(screenSize.width - size.width, x));
    y = Math.max(0, Math.min(screenSize.height - size.height, y));

    frame.setLocation(x, y);/*  w ww .  ja  va 2s.  co  m*/
}

From source file:com.ariatemplates.seleniumjavarobot.LocalRobotizedBrowserFactory.java

private static void numLockStateWorkaround() {
    try {//  w w w  .  ja v  a 2 s. c om
        // Shift key is not kept pressed while using keyPress method
        // cf https://forums.oracle.com/thread/2232592
        // The workaround is to use the following line:
        Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
        SeleniumJavaRobot.log("Num lock state was successfully changed.");
    } catch (UnsupportedOperationException e) {
        SeleniumJavaRobot.log("Did not change num lock state: " + e);
    }
}

From source file:net.sf.nmedit.jtheme.JTCursor.java

private static Cursor createCursor(int id) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    URL res = getResource(id);//from   ww  w  . ja v a2s.c om

    if (res == null) {
        Log log = LogFactory.getLog(JTCursor.class);
        if (log.isWarnEnabled()) {
            log.warn("Could not find cursor: id=" + id + ", url=" + res);
        }
        return Cursor.getDefaultCursor();
    }

    Image img;
    try {
        img = ImageIO.read(res);
    } catch (IOException e) {
        Log log = LogFactory.getLog(JTCursor.class);
        if (log.isWarnEnabled()) {
            log.warn("Could not find cursor: id=" + id + ", url=" + res, e);
        }
        return Cursor.getDefaultCursor();
    }

    return tk.createCustomCursor(img, new Point(4, 16), names[id]);
}

From source file:Main.java

/**
 * Loads an image from within a jar./*from   w  ww. ja va  2  s  .  c  om*/
 * 
 * @param fileName the image path. 
 * @return an initialised image-icon or null, when no image could be found.
 */
public static Image loadIcon(String fileName) {
    URL url = ClassLoader.getSystemResource(fileName);

    if (url == null) {
        System.out.println("unable to locate [" + fileName + "].");
        return null;
    }
    return Toolkit.getDefaultToolkit().createImage(url);
}

From source file:Main.java

public Main() {
    label = new JLabel("Waiting...");
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);/*from w  w w .  ja  v a 2 s. c  o  m*/
    frame.setSize(200, 200);
    frame.setVisible(true);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        int count;

        @Override
        public void eventDispatched(AWTEvent event) {
            Object source = event.getSource();
            if (source instanceof Component) {
                Component comp = (Component) source;
                Window win = null;
                if (comp instanceof Window) {
                    win = (Window) comp;
                } else {
                    win = SwingUtilities.windowForComponent(comp);
                }
                if (win == frame) {
                    timer.restart();
                    label.setText("Interrupted..." + (++count));
                }
            }
        }
    }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK);

    timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    timer.start();
}

From source file:Main.java

/**
 * Centers the given component over another component.
 * <p/>// ww  w . j  ava2s.  c o  m
 * <p> The method performs the alignment by setting a newly computed location for the component. It does not alter
 * the component's size.
 *
 * @param comp      the component whose location is to be altered
 * @param alignComp the component used for the alignment of the first component, if <code>null</code> the component
 *                  is ceneterd within the screen area
 *
 * @throws IllegalArgumentException if the component is <code>null</code>
 */
public static void centerComponent(Component comp, Component alignComp) {

    if (comp == null) {
        throw new IllegalArgumentException("comp must not be null");
    }

    Dimension compSize = comp.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int x1, y1;

    if (alignComp != null && !new Rectangle(alignComp.getSize()).isEmpty()) {
        Point alignCompOffs = alignComp.getLocation();
        Dimension alignCompSize = alignComp.getSize();
        x1 = alignCompOffs.x + (alignCompSize.width - compSize.width) / 2;
        y1 = alignCompOffs.y + (alignCompSize.height - compSize.height) / 2;
    } else {
        x1 = (screenSize.width - compSize.width) / 2;
        y1 = (screenSize.height - compSize.height) / 2;
    }

    int x2 = x1 + compSize.width;
    int y2 = y1 + compSize.height;

    if (x2 >= screenSize.width) {
        x1 = screenSize.width - compSize.width - 1;
    }
    if (y2 >= screenSize.height) {
        y1 = screenSize.height - compSize.height - 1;
    }
    if (x1 < 0) {
        x1 = 0;
    }
    if (y1 < 0) {
        y1 = 0;
    }

    comp.setLocation(x1, y1);
}

From source file:AppPackage.humidity.java

public humidity() {
    try {/*from   ww w. j  av a  2  s .  co  m*/

        JFrame window = new JFrame();
        window.setSize(1000, 615);
        window.setBackground(Color.blue.darker());
        double value = 45;
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - window.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - window.getHeight()) / 2);
        window.setLocation(x, y);
        DefaultValueDataset dataset = new DefaultValueDataset(value);

        ThermometerPlot thermometerplot = new ThermometerPlot(dataset);

        thermometerplot.setSubrangePaint(0, Color.green.darker());
        thermometerplot.setSubrangePaint(1, Color.orange);
        thermometerplot.setSubrangePaint(2, Color.red);
        JFreeChart jfreechart = new JFreeChart("Humidity readings", JFreeChart.DEFAULT_TITLE_FONT,
                thermometerplot, true);
        thermometerplot.setInsets(new RectangleInsets(5D, 5D, 5D, 5D));
        thermometerplot.setPadding(new RectangleInsets(10D, 10D, 10D, 10D));
        thermometerplot.setThermometerStroke(new BasicStroke(2.0F));
        thermometerplot.setThermometerPaint(Color.BLUE);
        thermometerplot.setUnits(0);
        thermometerplot.setGap(3);

        window.add(new ChartPanel(jfreechart), BorderLayout.CENTER);
        window.setVisible(true);

    } catch (Exception e) {
        System.out.print("Chart exception:" + e);
    }
    initComponents();
}

From source file:AppPackage.Temperature.java

public Temperature() {
    try {/*w  ww .  ja  v  a 2 s .com*/

        JFrame window = new JFrame();
        window.setSize(1000, 615);
        window.setBackground(Color.blue.darker());
        double value = 55;
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - window.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - window.getHeight()) / 2);
        window.setLocation(x, y);
        DefaultValueDataset dataset = new DefaultValueDataset(value);

        ThermometerPlot thermometerplot = new ThermometerPlot(dataset);
        thermometerplot.setSubrangePaint(0, Color.green.darker());
        thermometerplot.setSubrangePaint(1, Color.orange);
        thermometerplot.setSubrangePaint(2, Color.red);
        JFreeChart jfreechart = new JFreeChart("Temperature readings", JFreeChart.DEFAULT_TITLE_FONT,
                thermometerplot, true);
        thermometerplot.setInsets(new RectangleInsets(5D, 5D, 5D, 5D));
        thermometerplot.setPadding(new RectangleInsets(10D, 10D, 10D, 10D));
        thermometerplot.setThermometerStroke(new BasicStroke(2.0F));
        thermometerplot.setThermometerPaint(Color.BLUE);

        thermometerplot.setGap(3);

        window.add(new ChartPanel(jfreechart), BorderLayout.CENTER);
        window.setVisible(true);

    } catch (Exception e) {
        System.out.print("Chart exception:" + e);
    }
    initComponents();
}