在jframe游戏中绘制图像

时间:2016-09-17 23:02:11

标签: java jframe

我在使用java绘制图像时遇到了一些麻烦。没有控制台错误,它打印'绘图'。我相信我正在加载错误的图像,但我找不到正确的方法。如果有人能告诉我正确的方法那么好。

      package platformer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.Image;

public class test2 extends JPanel implements KeyListener, ActionListener {

    int Width = 1000;
    int Height = 600;
    private static Image offScreenBuffer;// needed for double buffering graphics
    private Graphics offScreenGraphics;// needed for double buffering graphics
    private BufferedImage[] img = new BufferedImage[1];

    public test2() {

    }

    public void paint(Graphics g) {

        super.paint(g);
                g.drawImage(img[0],0,0,null);
    }

    public void timer1() {
        int delay = 30; // milliseconds
        new Timer(delay, this).start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent evt) {

    }

    public static void main(String args[]) throws InterruptedException, Exception {

        test2 test2 = new test2();
        JFrame frame = new JFrame("platformer");
        frame.setSize(test2.Width, test2.Height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.green);
        frame.add(test2);
        frame.setResizable(false);
        frame.addKeyListener(test2);
        test2.init();
        test2.timer1();
        // What to do after the program starts
        while (true) {
            test2.repaint();

        }
    }

    public void init() {


        try {
            URL url = new URL("platform.png");
            img[0] = ImageIO.read(url);
        } catch (IOException e) {
        }


    }

    public void keyTyped(KeyEvent e) {
    }
    public void keyPressed(KeyEvent e) {
    }
    public void keyReleased(KeyEvent e) {
    }



}

1 个答案:

答案 0 :(得分:0)

Swing有一个无限循环,可以为您执行事件处理。您不需要while(true)循环。配置完所有UI后,应调用frame.setVisible(true)。我的意思是它应该是main()中的最后一件事。此外,您应该在UI线程上使用

执行此操作
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        frame.setVisible(true);
    }
});

所以main()应该看起来像

public static void main(String args[]) throws InterruptedException, Exception {

        test2 test2 = new test2();
        JFrame frame = new JFrame("platformer");
        frame.setSize(test2.Width, test2.Height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.green);
        frame.add(test2);
        frame.setResizable(false);
        frame.addKeyListener(test2);
        test2.init();
        test2.timer1();
        SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        frame.setVisible(true);
    }
});
    }

但是,主要问题是init()中的try ... catch忽略了异常。你永远不应该留空。至少你应该打印一个堆栈跟踪。更好的是,在方法throws子句中添加适当的异常,如下所示:

    public void init() throws IOException {
            URL url = new URL("platform.png");
            img[0] = ImageIO.read(url);
    }

请注意,如果出现问题,您将立即了解它。