Resize drawing to match frame size

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

    Not an expert, but you could just scale the Graphics2D object (the passed Graphics is in fact a Graphics2D instance), where the x and y ratios are the ratios of the fixed size you chose to draw and the actual size of the frame.

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 10:19

    if you want to painting Custom paint then look for JLabel or JPanel and including Icon/ImageIcon inside, simple example about that

    enter image description here enter 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);
            }
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题