Example usage for java.awt Toolkit getScreenResolution

List of usage examples for java.awt Toolkit getScreenResolution

Introduction

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

Prototype

public abstract int getScreenResolution() throws HeadlessException;

Source Link

Document

Returns the screen resolution in dots-per-inch.

Usage

From source file:Main.java

public static void main(String[] args) {
    Toolkit tk = Toolkit.getDefaultToolkit();

    System.out.println("Screen resolution = " + tk.getScreenResolution());

}

From source file:ireport_5_6_0.view.JasperDesignViewer.java

/** This method is called from within the constructor to
 * initialize the form./*w  ww.  ja v a2s. com*/
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
private void initComponents() {//GEN-BEGIN:initComponents
    pnlMain = new javax.swing.JPanel();

    setTitle("JasperDesignViewer");
    setIconImage(
            new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                    .getImage());
    addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm();
        }
    });

    pnlMain.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

    pack();

    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    java.awt.Dimension screenSize = toolkit.getScreenSize();
    int screenResolution = toolkit.getScreenResolution();
    float zoom = ((float) screenResolution) / JRViewer.REPORT_RESOLUTION;

    int height = (int) (550 * zoom);
    if (height > screenSize.getHeight()) {
        height = (int) screenSize.getHeight();
    }
    int width = (int) (750 * zoom);
    if (width > screenSize.getWidth()) {
        width = (int) screenSize.getWidth();
    }

    java.awt.Dimension dimension = new java.awt.Dimension(width, height);
    setSize(dimension);
    setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
}

From source file:net.sf.jasperreports.view.JasperDesignViewer.java

/** This method is called from within the constructor to
 * initialize the form.//w  ww  . j a va 2  s  .co m
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
private void initComponents() {//GEN-BEGIN:initComponents
    pnlMain = new javax.swing.JPanel();

    setTitle("JasperDesignViewer");
    setIconImage(
            new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                    .getImage());
    addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm();
        }
    });

    pnlMain.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

    pack();

    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    java.awt.Dimension screenSize = toolkit.getScreenSize();
    int screenResolution = toolkit.getScreenResolution();
    float zoom = ((float) screenResolution) / net.sf.jasperreports.swing.JRViewerPanel.REPORT_RESOLUTION;

    int height = (int) (550 * zoom);
    if (height > screenSize.getHeight()) {
        height = (int) screenSize.getHeight();
    }
    int width = (int) (750 * zoom);
    if (width > screenSize.getWidth()) {
        width = (int) screenSize.getWidth();
    }

    java.awt.Dimension dimension = new java.awt.Dimension(width, height);
    setSize(dimension);
    setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
}

From source file:com.jswitch.reporte.controlador.JasperViewer2.java

/** This method is called from within the constructor to
 * initialize the form./*from   w w  w .j  a v  a2s  . c  om*/
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
private void initComponents() {//GEN-BEGIN:initComponents
    pnlMain = new javax.swing.JPanel();

    setTitle("JasperViewer");
    setIconImage(
            new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                    .getImage());
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm();
        }
    });

    pnlMain.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

    pack();

    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    java.awt.Dimension screenSize = toolkit.getScreenSize();
    int screenResolution = toolkit.getScreenResolution();
    float zoom = ((float) screenResolution) / JRViewer.REPORT_RESOLUTION;

    int height = (int) (550 * zoom);
    if (height > screenSize.getHeight()) {
        height = (int) screenSize.getHeight();
    }
    int width = (int) (750 * zoom);
    if (width > screenSize.getWidth()) {
        width = (int) screenSize.getWidth();
    }

    java.awt.Dimension dimension = new java.awt.Dimension(width, height);
    setSize(dimension);
    setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
}

From source file:ireport_5_6_0.view.JasperViewer.java

/** This method is called from within the constructor to
 * initialize the form./*from  www.  j  av a2 s.c o m*/
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
private void initComponents() {//GEN-BEGIN:initComponents
    pnlMain = new javax.swing.JPanel();

    setTitle("JasperViewer");
    setIconImage(
            new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                    .getImage());
    addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm();
        }
    });

    pnlMain.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

    pack();

    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    java.awt.Dimension screenSize = toolkit.getScreenSize();
    int screenResolution = toolkit.getScreenResolution();
    float zoom = ((float) screenResolution) / JRViewer.REPORT_RESOLUTION;

    int height = (int) (550 * zoom);
    if (height > screenSize.getHeight()) {
        height = (int) screenSize.getHeight();
    }
    int width = (int) (750 * zoom);
    if (width > screenSize.getWidth()) {
        width = (int) screenSize.getWidth();
    }

    java.awt.Dimension dimension = new java.awt.Dimension(width, height);
    setSize(dimension);
    setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
}

