java拾取随机图像

java拾取随机图像,java,image,swing,random,paintcomponent,embedded-resource,Java,Image,Swing,Random,Paintcomponent,Embedded Resource,英语不是我的母语,抱歉有任何错误。我必须用Java做一个泡泡射击游戏。我想为气泡使用图像,我想随机选取图像。我使用了Random和ImageIcon类。我的程序在编译时没有显示任何内容,我也不知道问题出在哪里。我是Java的初学者 这是我的游戏类的代码: import java.awt.Graphics; import java.awt.Image; import java.util.Vector; import javax.swing.JPanel; public class Game e

英语不是我的母语,抱歉有任何错误。我必须用Java做一个泡泡射击游戏。我想为气泡使用图像,我想随机选取图像。我使用了
Random
ImageIcon
类。我的程序在编译时没有显示任何内容,我也不知道问题出在哪里。我是Java的初学者

这是我的游戏类的代码:

import java.awt.Graphics;
import java.awt.Image;
import java.util.Vector;

import javax.swing.JPanel;

public class Game extends JPanel{
  private static final long serialVersionUID = 1L;

  //what the balls are like
  public final static int START_BALLS=40;
  public static Vector<Ball> balls = new Vector<Ball>();
  private Image img;
  private Graphics graphics;

  public Game() {
    for(int i=0; i<START_BALLS; i++) {
      balls.add(new Ball());
    }
  }

  public void paint(Graphics g) {
    img = createImage(null);
    graphics = img.getGraphics();
    paintComponent(graphics);
    g.drawImage(img, 0, 0, null);
    repaint();
  }

  public void paintComponet(Graphics g) {
    for(int i=0; i<balls.size(); i++) {
      Ball b=(Ball)balls.get(i);
      b.draw(g);
    }
  }

  public static void main(String [] args) {
    new Frame();
    Game game = new Game();
    new Game();
    Window.window.add(game);
  }
}

我的程序出了什么问题?

查看我的注释,仔细查看代码中的注释,了解我是如何重新安排类的组织的

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;

public class Game extends JPanel {

    Random random = new Random();
    final String[] image_path = new String[]{
        "http://i.stack.imgur.com/gJmeJ.png",
        "http://i.stack.imgur.com/IHARa.png",
        "http://i.stack.imgur.com/wCF8S.png",
        "http://i.stack.imgur.com/T5uTa.png"
    };
    private static final long serialVersionUID = 1L;
    //what the balls are like
    public final static int START_BALLS = 40;
    public static Vector<Ball> balls = new Vector<Ball>();
    private Image img;
    // A Graphics instance is typically transient.
    // There is rarely, if ever, a need to store them
    //private Graphics graphics;

    public Game() {
        for (int i = 0; i < image_path.length; i++) {
            balls.add(new Ball(image_path[i]));
        }
        //I have no idea what you were trying to achieve here, but it fails horribly
        // img = createImage(null);
        img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        // graphics = img.getGraphics();
        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        };
        Timer timer = new Timer(400, al);
        timer.start();
    }

    @Override  // very handy!
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // g.drawImage(img, 0, 0, null);  A JPanel IS A ImageObserver
        g.drawImage(img, 0, 0, this);
        Ball b = (Ball) balls.get(random.nextInt(4));
        b.draw(g);
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Game");
                f.add(new Game());
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

class Ball {

    String randomBalls;
    public Image image;

