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:BoundedTextField.java

public void insertFailed(BoundedPlainDocument doc, int offset, String str, AttributeSet a) {
    // By default, just beep
    Toolkit.getDefaultToolkit().beep();
}

From source file:UndoableTextArea.java

public void keyPressed(KeyEvent e) {
    if ((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown())) {
        try {//from   www  .ja  va  2 s.  c  om
            m_undoManager.undo();
        } catch (CannotUndoException cue) {
            Toolkit.getDefaultToolkit().beep();
        }
    }

    if ((e.getKeyCode() == KeyEvent.VK_Y) && (e.isControlDown())) {
        try {
            m_undoManager.redo();
        } catch (CannotRedoException cue) {
            Toolkit.getDefaultToolkit().beep();
        }
    }
}

From source file:Thumbnail.java

/**
* Reads an image in a file and creates a thumbnail in another file.
* largestDimension is the largest dimension of the thumbnail, the other dimension is scaled accordingly.
* Utilises weighted stepping method to gradually reduce the image size for better results,
* i.e. larger steps to start with then smaller steps to finish with.
* Note: always writes a JPEG because GIF is protected or something - so always make your outFilename end in 'jpg'.
* PNG's with transparency are given white backgrounds
*//* www.  j  a v a2s .  c o  m*/
public String createThumbnail(String inFilename, String outFilename, int largestDimension) {
    try {
        double scale;
        int sizeDifference, originalImageLargestDim;
        if (!inFilename.endsWith(".jpg") && !inFilename.endsWith(".jpeg") && !inFilename.endsWith(".gif")
                && !inFilename.endsWith(".png")) {
            return "Error: Unsupported image type, please only either JPG, GIF or PNG";
        } else {
            Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
            if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
                return "Error loading file: \"" + inFilename + "\"";
            } else {
                //find biggest dimension       
                if (inImage.getWidth(null) > inImage.getHeight(null)) {
                    scale = (double) largestDimension / (double) inImage.getWidth(null);
                    sizeDifference = inImage.getWidth(null) - largestDimension;
                    originalImageLargestDim = inImage.getWidth(null);
                } else {
                    scale = (double) largestDimension / (double) inImage.getHeight(null);
                    sizeDifference = inImage.getHeight(null) - largestDimension;
                    originalImageLargestDim = inImage.getHeight(null);
                }
                //create an image buffer to draw to
                BufferedImage outImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); //arbitrary init so code compiles
                Graphics2D g2d;
                AffineTransform tx;
                if (scale < 1.0d) //only scale if desired size is smaller than original
                {
                    int numSteps = sizeDifference / 100;
                    int stepSize = sizeDifference / numSteps;
                    int stepWeight = stepSize / 2;
                    int heavierStepSize = stepSize + stepWeight;
                    int lighterStepSize = stepSize - stepWeight;
                    int currentStepSize, centerStep;
                    double scaledW = inImage.getWidth(null);
                    double scaledH = inImage.getHeight(null);
                    if (numSteps % 2 == 1) //if there's an odd number of steps
                        centerStep = (int) Math.ceil((double) numSteps / 2d); //find the center step
                    else
                        centerStep = -1; //set it to -1 so it's ignored later
                    Integer intermediateSize = originalImageLargestDim,
                            previousIntermediateSize = originalImageLargestDim;
                    Integer calculatedDim;
                    for (Integer i = 0; i < numSteps; i++) {
                        if (i + 1 != centerStep) //if this isn't the center step
                        {
                            if (i == numSteps - 1) //if this is the last step
                            {
                                //fix the stepsize to account for decimal place errors previously
                                currentStepSize = previousIntermediateSize - largestDimension;
                            } else {
                                if (numSteps - i > numSteps / 2) //if we're in the first half of the reductions
                                    currentStepSize = heavierStepSize;
                                else
                                    currentStepSize = lighterStepSize;
                            }
                        } else //center step, use natural step size
                        {
                            currentStepSize = stepSize;
                        }
                        intermediateSize = previousIntermediateSize - currentStepSize;
                        scale = (double) intermediateSize / (double) previousIntermediateSize;
                        scaledW = (int) scaledW * scale;
                        scaledH = (int) scaledH * scale;
                        outImage = new BufferedImage((int) scaledW, (int) scaledH, BufferedImage.TYPE_INT_RGB);
                        g2d = outImage.createGraphics();
                        g2d.setBackground(Color.WHITE);
                        g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
                        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                        tx = new AffineTransform();
                        tx.scale(scale, scale);
                        g2d.drawImage(inImage, tx, null);
                        g2d.dispose();
                        inImage = new ImageIcon(outImage).getImage();
                        previousIntermediateSize = intermediateSize;
                    }
                } else {
                    //just copy the original
                    outImage = new BufferedImage(inImage.getWidth(null), inImage.getHeight(null),
                            BufferedImage.TYPE_INT_RGB);
                    g2d = outImage.createGraphics();
                    g2d.setBackground(Color.WHITE);
                    g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
                    tx = new AffineTransform();
                    tx.setToIdentity(); //use identity matrix so image is copied exactly
                    g2d.drawImage(inImage, tx, null);
                    g2d.dispose();
                }
                //JPEG-encode the image and write to file.
                OutputStream os = new FileOutputStream(outFilename);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
                encoder.encode(outImage);
                os.close();
            }
        }
    } catch (Exception ex) {
        String errorMsg = "";
        errorMsg += "<br>Exception: " + ex.toString();
        errorMsg += "<br>Cause = " + ex.getCause();
        errorMsg += "<br>Stack Trace = ";
        StackTraceElement stackTrace[] = ex.getStackTrace();
        for (int traceLine = 0; traceLine < stackTrace.length; traceLine++) {
            errorMsg += "<br>" + stackTrace[traceLine];
        }
        return errorMsg;
    }
    return ""; //success
}

