javax.swing解决背景图片随屏幕大小变化的问题

5 篇文章 0 订阅
2 篇文章 0 订阅

转载https://bbs.csdn.net/wap/topics/300154741来自四楼NB300的回复
(见main主函数)找一个jpg图片命名为:System.getProperty(“user.dir”)+"/pic/yjx/javaGame/test/mota/openFrame.png"
和这个类放在一起,直接运行就看到效果了,具体的实现里面注释写的很周到。

package com.yjx.javaGame.test.mota;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 解决背景图片随屏幕大小变化的问题
 * 找一个图片命名为System.getProperty("user.dir")+"/pic/yjx/javaGame/test/mota/openFrame.png"
 * 和这个类放在一起,直接运行就看到效果了,具体的实现里面注释写的很周到。
 * @author xiaole
 *
 */
public class JImagePane extends JPanel {
	private static final long serialVersionUID = -8251916094895167058L;

	/**
	 * 
	 * 居中
	 */
	public static final String CENTRE = "Centre";

	/**
	 * 
	 * 平铺
	 */
	public static final String TILED = "Tiled";

	/**
	 * 
	 * 拉伸
	 */
	public static final String SCALED = "Scaled";

	/**
	 * 
	 * 背景图片
	 */
	private Image backgroundImage;

	/**
	 * 
	 * 背景图片显示模式
	 */
	private String imageDisplayMode;

	/**
	 * 
	 * 背景图片显示模式索引(引入此属性有助于必要时扩展)
	 */
	private int modeIndex;

	/**
	 * 
	 * 构造一个没有背景图片的JImagePane
	 */
	public JImagePane(){
		this(null, CENTRE);
	}

	/**
	 * 
	 * 构造一个具有指定背景图片和指定显示模式的JImagePane
	 * 
	 * @param image
	 *            背景图片
	 * 
	 * @param modeName
	 *            背景图片显示模式
	 */
	public JImagePane(Image image, String modeName){
		super();
		setBackgroundImage(image);
		setImageDisplayMode(modeName);
	}

	/**
	 * 
	 * 设置背景图片
	 * 
	 * @param image
	 *            背景图片
	 */
	public void setBackgroundImage(Image image){
		this.backgroundImage = image;
		this.repaint();
	}

	/**
	 * 
	 * 获取背景图片
	 * 
	 * @return 背景图片
	 */
	public Image getBackgroundImage(){
		return backgroundImage;
	}

	/**
	 * 
	 * 设置背景图片显示模式
	 * 
	 * @param modeName
	 *            模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
	 */

	public void setImageDisplayMode(String modeName){
		if (modeName != null){
			modeName = modeName.trim();
			// 居中
			if (modeName.equalsIgnoreCase(CENTRE)){
				this.imageDisplayMode = CENTRE;
				modeIndex = 0;
			}

			// 平铺
			else if (modeName.equalsIgnoreCase(TILED)){
				this.imageDisplayMode = TILED;
				modeIndex = 1;
			}

			// 拉伸
			else if (modeName.equalsIgnoreCase(SCALED)){
				this.imageDisplayMode = SCALED;
				modeIndex = 2;
			}
			this.repaint();
		}
	}

	/**
	 * 
	 * 获取背景图片显示模式
	 * 
	 * @return 显示模式
	 */

	public String getImageDisplayMode(){
		return imageDisplayMode;
	}

	/**
	 * 
	 * 绘制组件
	 * 
	 * @see javax.swing.JComponent#paintComponent(Graphics)
	 */

	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		// 如果设置了背景图片则显示
		if (backgroundImage != null){
			int width = this.getWidth();
			int height = this.getHeight();
			int imageWidth = backgroundImage.getWidth(this);
			int imageHeight = backgroundImage.getHeight(this);
			switch (modeIndex){

			// 居中
			case 0:
				int x = (width - imageWidth) / 2;
				int y = (height - imageHeight) / 2;
				g.drawImage(backgroundImage, x, y, this);
				break;

			// 平铺
			case 1:
				for (int ix = 0; ix < width; ix += imageWidth)
					for (int iy = 0; iy < height; iy += imageHeight)
						g.drawImage(backgroundImage, ix, iy, this);
				break;

			// 拉伸
			case 2:
				g.drawImage(backgroundImage, 0, 0, width, height, this);
				break;
			}
		}
	}

	public static void main(String[] args) throws IOException{
		JFrame frame = new JFrame("JImagePane Test");
		Image iamge = new ImageIcon(ImageIO.read(new File(System.getProperty("user.dir")+"/pic/yjx/javaGame/test/mota/openFrame.png"))).getImage();
		JImagePane imagePane = new JImagePane(iamge, JImagePane.SCALED);
		frame.getContentPane().add(imagePane, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(680, 480);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值