【问题标题】:How would I go about repeating an image on a JPanel in Java?我将如何在 Java 中的 JPanel 上重复图像?
【发布时间】:2013-09-18 01:46:17
【问题描述】:

我很想知道如何设置一个 32 x 32 像素的图像以在 JPanel 上重复一定数量的空间。然后,在该图像下方,我可能对 JPanel 的另一部分进行了类似的设置。

这个概念类似于旧的 Minecraft 启动器(1.5 和更低版本)。

附:我也不想使用任何外部 jar,因为这款游戏将在商业上公开。

【问题讨论】:

  • 复合for-next循环...
  • 我猜可以使用javax.swing.Timer来完成这项任务。一位相关的example :-)
  • 即使是商业产品,使用外部 jar 也没有错。当然,检查许可证。

标签: java image swing


【解决方案1】:

方式一:

  • 为大图创建一个 BufferedImage
  • 为要重复的小图像创建一个 BufferedImage。
  • 从大的 BufferedImage 中获取 Graphics 上下文,
  • 使用此 Graphics 对象通过 drawImage(...) 使用 for 循环重复绘制小图像。
  • 使用drawImage(...) 中的适当x 和y 参数转换此绘图的位置。
  • 释放图形上下文。
  • 在 JPanel 中或通过 ImageIcon 在 JLabel 中显示大的 BufferedImage。

方式二:

  • 创建一个小的 BufferedImage
  • 用它制作一个 ImageIcon。
  • 制作使用 GridLayout 的 JPanel。
  • 用显示 ImageIcon 的 JLabel 填充网格。

【讨论】:

    【解决方案2】:

    按图片搜索,关键字“我的世界启动器”,我猜:

    import java.awt.*;
    import java.awt.image.*;
    import java.io.IOException;
    import javax.imageio.*;
    import javax.swing.*;
    
    class TexturePaintTest {
      public JComponent makeUI() {
        BufferedImage bi = null;
        try {
          bi = ImageIO.read(getClass().getResource("dirt_32x32.jpg"));
        } catch(IOException ioe) {
          ioe.printStackTrace();
          throw new RuntimeException(ioe);
        }
        final TexturePaint texture = new TexturePaint(
            bi, new Rectangle(bi.getWidth(),bi.getHeight()));
        JPanel p = new JPanel(new BorderLayout()) {
          @Override public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setPaint(texture);
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.setPaint(new GradientPaint(
                0, 0, new Color(0f,0f,0f,.2f),
                0, getHeight(), new Color(0f,0f,0f,.8f), true));
            g2.fillRect(0, 0, getWidth(), getHeight());
            super.paintComponent(g);
          }
        };
        p.setOpaque(false);
    
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);
        panel.add(makeLoginPanel(), BorderLayout.EAST);
        p.add(panel, BorderLayout.SOUTH);
        return p;
      }
      private JPanel makeLoginPanel() {
        GridBagConstraints c = new GridBagConstraints();
        JPanel p = new JPanel(new GridBagLayout());
        p.setOpaque(false);
        c.gridheight = 1;
        c.gridwidth  = 1;
        c.gridy = 0;
        c.gridx = 0;
        c.weightx = 0.0;
        c.insets = new Insets(2, 2, 2, 0);
        c.anchor = GridBagConstraints.WEST;
        p.add(makeLabel("Username:"), c);
        c.gridx = 1;
        c.weightx = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JTextField(12), c);
        c.gridx = 2;
        c.weightx = 0.0;
        c.insets = new Insets(2, 2, 2, 2);
        c.anchor = GridBagConstraints.WEST;
        p.add(new JButton("Option"), c);
        c.gridy = 1;
        c.gridx = 0;
        c.weightx = 0.0;
        c.insets = new Insets(2, 2, 2, 0);
        c.anchor = GridBagConstraints.WEST;
        p.add(makeLabel("Password:"), c);
        c.gridx = 1;
        c.weightx = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JPasswordField(12), c);
        c.gridx = 2;
        c.weightx = 0.0;
        c.insets = new Insets(2, 2, 2, 2);
        c.anchor = GridBagConstraints.WEST;
        p.add(new JButton("Login"), c);
        return p;
      }
      private JLabel makeLabel(String str) {
        JLabel l = new JLabel(str);
        l.setForeground(Color.WHITE);
        return l;
      }
      public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            createAndShowGUI();
          }
        });
      }
      public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new TexturePaintTest().makeUI());
        f.setSize(320, 240);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
      }
    }
    

    【讨论】:

      【解决方案3】:

      对于游戏,我会远离 Swing UI 组件和布局,直接通过LWJGL"full-screen" 使用"Tile technique"Mappy 等编辑器进行绘图。

      【讨论】:

        猜你喜欢
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 2016-08-10
        • 2012-08-06
        • 1970-01-01
        • 2017-09-07
        相关资源
        最近更新 更多