在任何屏幕分辨率图像的位置 [英] Position Image in any Screen Resolution

查看:374
本文介绍了在任何屏幕分辨率图像的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的程序有问题。每次当我改变我的屏幕分辨率的时候,我的形象开始搬出位置。如何让我的形象留在同一个位置,即使我改变任何其他决议有什么建议?

  P2 =新JPanel();
p2.setLayout(新的FlowLayout());
ImageIcon的IMG2 =新的ImageIcon(C:\\\\ \\\\鼓Invisible4.png);
jbtn2 =新的JLabel(IMG2);
p2.add(jbtn2);
加(jbtn2);
jbtn2.setSize(jbtn2.get preferredSize());
jbtn2.setLocation(140,380);


解决方案

我preFER总是试图在那里我可以,它使生活一般从长远来看更容易框架范围内工作。

摇摆已经开始设计了的LayoutManager 来上班S,这意味着摆动更新它的组件和通信,这些变化的方式是基于使用<$ C的$ C>的LayoutManager S(或多或少)。

下面的示例使用作品从<一个href=\"http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928\">Java:保持的JPanel背景图片的宽高比来缩放图像和 PropertionalLayoutManager 其目的是尝试和缩放不仅大小,而且部件的位置基于父容器的大小

PropertionalLayoutManager 这里演示将尝试和布局围绕父容器中心的组成部分。你可以改变这一点,但它看起来怪异 - 恕我直言

 进口java.awt.BorderLayout中;
