Java 如何将重复背景图像设置为JPanel?
我想设置一个在整个JPanel中重复的图像,就像我们在CSS中将背景图像应用到DIV一样。如何在swing中为JPanel获得该功能?swing不提供现成的功能,因此您需要自己完成 整个过程比较简单,Java 如何将重复背景图像设置为JPanel?,java,swing,graphics,background,Java,Swing,Graphics,Background,我想设置一个在整个JPanel中重复的图像,就像我们在CSS中将背景图像应用到DIV一样。如何在swing中为JPanel获得该功能?swing不提供现成的功能,因此您需要自己完成 整个过程比较简单, for (y = 0 to containerHeight) do for (x = 0 to containerWidth) do drawImage(tile, x, y) 有趣的是知道在哪里以及如何应用它。看看: 有关各个部分的详细信息,您需要了解 范例 所以用这个
for (y = 0 to containerHeight) do
for (x = 0 to containerWidth) do
drawImage(tile, x, y)
有趣的是知道在哪里以及如何应用它。看看:
有关各个部分的详细信息,您需要了解
范例
所以用这个做瓷砖
我能够制作这个
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PaintTitle {
public static void main(String[] args) {
new PaintTitle();
}
public PaintTitle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage tile;
public TestPane() {
try {
tile = ImageIO.read(getClass().getResource("/tile.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int tileWidth = tile.getWidth();
int tileHeight = tile.getHeight();
for (int y = 0; y < getHeight(); y += tileHeight) {
for (int x = 0; x < getWidth(); x += tileWidth) {
g2d.drawImage(tile, x, y, this);
}
}
g2d.dispose();
}
}
}
Swing不提供这种现成的功能,所以您需要自己完成它 整个过程比较简单,
for (y = 0 to containerHeight) do
for (x = 0 to containerWidth) do
drawImage(tile, x, y)
有趣的是知道在哪里以及如何应用它。看看:
有关各个部分的详细信息,您需要了解
范例
所以用这个做瓷砖
我能够制作这个
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PaintTitle {
public static void main(String[] args) {
new PaintTitle();
}
public PaintTitle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage tile;
public TestPane() {
try {
tile = ImageIO.read(getClass().getResource("/tile.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int tileWidth = tile.getWidth();
int tileHeight = tile.getHeight();
for (int y = 0; y < getHeight(); y += tileHeight) {
for (int x = 0; x < getWidth(); x += tileWidth) {
g2d.drawImage(tile, x, y, this);
}
}
g2d.dispose();
}
}
}
你必须画它…@MadProgrammer你能给我举个例子吗你必须画它…@MadProgrammer你能给我举个例子吗