1

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. Then, underneath that image, I might have a similiar setup for another bit of the JPanel.

The concept is similiar to that of the old Minecraft launcher (1.5 and less).

P.S. I'd also like to not use any external jars, as this game will be made commercially public.

3 Answers3

4

Way 1:

  • Create a BufferedImage for the large image
  • Create a BufferedImage for the small image to be repeated.
  • Get the Graphics context from the large BufferedImage,
  • Use this Graphics object to draw the small image repeatedly via drawImage(...), using a for loop.
  • Translate the location of this drawing by using the appropriate x and y parameters in drawImage(...).
  • Dispose of the Graphics context.
  • display the large BufferedImage in your JPanel or in a JLabel via an ImageIcon.

Way 2:

  • Create a small BufferedImage
  • Make an ImageIcon with it.
  • Make JPanel that uses a GridLayout.
  • Fill the Grid with JLabels that display the ImageIcon.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

Search by Image, keywords "Minecraft launcher", I would guess:

enter image description here

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);
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
2

For the games I would stay away from Swing UI components and layouts as such and go directly to drawing stuff either via LWJGL or "full-screen" using "Tile technique" with editors such as Mappy.

serg.nechaev
  • 1,323
  • 19
  • 26