无法在java中绘制形状

时间:2015-08-10 17:49:27

标签: java swing animation graphics jframe

我很确定这段代码应该在文字一词的屏幕上画一个椭圆形。但是,这个词全部出现,屏幕的其余部分是黑色的。这似乎发生在任何原始形状。我想我认为java相当不错,但图形化的东西对我来说真的很混乱。我很有智慧,任何帮助都会受到赞赏。

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.ArrayList;

public class Game extends JPanel implements Runnable {
    int W = 4;
    int H = 3;
    int windowSize = 300;
    boolean running;
    static boolean drawHitBoxes = true;
    int FPSLimit = 30;

    private Thread thread;
    private BufferedImage buffer;
    private Graphics2D g;

    public Game() {
        super();
        setPreferredSize(new Dimension(W * windowSize, H * windowSize));
        setFocusable(true);
        requestFocus();
    }

    public void addNotify() {
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        running = true;

        buffer = new BufferedImage(W * windowSize, H * windowSize,
                BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) buffer.getGraphics();

        // citList.add(new Citizen(200, 200, "Joe"));

        long startTime;
        long waitTime;

        long frameTime = 1000 / FPSLimit; // /How long one frame should take
        long currentFrameTime;

        while (running) {
            startTime = System.nanoTime(); // record when loop starts

            gameUpdate();
            gameRender();
            gameDraw();

            // Calculate how long the current frame took
            currentFrameTime = (System.nanoTime() - startTime) / 1000000;
            waitTime = frameTime - currentFrameTime;

            try {
                Thread.sleep(waitTime);
            } catch (Exception e) {
            } // Sleep for the remaining time
        }
    }

    private void gameUpdate() {
        // for(Citizen i:citList){i.update();} //Update citizens
    }

    private void gameRender() {
        g.setColor(Color.WHITE);
        g.drawOval(100, 100, W - 100, H - 100);
        g.setColor(Color.WHITE);
        g.drawString("Text.", 100, 100);
        System.out.println("Drawing white box.");

        // for(Citizen i:citList){i.draw(g);} //Draw citizens
    }

    private void gameDraw() {
        Graphics gMain = this.getGraphics();
        gMain.drawImage(buffer, 0, 0, null);
    }
}

2 个答案:

答案 0 :(得分:3)

g.drawOval(100, 100, W-100, H-100);

W为4,H为3,因此W-100为-96且H-100为-97,使得第3和第4个参数为负,这对{{{0}没有意义1}}方法,因为椭圆的宽度和高度怎么能是负的。解决方案:确保在调用此方法时仅使用有意义的正参数。可能你想要的是:

Graphics#drawOval(...)

顺便说一下,我自己,我更喜欢使用被动图形,绘制paintComponent并且每当我看到具有Graphics或Graphics2D实例字段的Swing代码时我都很害怕..而且你的代码看起来不遵守Swing线程规则,因为它似乎从Swing事件线程中进行Swing调用。

答案 1 :(得分:3)

我认为最好创建paintComponent方法并在那里传输gameRender和gameDraw,并在while循环中用repaint()替换它们的方法调用。这是有效的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.ArrayList;

public class Game extends JPanel implements Runnable {
    int W = 4;
    int H = 3;
    int windowSize = 300;
    boolean running;
    static boolean drawHitBoxes = true;
    int FPSLimit = 30;

    private Thread thread;
    private BufferedImage buffer;
    private Graphics2D g;

    public Game() {
        super();
        setPreferredSize(new Dimension(W * windowSize, H * windowSize));
        setFocusable(true);
        requestFocus();
    }

    public void addNotify() {
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        running = true;

        buffer = new BufferedImage(W * windowSize, H * windowSize,
                BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) buffer.getGraphics();

        // citList.add(new Citizen(200, 200, "Joe"));

        long startTime;
        long waitTime;

        long frameTime = 1000 / FPSLimit; // /How long one frame should take
        long currentFrameTime;

        while (running) {
            startTime = System.nanoTime(); // record when loop starts

            gameUpdate();
            //gameRender();
            //gameDraw();
            repaint();

            // Calculate how long the current frame took
            currentFrameTime = (System.nanoTime() - startTime) / 1000000;
            waitTime = frameTime - currentFrameTime;

            try {
                Thread.sleep(waitTime);
            } catch (Exception e) {
            } // Sleep for the remaining time
        }
    }

    private void gameUpdate() {
        // for(Citizen i:citList){i.update();} //Update citizens
    }

    private void gameRender() {
        g.setColor(Color.WHITE);
        //g.drawOval(100, 100, W - 100, H - 100);
        g.drawOval(100, 100, 100, 100);
        g.setColor(Color.WHITE);
        g.drawString("Text.", 100, 100);
        //System.out.println("Drawing white box.");

        // for(Citizen i:citList){i.draw(g);} //Draw citizens
    }

    private void gameDraw(Graphics gMain) {
        //Graphics gMain = this.getGraphics();
        gMain.drawImage(buffer, 0, 0, null);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        gameRender();
        gameDraw(g);
    }
}

正如@Hovercraft在他的回答中指出的那样,W-100和H-100代码为“g.drawOval(100,100,W-100,H-100);”会产生负数。我不确切地知道你想在那里得到什么值,但我只是用100替换它们只是为了消除这个bug。