我正在尝试在Java中绘制正方形,但无法正常工作。

huangapple go评论70阅读模式
英文:

I'm trying to draw square in java but it doesn't work

问题

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

public class SnakeGame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(300, 300);
        frame.setLayout(null);
        Grids grids = new Grids(300);
        GUI gui = new GUI(grids.nodesList, grids.nodeSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(gui);
        frame.setVisible(true);
    }

    public static class Node {
        public float[] position = new float[2];

        public Node(float[] Position) {
            position[0] = Position[0];
            position[1] = Position[1];
        }
    }

    public static class Grids {
        public int nodesNum = 6;
        public float screenSize;
        public float nodeSize;
        public float[] startNodePos = new float[2];
        public List<Node> nodesList = new ArrayList<Node>();

        public Grids(float ScreenSize) {
            screenSize = ScreenSize;
            nodeSize = screenSize / nodesNum;
            startNodePos[0] = nodeSize / 2;
            startNodePos[1] = nodeSize / 2;
            for (float X = startNodePos[0]; X <= nodeSize * 5; X += nodeSize / 2) {
                for (float Y = startNodePos[1]; Y <= nodeSize * 5; Y += nodeSize / 2) {
                    float[] Pos = {X, Y};
                    Node node = new Node(Pos);
                    nodesList.add(node);
                }
            }
        }
    }

    public static class GUI extends JPanel {
        public List<Node> nodes;
        public float NodeSize;

        public GUI(List<Node> Nodes, float nodeSize) {
            nodes = Nodes;
            NodeSize = nodeSize;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Node node : nodes) {
                g.setColor(Color.BLACK);
                g.drawRect((int) node.position[0], (int) node.position[1], (int) NodeSize, (int) NodeSize);
            }
        }
    }
}
英文:

I'm trying to draw squares to make grid system to make snake game in java but when i run my code i can't see any thing.
I made node class that has the square info like position, and grid class that has the data of all the squares and GUI class and i tried to use drawRect method but i have no result.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.*;
import java.util.List;
public class SnakeGame {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300,300);
frame.setLayout(null);
Grids grids  = new Grids(300);
GUI gui = new GUI(grids.nodesList,grids.nodeSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gui);
frame.setVisible(true);
}
public static class Node{
public float[] position = new float[2];
public Node(float[] Position){
//this is the x position
position[0] = Position[0];
//this is the y position
position[1] = Position[1];
}
}
public static class Grids{
public int nodesNum =  6;
public float screenSize;
public float nodeSize;
public float[] startNodePos = new float[2];
public List&lt;Node&gt; nodesList = new ArrayList&lt;Node&gt;();
public Grids(float ScreenSize){
screenSize = ScreenSize;
nodeSize = screenSize / nodesNum;
//set the start node position
//x
startNodePos[0] = nodeSize /2;
//y
startNodePos[1] = nodeSize /2;
//use for loop to create nodes
for (float X = startNodePos[0] ; X &lt;= nodeSize * 5; X += nodeSize / 2) {
for (float Y = startNodePos[1]; Y &lt;= nodeSize * 5; Y += nodeSize / 2) {
float[] Pos = {X, Y};
Node node = new Node(Pos);
nodesList.add(node);
}
}
}
}
public static class GUI extends JPanel{
public List&lt;Node&gt; nodes;
public float NodeSize;
public GUI(List&lt;Node&gt; Nodes,float nodeSize){
nodes = Nodes;
NodeSize = nodeSize;
}
@Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
for(Node node : nodes){
g.setColor(Color.BLACK);
g.drawRect((int)node.position[0],(int)node.position[1],(int)NodeSize,(int)NodeSize);
}
}
}
}

答案1

得分: 3

有两个主要的错误

第一个

frame.setLayout(null);

这意味着你将完全负责确定所有子组件的位置和大小。

在这种情况下,可能最好的办法就是摆脱它,使用默认的BorderLayout

第二个

你重写了paintComponents,而不是paintComponent(注意末尾没有s

public void paintComponents(Graphics g) {
//...
}

由于没有组件需要绘制,所以它不会被调用。

简单地将其更改为paintComponent(并确保正确调用它的超类)

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Node node : nodes) {
g.setColor(Color.BLACK);
g.drawRect((int) node.position[0], (int) node.position[1], (int) NodeSize, (int) NodeSize);
}
}

观察

在运行代码时,我注意到一些有趣的事情,这可能会在长期内给你带来问题。

  1. 视图区域的大小与窗口的大小不同。 窗口还需要显示窗口装饰(标题栏、边框等),这些装饰嵌入到窗口中。 这减少了可用的可视区域。 最好的方法是使内容提供一个大小提示,然后围绕窗口进行调整,这让我想到了...
  2. 该模型似乎考虑了视图属性。 你应该尽量使模型和视图尽可能地不受影响,这意味着模型不应该将显示状态规定给视图。 相反,单元格的大小,以及由此延伸,面板的大小,应该由视图确定。
英文:

There are two primary mistakes

#First

frame.setLayout(null);

This means that you will become completely responsible for determine the location and size of all child components.

In this case, it's probably just easier to get rid of it and use the default BorderLayout

#Second

You're overriding paintComponents, not paintComponent (not the s at the end)

public void paintComponents(Graphics g) {
//...
}

Since, there are no components to be painted, it's not getting called

Simple change it to paintComponent (and make sure you call it's super properly)

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Node node : nodes) {
g.setColor(Color.BLACK);
g.drawRect((int) node.position[0], (int) node.position[1], (int) NodeSize, (int) NodeSize);
}
}

#Observations

While running through the code, I note some ... interesting things, which might cause you issues in the long run.

  1. The size of the view area is not the same as the size of the window. A window also needs to display the window decorations (title bar, borders, etc), which are inset into the window. This reduces the available viewable area. It's better to have the content provide a sizing hint and pack the window around, which brings me to...
  2. The model seems to be taking into consideration view properties. You should aim to have the model and view be as agnostic as possible, meaning that the model should not be dictating display state to the view. Instead, the size of the cell's, and by extensions, the size of the panel, should be determined by the view.

huangapple
  • 本文由 发表于 2020年3月16日 05:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/60697663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定