Resize drawing to fit frame size

I wrote an application in which the user draws everything inside paint () based on fixed pixel positions. Then I turned off the frame size so that it was always visible.

However, now I would like to be able to resize, but I do not want to change my scroll code. I was hoping I could capture a 300x300 square of a Graphics g and resize it to the current JFrame size after all my spread code, but I have no idea what I'm doing.

Here is a sample code. In this, I want the 100x100 square to remain in the middle, proportional to the size of the JFrame:

package DrawAndScale; import java.awt.Color; import java.awt.Graphics; public class DASFrame extends javax.swing.JFrame { public DASFrame() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); this.setSize(300, 300); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DASFrame().setVisible(true); } }); } @Override public void paint(Graphics g) { g.setColor(Color.BLACK); g.fill3DRect(100, 100, 100, 100, true); } } 

Thanks.

+6
source share
4 answers

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); // empty image paint300(bufferImage.getGraphics()); // fill the image g.drawImage(bufferImage, 0, 0, null); // send the image to graphics device } 

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.

+3
source

if you want to paint custom paint, look for JLabel or JPanel , including Icon / ImageIcon inside, a simple example about this

enter image description hereenter image description here

enter image description here

 import java.awt.*; import javax.swing.*; public class MainComponentPaint extends JFrame { private static final long serialVersionUID = 1L; public MainComponentPaint() { setTitle("Customize Preffered Size Test"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void display() { add(new CustomComponent()); pack(); setMinimumSize(getSize()); setPreferredSize(getSize()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { setVisible(true); } }); } public static void main(String[] args) { MainComponentPaint main = new MainComponentPaint(); main.display(); } } class CustomComponent extends JComponent { private static final long serialVersionUID = 1L; @Override public Dimension getPreferredSize() { return new Dimension(50, 50); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); for (int i = 0; i < Math.max(w, h); i += 20) { g.drawLine(i, 0, i, h); g.drawLine(0, i, w, i); } } } 
+2
source

Not an expert, but you can simply scale the Graphics2D object (the past Graphics is actually an instance of Graphics2D), where the x and y ratios are the fixed-size ratios that you selected and the actual frame size.

See http://download.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#scale%28double,%20double%29

+1
source

You can 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 want to keep the width and height of the window in the same proportion, use int width = this.getWidth() / 3; and int height = this.getHeight() / 3 .

Another option is to use Graphics2D.scale() , as JB noted, the transferred Graphics object is actually a Graphics2D object.

+1
source

Source: https://habr.com/ru/post/895425/


All Articles