简体   繁体   English

Java:油漆中的测量

[英]Java: Measurement in paint

I'm still fairly new to java and I've been having trouble with the paint method. 我对Java还是很陌生,并且在paint方法方面遇到了麻烦。

when I learning how to use bufferedimages, I thought that the image sizes were a bit off relative to the frame size so I tested it out by changing the frame size to make the width & height multiples of 30: 当我学习如何使用bufferedimages时,我认为图像大小相对于帧大小略有偏离,因此我通过更改帧大小以使宽度和高度为30的倍数进行了测试:

int width = 1020;   //34 * 30
int height = 750;   //25 * 30
JFrame frame = new JFrame("Test");
GridT testGrid = new GridT();
frame.add(testGrid);
frame.setSize(width, height); 

Next I had the paint method draw a grid of 30 by 30 squares and, as I suspected, the grid was cut off at the edges: 接下来,我用绘制方法绘制了一个30 x 30的网格,并且我怀疑网格在边缘被切掉了:

在此处输入图片说明

(Note: The lines are all the same color and tickness but may appear differnt when the screenshot is resized.) (注意:这些行的颜色和刻度都相同,但是在调整屏幕截图大小时,它们可能看起来不同。)

Does paint use a different unit of measurent than the JFrame? 绘画使用的度量单位与JFrame是否不同? If so, how much is it off by? 如果是这样,多少钱呢? If not, what could I be doing wrong? 如果没有,我可能做错了什么?

You paint on a JPanel, overriding the paintComponent method. 您可以在JPanel上进行绘制,从而覆盖paintComponent方法。 You set the preferred size of the JPanel to be the size of your drawing area. 您可以将JPanel的首选大小设置为绘图区域的大小。 You pack the JFrame. 您打包JFrame。 The JFrame will be bigger than your drawing area because of the frame decorations. 由于框架装饰,JFrame将大于您的绘图区域。

You do have to add 2 pixels to the height and the width of the drawing area to see the grid. 您必须在绘图区域的高度和宽度上添加2个像素才能看到网格。 You start at 1,1; 您从1,1开始; rather than 0,0. 而不是0,0。

Here's a smaller version of your grid. 这是网格的较小版本。

网格测试

And here's the runnable code that creates the grid. 这是创建网格的可运行代码。

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridTest implements Runnable {

    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("Grid Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        DrawingPanel drawingPanel = new DrawingPanel();
        mainPanel.add(drawingPanel);

        frame.add(mainPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GridTest());
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = -5711127036945010446L;

        private int width = 750; // 25 * 30
        private int height = 600; // 20 * 30

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(width + 2, height + 2));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int x = 1;
            int y = 1;
            int size = 30;

            for (int i = 0; i < 25; i++) {
                for (int j = 0; j < 20; j++) {
                    g.drawRect(x, y, size, size);
                    y += size;
                }
                x += size;
                y = 1;
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM