将一些图片添加到 BuffredImage 然后保存

Adding some pictures to a BuffredImage then Save it

我的总体想法是制作一个 Sprite-Sheet-Maker 我的意思是程序将:

  1. Collect more than one picture like "1.bmp" , "2.png" and "3.jpg"
  2. Create new BuffredImage variable and draw on it the 3 pictures (and draw the BuffredImage on a JPanel on the same time)
  3. Save the final picture "Final.png"

我想在一个循环中完成第一步和第二步,因为我有一个 JList 包含所有图片的路径。

为了做到这一点,我在 eclipse 上使用了 Java window Builder,我制作了表格并尝试在 Panel.

上测试一个简单的代码
Panel panel = new Panel(); //Work
panel.setBackground(Color.BLUE); //Work
BufferedImage img = new BufferedImage(5,5,5); //Work
Graphics g = null ; //Work
panel.paintComponents(g); //work
g.setColor(Color.BLACK); //ERROR---------------------ERROR
g.fillRect(0, 0, 50, 50);

问题不仅存在于该代码中,还存在于所有想法中,所以请您的任何想法都可以帮助我什至是我项目的一部分解决方案,所以请发表您的想法。

 g.setColor(Color.BLACK);//error

  Graphics g = null ;//null value, you are not create any obeject

根据 null 值我们无法执行任何操作。

您必须覆盖 JPanel 中的 paintComponent 方法 class 然后您将收到 Graphics 对象。

      JPanel panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.fillRect(0, 0, 100, 100);
            }
        };
        frame.add(panel);//added to frame.

See this link

  1. 使用 GridLayout 创建 JPanel。
  2. 创建 3 个 JLabel,每个包含每个图像的图标。然后将每个标签添加到面板。
  3. 要创建任何 Swing 组件的图像,您可以使用 Screen Image class。