简体   繁体   English

Java JPanel平铺背景图像

[英]Java JPanel tiled background image

I currently have the code for creating a JOptionPane that tiles an image to the background no matter the size I set it to :) 我目前有创建JOptionPane的代码,无论我将其设置为:),它都会将图像平铺到背景中

package test;
import java.awt.*;  
import java.awt.image.BufferedImage;  
import java.io.*;  
import javax.imageio.ImageIO;  
import javax.swing.*;  

public class TiledImage extends JPanel {  
    BufferedImage tileImage;  


    public TiledImage(BufferedImage image) {  
        tileImage = image;  
    }  

    protected void paintComponent(Graphics g) {  
        int width = getWidth();  
        int height = getHeight();  
        for (int x = 0; x < width; x += tileImage.getWidth()) {  
            for (int y = 0; y < height; y += tileImage.getHeight()) {  
                g.drawImage(tileImage, x, y, this);  
            }  
        }  
    }  

    public Dimension getPreferredSize() {  
        return new Dimension(240, 240);  
    }  

    public static void main(String[] args) throws IOException {  
        BufferedImage image = ImageIO.read(new File("./resource/patterngrey.png"));  
        TiledImage test = new TiledImage(image);  
        JOptionPane.showMessageDialog(null, test, "", JOptionPane.PLAIN_MESSAGE);  
    }  
} 

the problem I am having is using the same code to add an image to a JPanel background in a JFrame here is what I have: 我遇到的问题是使用相同的代码将图像添加到JFrame中的JPanel背景,这就是我所拥有的:

package test;
import java.awt.*;  
import java.awt.image.BufferedImage;  
import java.io.*;  
import javax.imageio.ImageIO;  
import javax.swing.*;  

public class TiledImage extends JPanel {  
    BufferedImage tileImage;  
    static JFrame mainFrame = new JFrame("Program Name");
    static JPanel userDetailsPanel = new JPanel();

    public TiledImage(BufferedImage image) {  
        tileImage = image;  
    }  

    protected void paintComponent(Graphics g) {  
        int width = getWidth();  
        int height = getHeight();  
        for (int x = 0; x < width; x += tileImage.getWidth()) {  
            for (int y = 0; y < height; y += tileImage.getHeight()) {  
                g.drawImage(tileImage, x, y, this);  
            }  
        }  
    }  


    public static void main(String[] args) throws IOException {  
        mainFrame.setSize(400,400);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(userDetailsPanel, BorderLayout.CENTER);
        BufferedImage image = ImageIO.read(new File("./resource/patterngrey.png"));  
        TiledImage backgroundImage = new TiledImage(image);  
   //   userDetailsPanel.setComponent(backgroundImage); //i know this line is wrong 
   //but i dont know how to correct it
        mainFrame.setVisible(true);
    }  
} 

Any and all help is appreciated if there is a better way of doing it that is a lot less code that would also be great. 如果有更好的方法可以做到这一点,那么任何和所有的帮助都会受到赞赏。 I do need to add labels and buttons on top of the background once i have my background sorted. 我需要在背景排序后在背景上添加标签和按钮。

The background needs to be tiled as the application will have a couple of different JPanels in the JFrame with different pattern backgrounds and i would like to make the frame resizable 背景需要平铺,因为应用程序将在JFrame中具有几个不同的JPanel,具有不同的图案背景,我想使框架可调整大小

An instance of java.awt.TexturePaint provides a convenient way to tile a BufferedImage . java.awt.TexturePaint一个实例提供了一种平铺BufferedImage的便捷方法。 Related examples may be seen here . 这里可以看到相关的例子。 Given a TexturePaint , you can fill a component's background fairly easily, as shown here . 给定一个TexturePaint ,您可以填写一个组件的背景很容易,如图所示这里

    private TexturePaint paint;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(paint);
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }

图片

You state: 你说:

Any and all help is appreciated if there is a better way of doing it that is a lot less code that would also be great. 如果有更好的方法可以做到这一点,那么任何和所有的帮助都会受到赞赏。

That's not a lot of code actually. 实际上并不是很多代码。 The only thing else I could suggest is that if the JPanel is not going to vary in size, create a background BufferedImage, draw your tiled images in that, and then draw the one background image in either your JPanel's paintComponent method, or in a JLabel's icon. 我唯一可以建议的是,如果JPanel的大小不变,请创建一个背景BufferedImage,在其中绘制平铺图像,然后在JPanel的paintComponent方法或JLabel中绘制一个背景图像。图标。 If you go the latter route, then give the JLabel a layout manager so that it can act as a well-behaved container for your components. 如果你选择后一种方法,那么给JLabel一个布局管理器,这样它就可以作为组件的良好容器。 And make sure that anything on top of your tiled containers is not opaque if the image needs to show through, especially JPanels. 如果图像需要显示,请确保平铺容器顶部的任何内容都不是不透明的,尤其是JPanels。

To correct the issue you're currently having, you can set your TiledImage object as the content pane of your JFrame, and then ensure any panels that get added onto it are not opaque. 要更正您当前遇到的问题,可以将TiledImage对象设置为JFrame的内容窗格,然后确保添加到其上的任何面板都不是不透明的。

That is, 那是,

public static void main(String[] args) throws IOException {  
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    BufferedImage image = ImageIO.read(new File("./resource/patterngrey.png"));  
    TiledImage backgroundImage = new TiledImage(image);  
    // Make backgroundImage the content pane.
    mainFrame.setContentPane(backgroundImage);
    mainFrame.setLayout(new BorderLayout());
    // Make the userDetailsPanel not opaque.
    userDetailsPanel.setOpaque(false);
    mainFrame.add(userDetailsPanel, BorderLayout.CENTER);

    mainFrame.setSize(400,400);
    mainFrame.setVisible(true);
}  

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

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