进口java.awt.Component中;
进口java.awt.Container中;
进口java.awt.Dimension中;
进口java.awt.EventQueue中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.LayoutManager2;
进口java.awt.RenderingHints中;
进口java.awt.Transparency;
进口java.awt.image.BufferedImage中;
进口的java.io.File;
进口java.io.IOException异常;
进口的java.util.Map;
进口java.util.WeakHashMap中;
进口javax.imageio.ImageIO中;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.UIManager中;
进口javax.swing.UnsupportedLookAndFeelException;公共类鼓{    受保护的静态BufferedImage的象征;
    保护静态的BufferedImage桶;    公共静态无效的主要(字串[] args){
        新鼓();
    }    公共鼓(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的| InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException前){
                }                JFrame的帧=新的JFrame(测试);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(新的BorderLayout());
                frame.add(新TestPane());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }    公共类TestPane继承JPanel {        公共TestPane(){
            的setLayout(新PropertionalLayoutManager(400,400));
            添加(新符号(),新PropertionalConstraint(0F,0));
            添加(新符号(),新PropertionalConstraint(0.67f,0));
            添加(新符号(),新PropertionalConstraint(0F,0.4675f));
            添加(新符号(),新PropertionalConstraint(0.67f,0.4675f));
            添加(新鼓(),新PropertionalConstraint(0.205f,0.1F));
            添加(新鼓(),新PropertionalConstraint(0.5F,0.1F));
            添加(新鼓(),新PropertionalConstraint(0F,0.33f));
            添加(新鼓(),新PropertionalConstraint(0.705f,0.33f));
        }
    }    公共类PropertionalConstraint {        私人浮动X;
        私人浮动Ÿ;        公共PropertionalConstraint(浮点X,浮法Y){
            this.x = X;
            this.y = Y;
        }        公众持股量的getX(){
            返回X;
        }        公众持股量的getY(){
            返回是;
        }
    }    公共类PropertionalLayoutManager实现LayoutManager2中的{        私人地图&lt;成分,PropertionalConstraint&GT;限制;
        私人尺寸defaultSize;        公共PropertionalLayoutManager(INT defaultWidth,诠释defaultHeight){
            约束=新的WeakHashMap&LT;&GT;(25);
            defaultSize =新的Dimension(defaultWidth,defaultHeight);
        }        @覆盖
        公共无效addLayoutComponent方法(组件补偿,对象约束){
            如果(约束的instanceof PropertionalConstraint){
                constraints.put(补偿,((PropertionalConstraint)约束));
            }
        }        @覆盖
        公共尺寸的maximumLayoutSize(Container target)在{
            返回preferredLayoutSize(目标);
        }        @覆盖
        公众持股量getLayoutAlignmentX(Container target)在{
            返回0.5F;
        }        @覆盖
        公众持股量中的getLayoutAlignmentY(Container target)在{
            返回0.5F;
        }        @覆盖
        公共无效的invalidateLayout(Container target)在{
        }        @覆盖
        公共无效addLayoutComponent方法(字符串名称,组件comp){
        }        @覆盖
        公共无效中的removeLayoutComponent(组件comp){
            constraints.remove(化合物);
        }        @覆盖
        公共尺寸preferredLayoutSize(集装箱父){            返回defaultSize;        }        @覆盖
        公共尺寸的minimumLayoutSize(集装箱父){
            返回preferredLayoutSize(父);
        }        @覆盖
        公共无效layoutContainer(集装箱父){            INT宽度= parent.getWidth();
            INT高度= parent.getHeight();            双dScaleWidth = getScaleFactor(defaultSize.width,宽度);
            双dScaleHeight = getScaleFactor(defaultSize.height,高度);            双scaleSize = Math.min(dScaleHeight,dScaleWidth);
            INT minRange = Math.min(宽度,高度);            如果(宽度大于0&放大器;&放大器;高度大于0){                INT MAXY = 0;
                INT MAXX = 0;                为(成分排版:parent.getComponents()){
                    PropertionalConstraint p值= constraints.get(化合物);
                    如果(P!= NULL){
                        尺寸prefSize = comp.get preferredSize();                        prefSize.width * = scaleSize;
                        prefSize.height * = scaleSize;                        INT X = Math.round(minRange * p.getX());
                        INT Y = Math.round(minRange * p.getY());                        comp.setBounds(X,Y,prefSize.width,prefSize.height);                        MAXX = Math.max(MAXX,X + prefSize.width);
                        MAXY = Math.max(MAXY,Y + prefSize.height);
                    }其他{
                        comp.setBounds(0,0,0,0);
                    }
                }                为(成分排版:parent.getComponents()){
                    的System.out.println(MAXX =+ MAXX);
                    的System.out.println(MAXY =+ MAXY);
                    如果(comp.getWidth()大于0&放大器;&放大器; comp.getHeight()大于0){
                        INT X =((宽 - MAXX)/ 2)+ comp.getX();
                        INT Y =((高度 - MAXY)/ 2)+ comp.getY();
                        comp.setLocation(X,Y);
                    }
                }            }其他{                为(成分排版:parent.getComponents()){
                    comp.setBounds(0,0,0,0);
                }            }        }
    }    公共抽象类AbstractKitPiecePane继承JPanel {        私人BufferedImage的比例;        公共AbstractKitPiecePane(){
            setOpaque(假);
        }        公共抽象的BufferedImage getKitImage();        @覆盖
        公共尺寸的get preferredSize(){
            返回新的Dimension(getKitImage()的getWidth(),getKitImage()的getHeight());
        }        @覆盖
        公共无效无效(){
            super.invalidate();
            如果(的getWidth()大于0&放大器;&放大器;的getHeight()大于0){
                缩放= getScaledInstanceToFit(getKitImage(),的getSize());
            }其他{
                缩放=无效;
            }
        }        @覆盖
        保护无效paintComponent(图形G){
            super.paintComponent方法(G);
            如果(缩放!= NULL){
                INT X =(的getWidth() - scaled.getWidth())/ 2;
                INT Y =(的getHeight() - scaled.getHeight())/ 2;
                g.drawImage(缩放,X,Y,这一点);
            }
        }
    }    公共类鼓扩展AbstractKitPiecePane {        @覆盖
        公众的BufferedImage getKitImage(){
            返回DRUM;
        }
    }    公共类符号扩展AbstractKitPiecePane {        @覆盖
        公众的BufferedImage getKitImage(){
            返回符号;
        }
    }    保护静态的BufferedImage getScaledInstance(BufferedImage的IMG,双dScaleFactor){        BufferedImage的imgScale = IMG;        INT iImageWidth =(INT)Math.round(img.getWidth()* dScaleFactor);
        INT iImageHeight =(INT)Math.round(img.getHeight()* dScaleFactor);        如果(dScaleFactor&LT; = 1.0D){            imgScale = getScaledDownInstance(IMG,iImageWidth,iImageHeight,RenderingHints.VALUE_INTERPOLATION_BILINEAR);        }其他{            imgScale = getScaledUpInstance(IMG,iImageWidth,iImageHeight,RenderingHints.VALUE_INTERPOLATION_BILINEAR);        }        返回imgScale;    }    保护静态的BufferedImage getScaledDownInstance(BufferedImage的IMG,
            INT targetWidth,
            INT targetHeight,
            对象提示){//的System.out.println(按比例缩小......);
        整型=(img.getTransparency()==为Transparency.OPAQUE)
                ? BufferedImage.TYPE_INT_RGB:BufferedImage.TYPE_INT_ARGB;        RET的BufferedImage =(BufferedImage的)IMG;        如果(targetHeight大于0 || targetWidth大于0){
            INT W,H;
            W = img.getWidth();
            H = img.getHeight();            做{                如果(并且R w targetWidth){
                    重量/ = 2;
                    如果(W&LT; targetWidth){
                        W = targetWidth;
                    }
                }                如果(H&GT; targetHeight){
                    的h / = 2;
                    如果(H&LT; targetHeight){
                        H = targetHeight;
                    }
                }                BufferedImage的TMP =新的BufferedImage(Math.max(W,1),Math.max(H,1)型);
                G2的Graphics2D = tmp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,暗示);
                g2.drawImage(保留,0,0,W,H,NULL);
                g2.dispose();                RET = tmp目录;            }而(W = targetWidth || H = targetHeight!);
        }其他{
            RET =新的BufferedImage(1,1,类型);
        }        返回RET;
    }    保护静态的BufferedImage getScaledUpInstance(BufferedImage的IMG,
            INT targetWidth,
            INT targetHeight,
            对象提示){        整型= BufferedImage.TYPE_INT_ARGB;        RET的BufferedImage =(BufferedImage的)IMG;
        INT W,H;
        W = img.getWidth();
        H = img.getHeight();        做{
            如果(W&LT; targetWidth){
                W * = 2;
                如果(并且R w targetWidth){
                    W = targetWidth;
                }
            }            如果(H&LT; targetHeight){
                H * = 2;
                如果(H&GT; targetHeight){
                    H = targetHeight;
                }
            }            BufferedImage的TMP =新的BufferedImage(W,H,类型);
            G2的Graphics2D = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,暗示);
            g2.drawImage(保留,0,0,W,H,NULL);
            g2.dispose();            RET = tmp目录;
            TMP = NULL;
        }而(W = targetWidth || H = targetHeight!);        返回RET;
    }    公共静态的BufferedImage getScaledInstanceToFit(BufferedImage的IMG,尺寸大小){
        双比例因子= getScaleFactorToFit(IMG,大小);
        返回getScaledInstance(IMG,比例因子);
    }    公共静态双getScaleFactorToFit(BufferedImage的IMG,尺寸大小){
        双DSCALE = 1;        如果(IMG!= NULL){
            INT imageWidth = img.getWidth();
            INT imageHeight = img.getHeight();
            DSCALE = getScaleFactorToFit(新尺寸(imageWidth,imageHeight),大小);
        }        返回DSCALE;
    }    公共静态双getScaleFactorToFit(原尺寸,外形尺寸toFit){
        双DSCALE = 1D;        如果(原件= NULL&放大器;!&安培;!toFit = NULL){
            双dScaleWidth = getScaleFactor(original.width,toFit.width);
            双dScaleHeight = getScaleFactor(original.height,toFit.height);
            DSCALE = Math.min(dScaleHeight,dScaleWidth);
        }        返回DSCALE;
    }    公共静态双getScaleFactor(INT iMasterSize,诠释iTargetSize){
        双DSCALE = 1;
        如果(iMasterSize&GT; iTargetSize){
            DSCALE =(双)iTargetSize /(双)iMasterSize;
        }其他{
            DSCALE =(双)iTargetSize /(双)iMasterSize;
        }        返回DSCALE;
    }    静态的 {        尝试{
            SYMBOL = ImageIO.read(新文件(Symbol.png));
        }赶上(IOException异常前){
            ex.printStackTrace();
        }
        尝试{
            DRUM = ImageIO.read(新文件(Drum.png));
        }赶上(IOException异常前){
            ex.printStackTrace();
        }    }
}

