简体   繁体   English

我将如何在Java的JPanel上重复图像?

[英]How would I go about repeating an image on a JPanel in Java?

I am curious to know how I would set up a 32 x 32 pixel image to repeat across a JPanel for a certain amount of spaces. 我很好奇我如何设置32 x 32像素的图像,以便在JPanel上重复一定的空间。 Then, underneath that image, I might have a similiar setup for another bit of the JPanel. 然后,在该图像的下面,我可能对JPanel的另一部分进行了类似的设置。

The concept is similiar to that of the old Minecraft launcher (1.5 and less). 这个概念与旧的Minecraft发射器(1.5及以下)类似。

PS I'd also like to not use any external jars, as this game will be made commercially public. PS我也不想使用任何外部的罐子,因为该游戏将在商业上公开发行。

Way 1: 方法1:

  • Create a BufferedImage for the large image 为大图像创建BufferedImage
  • Create a BufferedImage for the small image to be repeated. 为要重复的小图像创建BufferedImage。
  • Get the Graphics context from the large BufferedImage, 从较大的BufferedImage获取Graphics上下文,
  • Use this Graphics object to draw the small image repeatedly via drawImage(...) , using a for loop. 使用此Graphics对象,使用for循环通过drawImage(...)重复绘制小图像。
  • Translate the location of this drawing by using the appropriate x and y parameters in drawImage(...) . 通过在drawImage(...)使用适当的x和y参数来转换此图形的位置。
  • Dispose of the Graphics context. 处置Graphics上下文。
  • display the large BufferedImage in your JPanel or in a JLabel via an ImageIcon. 通过ImageIcon在您的JPanel或JLabel中显示较大的BufferedImage。

Way 2: 方式2:

  • Create a small BufferedImage 创建一个小的BufferedImage
  • Make an ImageIcon with it. 用它制作一个ImageIcon。
  • Make JPanel that uses a GridLayout. 制作使用GridLayout的JPanel。
  • Fill the Grid with JLabels that display the ImageIcon. 用显示ImageIcon的JLabel填充网格。

Search by Image, keywords "Minecraft launcher", I would guess: 通过图像搜索,关键字“ Minecraft launcher”,我猜:

在此处输入图片说明

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);
  }
}

对于游戏,我不会使用Swing UI组件和布局,而是直接通过LWJGL或使用“平铺技术” 全屏”通过Mappy等编辑器直接绘制东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM