计算填充以使矩形居中对齐(按百分比调整大小)

时间:2018-09-12 07:37:05

标签: java algorithm swing

enter image description here

下面是我当前的算法,用于在画布空间(代表图标)的中心对齐矩形(代表符号)。这只是我感兴趣的算法,因此请忽略其余代码,因为它只是出于演示目的而作为视觉辅助。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class IconSymbol extends JFrame {

    public IconSymbol(double iWH, double s, double w, double h) {

        getContentPane().add(new Canvas((int)iWH, s, w, h));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize((int)iWH, (int)iWH);
        setVisible(true);        
    }

    public static void main(String arg[]) {
        IconSymbol is = new IconSymbol(100, 0.9, 50, 50);
    }

    class Canvas extends JPanel {

        // STIPULATED
        double iconWH = 0;
        double sScale = 0;
        double sWidth = 0;
        double sHeight = 0;

        // CALCULATED
        double padX = 0;
        double padY = 0;
        double xOffSet = 0;
        double yOffSet = 0;

        public Canvas(double iWH,double sS,double sW,double sH) {
            this.iconWH = iWH;
            this.sScale = sS;
            this.sWidth = sW;
            this.sHeight = sH;
        }

    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;

        g2D.setBackground(Color.WHITE);             

        g2D.setPaint(Color.BLUE);

        Shape icon = new Rectangle.Double(0,0,(int)iconWH,(int)iconWH);
        g2D.fill(icon);

        g2D.setPaint(Color.BLACK);

        int width = (int)iconWH / 10;
        int height= (int)iconWH / 10;
        for(int row=0;row<10;row++){
            for(int col=0;col<10;col++){
                g.drawRect(row*width,col*height,width,height);
            }
        }            

        Point off = algorithm(); 

        g2D.setPaint(Color.RED);            

        Shape s = new Rectangle.Double(off.x,off.y,(int)sWidth,(int)sHeight);

        AffineTransform tran = AffineTransform.getScaleInstance(sScale, sScale);

        g2D.fill(tran.createTransformedShape(s));

    }

    public Point algorithm(){
        // ALGORITHM WITH EXACT NEEDED PARAMETERS
        padX = (sWidth - ((sWidth * sScale))) / 2;
        padY = (sHeight - ((sHeight * sScale))) / 2;
        xOffSet = padX + ((iconWH - (sWidth * sScale)) / 2);
        yOffSet = padX + ((iconWH - (sHeight * sScale)) / 2);            
        Point point = new Point((int)xOffSet, (int)yOffSet);
        return point;
    }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码的问题在于,比例变换tran缩放了矩形off以及sWidthsHeight的计算出的原点。如果要保持当前方案,则需要在algorithm方法中将比例转换的逆应用于计算出的偏移量:

public Point algorithm(){
    // ALGORITHM WITH EXACT NEEDED PARAMETERS
    xOffSet = ((iconWH - (sWidth * sScale)) / 2) / sScale;
    yOffSet = ((iconWH - (sHeight * sScale)) / 2) / sScale; 
    Point point = new Point((int)xOffSet, (int)yOffSet);
    return point;
}

请注意,我删除了padXpadY,因为它们不需要计算偏移量。