    public Ball(String url) {
        try {
            image = ImageIO.read(new URL(url));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void draw(Graphics g) {
        g.drawImage(image, 0, 0, null, null);
    }
}

import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.util.Random;
导入java.util.Vector;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
导入javax.imageio.imageio;
导入java.net.URL;
公共类游戏扩展JPanel{
随机=新随机();
最终字符串[]图像路径=新字符串[]{
"http://i.stack.imgur.com/gJmeJ.png",
"http://i.stack.imgur.com/IHARa.png",
"http://i.stack.imgur.com/wCF8S.png",
"http://i.stack.imgur.com/T5uTa.png"
};
私有静态最终长serialVersionUID=1L;
//球是什么样子的
公共最终静态int开始球=40;
公共静态向量球=新向量();
私有图像img;
//图形实例通常是暂时的。
//几乎不需要存储它们
//私有图形;
公共游戏(){
对于(int i=0;i
提示
  • 线程“AWT-EventQueue-0”java.lang.UnsupportedOperationException中的异常:getGraphics()对于在img=createImage(null)处使用createImage(producer)创建的图像无效
    
  • JPanel
    中进行自定义绘制时,我们应该只覆盖
    paintComponent(Graphics)
    ,并保持
    paint(Graphics)
    方法不变。重写前者时,立即调用super方法
  • paint(Graphics)
    内部添加对
    repaint()
    的调用将导致无限循环。。如果代码需要循环,请建立一个Swing计时器来调用
    repaint()
  • paintComponet(图形g)
    应该是
    paintComponent(图形g)
    在适当的时候使用
    @Override
    符号。它会警告您方法名称拼写错误
更一般的提示
  • 为了更快地获得更好的帮助,请发布一个
  • 例如,获取图像的一种方法是热链接到中看到的图像
  • 到部署时,这些映像可能会成为一个新的应用程序。在这种情况下,必须通过
    URL
    而不是
    文件来访问它们。有关形成
    URL
    的方法,请参阅标签的

  • 如果你只是在没有随机性的情况下放置文件名,代码就可以工作了?因为这里没有什么明显的问题,主要的方法对我来说也有点奇怪。不应该像Frame=newframe()那样;帧。添加(游戏)?没有创建两个游戏类实例?有人能给我一个绘画方法的例子吗?它应该是什么样子?我知道这可能是一个愚蠢的问题,但我不知道怎么做。谢谢你的回答这些“答案”都是评论。请参见我的示例,其中显示了一个工作示例。
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.imageio.ImageIO;
    import java.net.URL;
    
    public class Game extends JPanel {
    
        Random random = new Random();
        final String[] image_path = new String[]{
            "http://i.stack.imgur.com/gJmeJ.png",
            "http://i.stack.imgur.com/IHARa.png",
            "http://i.stack.imgur.com/wCF8S.png",
            "http://i.stack.imgur.com/T5uTa.png"
        };
        private static final long serialVersionUID = 1L;
        //what the balls are like
        public final static int START_BALLS = 40;
        public static Vector<Ball> balls = new Vector<Ball>();
        private Image img;
        // A Graphics instance is typically transient.
        // There is rarely, if ever, a need to store them
        //private Graphics graphics;
    
        public Game() {
            for (int i = 0; i < image_path.length; i++) {
                balls.add(new Ball(image_path[i]));
            }
            //I have no idea what you were trying to achieve here, but it fails horribly
            // img = createImage(null);
            img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            // graphics = img.getGraphics();
            ActionListener al = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            };
            Timer timer = new Timer(400, al);
            timer.start();
        }
    
        @Override  // very handy!
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // g.drawImage(img, 0, 0, null);  A JPanel IS A ImageObserver
            g.drawImage(img, 0, 0, this);
            Ball b = (Ball) balls.get(random.nextInt(4));
            b.draw(g);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JFrame f = new JFrame("Game");
                    f.add(new Game());
                    // Ensures JVM closes after frame(s) closed and
                    // all non-daemon threads are finished
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // See https://stackoverflow.com/a/7143398/418556 for demo.
                    f.setLocationByPlatform(true);
    
                    // ensures the frame is the minimum size it needs to be
                    // in order display the components within it
                    f.pack();
                    // should be done last, to avoid flickering, moving,
                    // resizing artifacts.
                    f.setVisible(true);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }
    
    class Ball {
    
        String randomBalls;
        public Image image;
    
        public Ball(String url) {
            try {
                image = ImageIO.read(new URL(url));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void draw(Graphics g) {
            g.drawImage(image, 0, 0, null, null);
        }
    }