From source file:com.qspin.qtaste.testapi.impl.generic.UtilityImpl.java

public void createScreenshot(String fileName) throws QTasteException {
    try {//from  ww w .ja va2  s.  c om
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    } catch (Exception e) {
        throw new QTasteException("Error in createScreenshot: " + e.getMessage());
    }
}

From source file:vic.collaborativeClouds.forms.Login.java

/**
 * Creates new form Login//ww w.ja  va  2  s  . co m
 */
public Login() {
    initComponents();
    //this.setExtendedState(Login.MAXIMIZED_BOTH);
    this.setAlwaysOnTop(true);

    setLayout(new BorderLayout());
    ImageLocator mImage = new ImageLocator();
    //System.err.println(mImage.login_dash);
    JLabel background = new JLabel(new ImageIcon(mImage.login_dash));
    this.add(background);
    background.setLayout(new FlowLayout());

    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());
    this.setSize(xSize, ySize);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
    heading.setLocation((xSize / 2) - 100, (ySize / 4) - 100);
    username.setLocation((xSize / 2) - 150, (ySize / 4));
    password.setLocation((xSize / 2) - 150, (ySize / 4) + 50);
    login.setLocation((xSize / 2) - 150, (ySize / 4) + 100);
    cancel.setLocation((xSize / 2) + 120, (ySize / 4) + 100);
    username.setOpaque(false);

}

From source file:ImageOps.java

/** This constructor loads the image we will manipulate */
public ImageOps() {
    image = Toolkit.getDefaultToolkit().getImage("a.jpg");
}

From source file:de.bley.word.menu.GuiMenu.java

/**
 * Kmponenten der Oberflaeche werden Initialisiert.
 *//*from   w  w w .  j  a v  a  2  s .  co  m*/
public GuiMenu() {
    instance = this;
    setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width / 4,
            Toolkit.getDefaultToolkit().getScreenSize().height / 2);
    setLocationRelativeTo(null);
    menuPanel = new MenuBarPanel();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    panel = new JPanel(new BorderLayout());
    panel.setBackground(Color.LIGHT_GRAY);
    textfield = new JTextArea();
    panel.add(textfield);
    JScrollPane scr = new JScrollPane(textfield, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.add(scr);
    button = new JButton();
    button.setText("Save");
    addListener();

}

From source file:com.github.srec.rec.DefaultScreenShot.java

@Override
public String captureDesktop(String subdir, Robot robot) {
    log.info("Capturing desktop screenshot");
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    return capture(subdir, screenRect, robot);
}

From source file:ja.lingo.application.gui.actions.PasteAndTranslateAction.java

public void actionPerformed(ActionEvent e) {
    try {/*w ww .  java  2 s.  c  o  m*/
        String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                .getData(DataFlavor.stringFlavor);
        data = data.trim();
        model.navigateAndTranslate(data);
        model.main_showAtTop();
    } catch (UnsupportedFlavorException e1) {
        LOG.error("Could not paste from system clipboard", e1);
    } catch (IOException e1) {
        LOG.error("Could not paste from system clipboard", e1);
    }
}

From source file:de.cebitec.readXplorer.util.GeneralUtils.java

/**
 * @param parent the parent component/*  w  ww  .ja va2  s .c o  m*/
 * @return Any text found in the clipboard. If none is found, an empty
 * String is returned.
 */
public static String getClipboardContents(Component parent) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String result = "";
    Transferable contents = clipboard.getContents(null);
    final boolean hasTransferableText = (contents != null)
            && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if (hasTransferableText) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException ex) {
            JOptionPane.showMessageDialog(parent, "Unsupported DataFlavor for clipboard copying.",
                    "Paste Error", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(parent,
                    "IOException occured during recovering of text from clipboard.", "Paste Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
    return result;
}