如何设置重复的背景图像到JPanel? [英] How to set repeating background image to a JPanel?

查看:116
本文介绍了如何设置重复的背景图像到JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个图像,在整个JPanel的整个宽度上重复,就像我们将背景图像应用于CSS中的DIV一样。如何获得JPanel的swing?

解决方案

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){
PaintTitle();

$ b public PaintTitle(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}

JFrame框架= 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;
$ b 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();
}
}

}


I want to set an image which repeats throughout the full width of the JPanel like we apply Background image to a DIV in CSS. How do I obtain that in swing for a JPanel?

解决方案

Swing doesn't provide this functionality out of the box, so you will need to do it yourself...

The overall process is relative simple,

for (y = 0 to containerHeight) do
    for (x = 0 to containerWidth) do
        drawImage(tile, x, y)

The fun part is knowing where and how to apply it. Take a look at:

For details about the various parts you will need to know.

Example

So using this as the tile...

I was able to produce this...

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

}

这篇关于如何设置重复的背景图像到JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