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

/**
 * initialize keystroke bindings//from  w w w .jav  a 2 s.  c o  m
 */
public final static void InitializeKeyStrokeBindings() {
    String selectAllAction = DefaultEditorKit.selectAllAction;
    String cutAction = DefaultEditorKit.cutAction;
    String copyAction = DefaultEditorKit.copyAction;
    String pasteAction = DefaultEditorKit.pasteAction;

    int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    JTextComponent.KeyBinding ctrlA = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_A, mask),
            selectAllAction);
    JTextComponent.KeyBinding ctrlX = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_X, mask),
            cutAction);
    JTextComponent.KeyBinding ctrlC = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask),
            copyAction);
    JTextComponent.KeyBinding ctrlV = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask),
            pasteAction);

    JTextComponent.KeyBinding[] extraBindings = new JTextComponent.KeyBinding[] { ctrlA, ctrlX, ctrlC, ctrlV };

    Keymap defaultKeyMap = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);

    JTextComponent dummy = new JTextField();
    JTextComponent.loadKeymap(defaultKeyMap, extraBindings, dummy.getActions());
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*from  w w w  .j  av  a  2s. c  o m*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();
            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*from w w  w  .  ja  v  a  2  s  . com*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();

            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 */// www . j  a  v a 2 s  . c o m
public static Cursor createImageCursor(Image img, Point hotspot) {
    Toolkit tk = Toolkit.getDefaultToolkit();

    // for now, just report the cursor restrictions, then blindly create
    int w = img.getWidth(null);
    int h = img.getHeight(null);
    Dimension d = tk.getBestCursorSize(w, h);
    //         int colors = tk.getMaximumCursorColors();
    //         Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h +
    //                   ", bestSize=" + d.width + "x" + d.height +
    //                   ", maxcolors=" + colors + "].");

    // if the passed-in image is smaller, pad it with transparent pixels and use it anyway.
    if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) {
        Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK);
        Graphics g = padder.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        // and reassign the image to the padded image
        img = padder;

        // and adjust the 'best' to cheat the hotspot checking code
        d.width = w;
        d.height = h;
    }

    // make sure the hotspot is valid
    if (hotspot == null) {
        hotspot = new Point(d.width / 2, d.height / 2);
    } else {
        hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x));
        hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y));
    }

    // and create the cursor
    return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor");
}

From source file:md.mclama.com.ModManager.java

/**
 * Launch the application./*from w w  w  .ja  v  a2  s .co  m*/
 */
public static void main(String[] args) {
    try {
        workDir = new File(ModManager.class.getProtectionDomain().getCodeSource().getLocation().toURI()
                .getPath().replace("McLauncher.jar", ""));
        System.out.println("Running McLauncher from " + workDir);
    } catch (URISyntaxException e1) {
        workDir = new File("");
        e1.printStackTrace();
    }
    //      try {
    //           // Get a file channel for the file
    //         boolean FileNotLocked=false;
    //
    //           
    //           try {
    //            FileWriter file2 = new FileWriter(System.getProperty("java.io.tmpdir") + "/McLauncher.lock");
    //            file2.write("McLauncher lock");
    //            file2.close();
    //         } catch (IOException e) {
    //            FileNotLocked=true;
    //         }
    //           
    //           File file = new File(System.getProperty("java.io.tmpdir") + "/McLauncher.lock");
    //
    //           FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    //           if(file.renameTo(file)) FileNotLocked=true;
    //           if(FileNotLocked){
    //              JOptionPane.showMessageDialog(null, "Already running McLauncher, Only one instance allowed at a time\n"
    //                  + "Is this wrong? Delete McLauncher.lock in your temp folder");
    //            System.out.println("Already running McLauncher, Only one instance allowed at a time");
    //            System.exit(0);
    //           }
    //           lock = channel.lock();
    //
    //           try {
    //               lock = channel.tryLock();
    //           } catch (OverlappingFileLockException e) {
    //           }
    //           channel.close();
    //       } catch (Exception e) {
    //       }

    //Not added because i do not want to lock people out from using McLauncher if somehow this fails.
    //      if(new File(System.getProperty("java.io.tmpdir") + "/McLauncher.lock").exists()){
    //         System.exit(0);//close this instance if McLauncher is already running.
    //      }
    //      try {
    //         FileWriter file = new FileWriter(System.getProperty("java.io.tmpdir") + "/McLauncher.lock");
    //         file.write("McLauncher one instance lock");
    //         file.close();
    //      } catch (IOException e) {
    //         System.out.println("Severe, failed to create temp lock file");
    //      }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                con = new Console();
                con.setVisible(false);
                Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
                int x = (int) ((dimension.getWidth() - con.getWidth()) / 2);
                int y = (int) ((dimension.getHeight() - con.getHeight()) / 2);
                con.setLocation(x, y);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
                ModManager frame = new ModManager();
                frame.setResizable(false);
                frame.setVisible(true);
                Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
                int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
                int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
                frame.setLocation(x, y);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:Main.java

/**
 * Centers a component on screen./*  w  w  w.  j  a va  2 s  . c om*/
 *
 * @param c The component that will be centered.
 */
public static void centerComponentOnScreen(Component c) {

    // Size of the screen.
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    double width = screenSize.getWidth();
    double height = screenSize.getHeight();

    // Size of the component.
    Dimension size = c.getSize();

    width -= size.getWidth();
    height -= size.getHeight();

    c.setLocation((int) width / 2, (int) height / 2);
}

From source file:Main.java

public static void centerWindowOnScreen(Window window) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    window.setLocation((int) (screenSize.getWidth() / 2 - window.getWidth() / 2),
            (int) (screenSize.getHeight() / 2 - window.getHeight() / 2));
}

From source file:Main.java

public static void setDialogWindowToCenter(JDialog dialog) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    dialog.setBounds((screenSize.width - dialog.getWidth()) / 2, (screenSize.height - dialog.getHeight()) / 2,
            dialog.getWidth(), dialog.getHeight());
}

From source file:Demo3D.java

/**
 * Main of the Demo program. Take the graphic environment of the
 * workstation.//from  ww  w.ja  va 2 s.c  om
 */
public static void main(String args[]) {
    JFrame jFrameDemo = new Demo3D();

    // To be sure to stop the application when the frame is closed.
    WindowListener winListener = new WindowAdapter() {
        public void windowClosing(WindowEvent event) {
            System.exit(0);
        }
    };

    jFrameDemo.addWindowListener(winListener);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    screenwidth = (int) screenSize.getWidth();
    screenheight = (int) screenSize.getHeight();
    jFrameDemo.setSize(screenwidth, screenheight);

    // Turn on the visibility of the frame.
    jFrameDemo.setVisible(true);

    fpsThread.start();
}

From source file:Main.java

/**
 * Set the text component in which only numerics (positive decimal integers
 * with character 0-9)./*from ww w  .  ja va2s.  com*/
 * 
 * @param c
 *            the specified text component
 */
public static void setTextComponetIntegerValid(final JTextComponent c) {
    c.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE)
                try {
                    Integer.valueOf(c.getText() + e.getKeyChar());
                } catch (NumberFormatException nfe) {
                    e.consume();
                    Toolkit.getDefaultToolkit().beep();
                }
        }
    });
}