Resize drawing to match frame size

前端 未结 4 1640
自闭症患者
自闭症患者 2020-12-19 09:40

I\'ve written an app that custom draws everything inside paint() based on fixed pixel positions. Then I disabled resize of the frame so its always visible.

However,

4条回答
  •  时光说笑
    2020-12-19 10:28

    You could do this with some math.

    public void paint(Graphics g){
        int height = 100;
        int width = 100;
    
        int x = (this.getWidth() / 2) - (width / 2);
        int y = (this.getHeight() / 2) - (height / 2);
    
        g.setColor(Color.BLACK);
        g.fill3DRect(x, y, width, height, true);
    
    }
    

    Or if you wanted to keep the width and height of the box with the same proportion, use int width = this.getWidth() / 3; and int height = this.getHeight() / 3.

    The other option is to use Graphics2D.scale(), as JB pointed out, the passed Graphics object is actually a Graphics2D object.

提交回复
热议问题