Assuming you rename your method, which paints for 300x300 as paint300, define a buffer image:
@Override public void paint(Graphics g) { Image bufferImage = createImage(300, 300);
Above, when you want to paint in 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); }
New classes are taken from java.awt.image. *.
I have not tested this code. This is just to send you in the right direction.
toto2 source share