From source file:net.sf.jasperreports.view.JasperViewer.java

/** This method is called from within the constructor to
 * initialize the form.//w  w  w.  j ava2s  .com
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
private void initComponents() {//GEN-BEGIN:initComponents
    pnlMain = new javax.swing.JPanel();

    setTitle("JasperViewer");
    setIconImage(
            new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                    .getImage());
    addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm();
        }
    });

    pnlMain.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

    pack();

    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    java.awt.Dimension screenSize = toolkit.getScreenSize();
    int screenResolution = toolkit.getScreenResolution();
    float zoom = ((float) screenResolution) / net.sf.jasperreports.swing.JRViewerPanel.REPORT_RESOLUTION;

    int height = (int) (550 * zoom);
    if (height > screenSize.getHeight()) {
        height = (int) screenSize.getHeight();
    }
    int width = (int) (750 * zoom);
    if (width > screenSize.getWidth()) {
        width = (int) screenSize.getWidth();
    }

    java.awt.Dimension dimension = new java.awt.Dimension(width, height);
    setSize(dimension);
    setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
}

From source file:HardcopyWriter.java

/**
 * The constructor for this class has a bunch of arguments: The frame argument
 * is required for all printing in Java. The jobname appears left justified at
 * the top of each printed page. The font size is specified in points, as
 * on-screen font sizes are. The margins are specified in inches (or fractions
 * of inches)./*from   w w w . j  av a2  s  . c om*/
 */
public HardcopyWriter(Frame frame, String jobname, int fontsize, double leftmargin, double rightmargin,
        double topmargin, double bottommargin) throws HardcopyWriter.PrintCanceledException {
    // Get the PrintJob object with which we'll do all the printing.
    // The call is synchronized on the static printprops object, which
    // means that only one print dialog can be popped up at a time.
    // If the user clicks Cancel in the print dialog, throw an exception.
    Toolkit toolkit = frame.getToolkit(); // get Toolkit from Frame
    synchronized (printprops) {
        job = toolkit.getPrintJob(frame, jobname, printprops);
    }
    if (job == null)
        throw new PrintCanceledException("User cancelled print request");

    pagesize = job.getPageDimension(); // query the page size
    pagedpi = job.getPageResolution(); // query the page resolution

    // Bug Workaround:
    // On windows, getPageDimension() and getPageResolution don't work, so
    // we've got to fake them.
    if (System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7)) {
        // Use screen dpi, which is what the PrintJob tries to emulate
        pagedpi = toolkit.getScreenResolution();
        // Assume a 8.5" x 11" page size. A4 paper users must change this.
        pagesize = new Dimension((int) (8.5 * pagedpi), 11 * pagedpi);
        // We also have to adjust the fontsize. It is specified in points,
        // (1 point = 1/72 of an inch) but Windows measures it in pixels.
        fontsize = fontsize * pagedpi / 72;
    }

    // Compute coordinates of the upper-left corner of the page.
    // I.e. the coordinates of (leftmargin, topmargin). Also compute
    // the width and height inside of the margins.
    x0 = (int) (leftmargin * pagedpi);
    y0 = (int) (topmargin * pagedpi);
    width = pagesize.width - (int) ((leftmargin + rightmargin) * pagedpi);
    height = pagesize.height - (int) ((topmargin + bottommargin) * pagedpi);

    // Get body font and font size
    font = new Font("Monospaced", Font.PLAIN, fontsize);
    metrics = frame.getFontMetrics(font);
    lineheight = metrics.getHeight();
    lineascent = metrics.getAscent();
    charwidth = metrics.charWidth('0'); // Assumes a monospaced font!

    // Now compute columns and lines will fit inside the margins
    chars_per_line = width / charwidth;
    lines_per_page = height / lineheight;

    // Get header font information
    // And compute baseline of page header: 1/8" above the top margin
    headerfont = new Font("SansSerif", Font.ITALIC, fontsize);
    headermetrics = frame.getFontMetrics(headerfont);
    headery = y0 - (int) (0.125 * pagedpi) - headermetrics.getHeight() + headermetrics.getAscent();

    // Compute the date/time string to display in the page header
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
    df.setTimeZone(TimeZone.getDefault());
    time = df.format(new Date());

    this.jobname = jobname; // save name
    this.fontsize = fontsize; // save font size
}

