网格被绘制出屏幕

发布于 2024-12-11 06:07:10 字数 1454 浏览 0 评论 0原文

我在将网格绘制为距框架顶部 10 像素和距左侧 10 像素时遇到了一些麻烦。

我可以通过将 this.getY() + 10 增加到一个更高的数字来实现这一点,只是想知道为什么如果删除 + 10 它会被绘制出屏幕。

忽略变量名称和任何格式,我只是将它们放在一起

package griddrawing;

import java.awt.*;
import javax.swing.*;

public class Grid extends JFrame
{

    private int TILEWIDTH;
    private int TILEHEIGHT;
    private int COLS;
    private int ROWS;
    private int defaultX;
    private int defaultY;
    private int currentX;
    private int currentY;

    public Grid()
    {
        setSize(800,400);
        TILEWIDTH = 30;
        TILEHEIGHT = 30;
        COLS = 10;
        ROWS = 10;
        defaultX = this.getX() + 10;
        defaultY = this.getY() + 10;
        currentX = 0;
        currentY = 0;
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        currentX = defaultX;
        currentY = defaultY;

        g.setColor(Color.black);

        for(int i = 0; i < COLS; i++)
        {
            for(int k = 0 ; k < ROWS; k++)
            {
                g.drawRect(currentX - (TILEWIDTH / 2), currentY - (TILEHEIGHT / 2), TILEWIDTH, TILEHEIGHT);
                g.drawString("" + k, currentX, currentY);
                currentY += TILEWIDTH;
                System.out.println("COL: " + i + " ROW: " + k + " Current X: " + currentX + " Current Y: " + currentY);
            }
            currentY = defaultY;
            currentX += TILEHEIGHT;
        }
    }
}

I am having a bit of trouble making this grid be drawn 10 pixels from the top and 10 pixels from the left of the Frame.

I can make it do it by increasing this.getY() + 10 to a higher number, just wondering why if is remove the + 10 it getting drawn off screen.

Ignore the variable names and any formatting I just threw this together

package griddrawing;

import java.awt.*;
import javax.swing.*;

public class Grid extends JFrame
{

    private int TILEWIDTH;
    private int TILEHEIGHT;
    private int COLS;
    private int ROWS;
    private int defaultX;
    private int defaultY;
    private int currentX;
    private int currentY;

    public Grid()
    {
        setSize(800,400);
        TILEWIDTH = 30;
        TILEHEIGHT = 30;
        COLS = 10;
        ROWS = 10;
        defaultX = this.getX() + 10;
        defaultY = this.getY() + 10;
        currentX = 0;
        currentY = 0;
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        currentX = defaultX;
        currentY = defaultY;

        g.setColor(Color.black);

        for(int i = 0; i < COLS; i++)
        {
            for(int k = 0 ; k < ROWS; k++)
            {
                g.drawRect(currentX - (TILEWIDTH / 2), currentY - (TILEHEIGHT / 2), TILEWIDTH, TILEHEIGHT);
                g.drawString("" + k, currentX, currentY);
                currentY += TILEWIDTH;
                System.out.println("COL: " + i + " ROW: " + k + " Current X: " + currentX + " Current Y: " + currentY);
            }
            currentY = defaultY;
            currentX += TILEHEIGHT;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-12-18 06:07:10
  1. 不要设置框架的大小。
  2. 也不要直接在框架上绘画。

而不是两者都使用:

  1. 重写 JComponentJPanelpaintComponent(Graphics) 方法。
  2. 调用 theComponent.setPreferredSize(Dimension) 或重写相同的方法。
  3. 将自定义组件添加到框架中并调用 pack()

这意味着您不再需要考虑任何偏移量(这可能会因平台或 PLAF 而改变)。

  1. Don't set the size of the frame.
  2. Don't paint directly to the frame either.

Instead of both:

  1. Override the paintComponent(Graphics) method of a JComponent or JPanel.
  2. Either call theComponent.setPreferredSize(Dimension) or override that same method.
  3. Add the custom component to the frame and call pack().

That lot should mean you no longer need to account for any offset (which might change by platform or PLAF).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文