Resize drawing to match frame size

前端 未结 4 1661
自闭症患者
自闭症患者 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:19

    Assuming you rename your method that paints for 300x300 as paint300, define a buffered image:

    @Override public void paint(Graphics g) {
         Image bufferImage = createImage(300, 300);  // empty image
         paint300(bufferImage.getGraphics());  // fill the image
         g.drawImage(bufferImage, 0, 0, null);  // send the image to graphics device
    }
    

    Above is when you want to draw at full size (300x300). If your window is resized:

    @Override public void paint(Graphics g) {
         Image bufferImage = createImage(300, 300);  
         paint300(bufferImage.getGraphics());
         int width = getWidth();
         int height = getHeight(); 
         CropImageFilter crop = 
             new CropImageFilter((300 - width)/2, (300 - height)/2 , width, height);
         FilteredImageSource fis = new FilteredImageSource(bufferImage, crop);
         Image croppedImage = createImage(fis);
         g.drawImage(croppedImage, 0, 0, null);
    }
    

    The new classes are from from java.awt.image.*.

    I didn't test this code. It's just to send you in the right direction.

提交回复
热议问题