From source file:org.pgptool.gui.ui.tools.UiUtils.java

public static void setLookAndFeel() {
    // NOTE: We doing it this way to prevent dead=locks that is sometimes
    // happens if do it in main thread
    Edt.invokeOnEdtAndWait(new Runnable() {
        @Override/*from www.  j  a  v a  2 s .  com*/
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                fixCheckBoxMenuItemForeground();
                fixFontSize();
            } catch (Throwable t) {
                log.error("Failed to set L&F", t);
            }
        }

        /**
         * In some cases (depends on OS theme) check menu item foreground is same as
         * bacground - thus it;'s invisible when cheked
         */
        private void fixCheckBoxMenuItemForeground() {
            UIDefaults defaults = UIManager.getDefaults();
            Color selectionForeground = defaults.getColor("CheckBoxMenuItem.selectionForeground");
            Color foreground = defaults.getColor("CheckBoxMenuItem.foreground");
            Color background = defaults.getColor("CheckBoxMenuItem.background");
            if (colorsDiffPercentage(selectionForeground, background) < 10) {
                // TODO: That doesn't actually affect defaults. Need to find out how to fix it
                defaults.put("CheckBoxMenuItem.selectionForeground", foreground);
            }
        }

        private int colorsDiffPercentage(Color c1, Color c2) {
            int diffRed = Math.abs(c1.getRed() - c2.getRed());
            int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
            int diffBlue = Math.abs(c1.getBlue() - c2.getBlue());

            float pctDiffRed = (float) diffRed / 255;
            float pctDiffGreen = (float) diffGreen / 255;
            float pctDiffBlue = (float) diffBlue / 255;

            return (int) ((pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100);
        }

        private void fixFontSize() {
            if (isJreHandlesScaling()) {
                log.info("JRE handles font scaling, won't change it");
                return;
            }
            log.info("JRE doesnt't seem to support font scaling");

            Toolkit toolkit = Toolkit.getDefaultToolkit();
            int dpi = toolkit.getScreenResolution();
            if (dpi == 96) {
                if (log.isDebugEnabled()) {
                    Font font = UIManager.getDefaults().getFont("TextField.font");
                    String current = font != null ? Integer.toString(font.getSize()) : "unknown";
                    log.debug(
                            "Screen dpi seem to be 96. Not going to change font size. Btw current size seem to be "
                                    + current);
                }
                return;
            }
            int targetFontSize = 12 * dpi / 96;
            log.debug("Screen dpi = " + dpi + ", decided to change font size to " + targetFontSize);
            setDefaultSize(targetFontSize);
        }

        private boolean isJreHandlesScaling() {
            try {
                JreVersion noNeedToScaleForVer = JreVersion.parseString("9");
                String jreVersionStr = System.getProperty("java.version");
                if (jreVersionStr != null) {
                    JreVersion curVersion = JreVersion.parseString(jreVersionStr);
                    if (noNeedToScaleForVer.compareTo(curVersion) <= 0) {
                        return true;
                    }
                }

                return false;
            } catch (Throwable t) {
                log.warn("Failed to see oif JRE can handle font scaling. Will assume it does. JRE version: "
                        + System.getProperty("java.version"), t);
                return true;
            }
        }

        public void setDefaultSize(int size) {
            Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
            Object[] keys = keySet.toArray(new Object[keySet.size()]);
            for (Object key : keys) {
                if (key != null && key.toString().toLowerCase().contains("font")) {
                    Font font = UIManager.getDefaults().getFont(key);
                    if (font != null) {
                        Font changedFont = font.deriveFont((float) size);
                        UIManager.put(key, changedFont);
                        Font doubleCheck = UIManager.getDefaults().getFont(key);
                        log.debug("Font size changed for " + key + ". From " + font.getSize() + " to "
                                + doubleCheck.getSize());
                    }
                }
            }
        }
    });
    log.info("L&F set");
}