和程序中使用,为您的享受图像。他们住在程序从

运行相同的目录

I have a problem with my program. Every time when I change my screen resolution, my image starts to move out of position. Any suggestions on how to make my image stay in the same location even though I change to any other resolution?

p2 = new JPanel();
p2.setLayout(new FlowLayout());      
ImageIcon img2 = new ImageIcon("C:\\Drum\\Invisible4.png");             
jbtn2 = new JLabel(img2);
p2.add(jbtn2);                      
add(jbtn2);
jbtn2.setSize(jbtn2.getPreferredSize());
jbtn2.setLocation(140, 380);

解决方案

I prefer to always try and work within boundaries of the framework where I can, it makes life generally easier in the long run.

Swing has begin designed to work with LayoutManagers, this means that the way that Swing updates it's components and communicates these changes is based on the use of the LayoutManagers (more or less).

The following example uses pieces from Java: maintaining aspect ratio of JPanel background image to scale the images and a PropertionalLayoutManager which is designed to try and scale not only the size, but also the position of the components based on the size of the parent container.

The PropertionalLayoutManager demonstrated here will try and layout the components around the center of the parent container. You can change this, but it looks weird - IMHO

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager2;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.WeakHashMap;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Drums {

    protected static BufferedImage SYMBOL;
    protected static BufferedImage DRUM;

    public static void main(String[] args) {
        new Drums();
    }

    public Drums() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new PropertionalLayoutManager(400, 400));
            add(new Symbol(), new PropertionalConstraint(0f, 0));
            add(new Symbol(), new PropertionalConstraint(0.67f, 0));
            add(new Symbol(), new PropertionalConstraint(0f, 0.4675f));
            add(new Symbol(), new PropertionalConstraint(0.67f, 0.4675f));
            add(new Drum(), new PropertionalConstraint(0.205f, 0.1f));
            add(new Drum(), new PropertionalConstraint(0.5f, 0.1f));
            add(new Drum(), new PropertionalConstraint(0f, 0.33f));
            add(new Drum(), new PropertionalConstraint(0.705f, 0.33f));
        }
    }

    public class PropertionalConstraint {

        private float x;
        private float y;

        public PropertionalConstraint(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float getX() {
            return x;
        }

        public float getY() {
            return y;
        }
    }

    public class PropertionalLayoutManager implements LayoutManager2 {

        private Map<Component, PropertionalConstraint> constraints;
        private Dimension defaultSize;

        public PropertionalLayoutManager(int defaultWidth, int defaultHeight) {
            constraints = new WeakHashMap<>(25);
            defaultSize = new Dimension(defaultWidth, defaultHeight);
        }

        @Override
        public void addLayoutComponent(Component comp, Object constraint) {
            if (constraint instanceof PropertionalConstraint) {
                constraints.put(comp, ((PropertionalConstraint) constraint));
            }
        }

        @Override
        public Dimension maximumLayoutSize(Container target) {
            return preferredLayoutSize(target);
        }

        @Override
        public float getLayoutAlignmentX(Container target) {
            return 0.5f;
        }

        @Override
        public float getLayoutAlignmentY(Container target) {
            return 0.5f;
        }

        @Override
        public void invalidateLayout(Container target) {
        }

        @Override
        public void addLayoutComponent(String name, Component comp) {
        }

        @Override
        public void removeLayoutComponent(Component comp) {
            constraints.remove(comp);
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {

            return defaultSize;

        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return preferredLayoutSize(parent);
        }

        @Override
        public void layoutContainer(Container parent) {

            int width = parent.getWidth();
            int height = parent.getHeight();

            double dScaleWidth = getScaleFactor(defaultSize.width, width);
            double dScaleHeight = getScaleFactor(defaultSize.height, height);

            double scaleSize = Math.min(dScaleHeight, dScaleWidth);
            int minRange = Math.min(width, height);

            if (width > 0 && height > 0) {

                int maxY = 0;
                int maxX = 0;

                for (Component comp : parent.getComponents()) {
                    PropertionalConstraint p = constraints.get(comp);
                    if (p != null) {
                        Dimension prefSize = comp.getPreferredSize();

                        prefSize.width *= scaleSize;
                        prefSize.height *= scaleSize;

                        int x = Math.round(minRange * p.getX());
                        int y = Math.round(minRange * p.getY());

                        comp.setBounds(x, y, prefSize.width, prefSize.height);

                        maxX = Math.max(maxX, x + prefSize.width);
                        maxY = Math.max(maxY, y + prefSize.height);
                    } else {
                        comp.setBounds(0, 0, 0, 0);
                    }
                }

                for (Component comp : parent.getComponents()) {
                    System.out.println("maxX = " + maxX);
                    System.out.println("maxY = " + maxY);
                    if (comp.getWidth() > 0 && comp.getHeight() > 0) {
                        int x = ((width - maxX) / 2) + comp.getX();
                        int y = ((height - maxY) / 2) + comp.getY();
                        comp.setLocation(x, y);
                    }                    
                }

            } else {

                for (Component comp : parent.getComponents()) {
                    comp.setBounds(0, 0, 0, 0);
                }

            }

        }
    }

    public abstract class AbstractKitPiecePane extends JPanel {

        private BufferedImage scaled;

        public AbstractKitPiecePane() {
            setOpaque(false);
        }

        public abstract BufferedImage getKitImage();

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(getKitImage().getWidth(), getKitImage().getHeight());
        }

        @Override
        public void invalidate() {
            super.invalidate();
            if (getWidth() > 0 && getHeight() > 0) {
                scaled = getScaledInstanceToFit(getKitImage(), getSize());
            } else {
                scaled = null;
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (scaled != null) {
                int x = (getWidth() - scaled.getWidth()) / 2;
                int y = (getHeight() - scaled.getHeight()) / 2;
                g.drawImage(scaled, x, y, this);
            }
        }
    }

    public class Drum extends AbstractKitPiecePane {

        @Override
        public BufferedImage getKitImage() {
            return DRUM;
        }
    }

    public class Symbol extends AbstractKitPiecePane {

        @Override
        public BufferedImage getKitImage() {
            return SYMBOL;
        }
    }

    protected static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {

        BufferedImage imgScale = img;

        int iImageWidth = (int) Math.round(img.getWidth() * dScaleFactor);
        int iImageHeight = (int) Math.round(img.getHeight() * dScaleFactor);

        if (dScaleFactor <= 1.0d) {

            imgScale = getScaledDownInstance(img, iImageWidth, iImageHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        } else {

            imgScale = getScaledUpInstance(img, iImageWidth, iImageHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        }

        return imgScale;

    }

    protected static BufferedImage getScaledDownInstance(BufferedImage img,
            int targetWidth,
            int targetHeight,
            Object hint) {

//        System.out.println("Scale down...");
        int type = (img.getTransparency() == Transparency.OPAQUE)
                ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

        BufferedImage ret = (BufferedImage) img;

        if (targetHeight > 0 || targetWidth > 0) {
            int w, h;
            w = img.getWidth();
            h = img.getHeight();

            do {

                if (w > targetWidth) {
                    w /= 2;
                    if (w < targetWidth) {
                        w = targetWidth;
                    }
                }

                if (h > targetHeight) {
                    h /= 2;
                    if (h < targetHeight) {
                        h = targetHeight;
                    }
                }

                BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
                Graphics2D g2 = tmp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
                g2.drawImage(ret, 0, 0, w, h, null);
                g2.dispose();

                ret = tmp;

            } while (w != targetWidth || h != targetHeight);
        } else {
            ret = new BufferedImage(1, 1, type);
        }

        return ret;
    }

    protected static BufferedImage getScaledUpInstance(BufferedImage img,
            int targetWidth,
            int targetHeight,
            Object hint) {

        int type = BufferedImage.TYPE_INT_ARGB;

        BufferedImage ret = (BufferedImage) img;
        int w, h;
        w = img.getWidth();
        h = img.getHeight();

        do {
            if (w < targetWidth) {
                w *= 2;
                if (w > targetWidth) {
                    w = targetWidth;
                }
            }

            if (h < targetHeight) {
                h *= 2;
                if (h > targetHeight) {
                    h = targetHeight;
                }
            }

            BufferedImage tmp = new BufferedImage(w, h, type);
            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
            g2.drawImage(ret, 0, 0, w, h, null);
            g2.dispose();

            ret = tmp;
            tmp = null;
        } while (w != targetWidth || h != targetHeight);

        return ret;
    }

    public static BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) {
        double scaleFactor = getScaleFactorToFit(img, size);
        return getScaledInstance(img, scaleFactor);
    }

    public static double getScaleFactorToFit(BufferedImage img, Dimension size) {
        double dScale = 1;

        if (img != null) {
            int imageWidth = img.getWidth();
            int imageHeight = img.getHeight();
            dScale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
        }

        return dScale;
    }

    public static double getScaleFactorToFit(Dimension original, Dimension toFit) {
        double dScale = 1d;

        if (original != null && toFit != null) {
            double dScaleWidth = getScaleFactor(original.width, toFit.width);
            double dScaleHeight = getScaleFactor(original.height, toFit.height);
            dScale = Math.min(dScaleHeight, dScaleWidth);
        }

        return dScale;
    }

    public static double getScaleFactor(int iMasterSize, int iTargetSize) {
        double dScale = 1;
        if (iMasterSize > iTargetSize) {
            dScale = (double) iTargetSize / (double) iMasterSize;
        } else {
            dScale = (double) iTargetSize / (double) iMasterSize;
        }

        return dScale;
    }

    static {

        try {
            SYMBOL = ImageIO.read(new File("Symbol.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            DRUM = ImageIO.read(new File("Drum.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

And the images used within the program, for your enjoyment. They live in the same directory the program is run from

这篇关于在任何屏幕分辨率图像的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