[英]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我也不想使用任何外部的罐子,因为该游戏将在商业上公开发行。
drawImage(...)
, using a for loop. 使用此Graphics对象,使用for循环通过drawImage(...)
重复绘制小图像。 drawImage(...)
. 通过在drawImage(...)
使用适当的x和y参数来转换此图形的位置。 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.