简体   繁体   English

Java:JFrame 图形未绘制矩形

[英]Java: JFrame Graphics not drawing rectangle

Hello fellow programmers,各位程序员好,

I've ran into a little issue in my code that I can't seem to crack.我在我的代码中遇到了一个我似乎无法破解的小问题。 It has to do with the Jframe;它与Jframe有关; Graphics area of Java. Java的图形区。 The code that I'll post below, is over a drawing method.我将在下面发布的代码是关于绘图方法的。 Which purpose is to draw the "rooms" that are in a ArrayList roomList which is located in another class hence lvl.其目的是绘制ArrayList roomList中的“房间”,该房间列表位于另一个 class 因此lvl. before.前。 This off-course doesn't happen, hence the post on here.这种偏离路线不会发生,因此在这里发帖。

public class LevelGUI implements Observer {

    private Level lv;
    private Display d;
    
    public LevelGUI(Level level, String name) {

        this.lv = level;
        
        JFrame frame = new JFrame(name);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d = new Display(lv, 500, 500);
        frame.getContentPane().add(d);
        frame.pack();
        frame.setLocation(0, 0);
        frame.setVisible(true);
    }
    private class Display extends JPanel {

        public Display(Level fp, int x, int y) {
            addKeyListener(new Listener());
            setBackground(Color.GRAY);
            setPreferredSize(new Dimension(x + 20, y + 20));
            setFocusable(true);
        }
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            draw(g);
        }
        private void draw(Graphics g) {
            Level lvl = new Level();
            for(int i = 0; i < lvl.roomList.size(); i++) {
                Room room = lvl.roomList.get(i);
                g.setColor(room.floorColor);
                g.drawRect(room.posX, room.posY, room.roomWidth, room.roomHeight);
            }   
        }
    }
}

To get some background info on the program .获取有关该程序的一些背景信息 roomList is the ArrayList , and it is filled with various different sized and colored rooms. roomListArrayList ,它充满了各种不同大小和颜色的房间。 The rooms themselves are objects .房间本身就是对象

Here comes first Level class :这是第一Level class

public class Level extends Observable {

    private boolean Switch = true;

    public ArrayList<Room> roomList = new ArrayList<Room>(); 

    (...)
}

Here is the Class Room() that is used to create the rooms.这是用于创建房间的Class Room()

public class Room {
    Color floorColor;
    int roomWidth;
    int roomHeight;
    int posX;
    int posY;

    public Room(int dx, int dy, Color color) {
        this.floorColor = color;
        this.roomHeight = dy;
        this.roomWidth = dx;
        this.posY = 0;
        this.posX = 0;
    }

    (...)
}

I've managed to locate where the problem is thought to occur, and it's the code in the for-loop .我已经设法找到问题发生的位置,它是for-loop中的代码。 I tried switching the roomList.size() for an integer to test if it was the loop., But it wasn't.我尝试将roomList.size()切换为integer以测试它是否是循环。但事实并非如此。 It is possible to draw a figure outside of the for-loop .可以在for-loop之外绘制图形。

and again, the problem isn't an error message, the program simply doesn't draw the rooms that I've instructed it to draw in the method draw() .再一次,问题不是错误消息,程序根本没有绘制我指示它在方法draw()中绘制的房间。

The display output looks like this:显示 output 如下所示: 在此处输入图像描述

Thanks beforehand!预先感谢!

Be aware that the paintComponent() method is invoked by Swing whenever the framework thinks the component needs to be rendered on screen.请注意,每当框架认为组件需要在屏幕上呈现时,Swing 就会调用 paintComponent() 方法。 This usually is when the window is getting visible - initially or because some other window no longer hides the component.这通常是当 window 变得可见时 - 最初或因为其他一些 window 不再隐藏组件。 Such events are out of your control.此类事件超出您的控制范围。

So your application should create a state and be ready to draw it anytime.所以你的应用程序应该创建一个 state 并准备好随时绘制它。 Therefore you do not create state (like a level) inside the paint() or paintComponent() method.因此,您不要在 paint() 或 paintComponent() 方法中创建 state(如关卡)。 Put that elsewhere - if need be into the constructor.把它放在其他地方——如果需要的话,放在构造函数中。

Looking at you code: As you are creating a new level inside paintComponent()/draw(), is it correct to assume that this level has no rooms associated?查看您的代码:当您在paintComponent()/draw() 中创建一个新关卡时,假设该关卡没有关联的房间是否正确? In that case the method is right to return without having painted anything.在这种情况下,该方法无需绘制任何内容即可返回。

If your application thinks the screen should be updated call repaint(), knowing that the paint() method will be called by the framework soon.如果您的应用程序认为应该更新屏幕,请调用 repaint(),因为框架很快就会调用 paint() 方法。

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

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