java中用什么方法让图片居中

java中用什么方法让图片居中,第1张

Swing中通过Toolkit类获取屏幕分辨率,在根据屏幕分辨率和你的图片大小计算出你的图片的左上角的坐标。

核心代码:

Toolkit kit = Toolkit.getDefaultToolkit()

Dimension screenSize = kit.getScreenSize()

int screenWidth = screenSize.width

int screenHeight = screenSize.height

//background.java

import java.applet.AudioClip

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Container

import java.awt.Font

import java.awt.Graphics

import java.awt.Image

import java.awt.MediaTracker

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.MouseEvent

import java.awt.event.MouseListener

import java.util.Random

import javax.swing.JApplet

import javax.swing.JButton

import javax.swing.JLabel

import javax.swing.JPanel

public class background 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 background()

{

this(null, CENTRE)

}

/**

* 构造一个具有指定背景图片和指定显示模式的JImagePane

* @param image 背景图片

* @param modeName 背景图片显示模式

*/

public background(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 = 0ix <widthix += imageWidth)

{

for(int iy = 0iy <heightiy += imageHeight)

{

g.drawImage(backgroundImage, ix, iy, this)

}

}

break

}

//拉伸

case 2:

{

g.drawImage(backgroundImage, 0, 0, width, height, this)

break

}

}

}

}

}

//test.java

import java.applet.AudioClip

import java.awt.*

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.Random

import javax.swing.*

public class test extends JApplet implements Runnable{

Image img

JButton b1

Container hx

background dd,dd1,dd2

JLabel jl

JButton jb,jb1

JPanel jp,jp1

int imgWidth,imgHeight

AudioClip sound,sound1,sound2

String m ="arash.wav"

String[] image ={"AA.PNG","CC.PNG","qq.png","uu.png","MM.PNG","tt.png"}

int count=0,count1=0

Thread ShapeThread=null

Random RandomNumber=new Random( )

Color ImageColor

public void init(){

hx = getContentPane()

jp = new JPanel()

jp1 = new JPanel()

jl = new JLabel("Merry Christmas!")

jb1 = new JButton("变换背景图片")

jb1.setForeground(Color.blue)

jp.add(jb1)

jp.setOpaque(false)

jp1.add(jl)

jp1.setOpaque(false)

jl.setFont(new Font("隶书",Font.PLAIN,50))

img = getImage(getCodeBase(),image[0])

dd = new background(img,dd.CENTRE)

dd.add(jp1)

dd.add(jp)

dd.setOpaque(false)

hx.add(dd)

sound = getAudioClip(getCodeBase(),m)

MediaTracker tracker = new MediaTracker(this)

tracker.addImage( img, 0 )

try

{

tracker.waitForID( 0 )

}catch(Exception e){}

imgWidth = img.getWidth(this)

imgHeight = img.getHeight(this)

jb1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String buttonName = e.getActionCommand()

if (buttonName.equals("变换背景图片")){

count ++

img = getImage(getCodeBase(),image[count])

dd.setVisible(false)

dd = new background(img,dd.CENTRE)

dd.add(jp1)

dd.add(jp)

hx.add(dd)

if(count == 5)

count = -1

}

}

})

}

public void start(){

sound.loop()

if (ShapeThread==null)

{

ShapeThread=new Thread(this)

ShapeThread.start( )

}

}

public void stop(){

sound.stop()

}

public void run() {

// TODO Auto-generated method stub

while(true){

switch(RandomNumber.nextInt(3)) {

case 0:ImageColor=Color.greenbreak

case 1:ImageColor=Color.yellowbreak

case 2:ImageColor=Color.orangebreak

default: ImageColor=Color.white

}

try{

ShapeThread.sleep(100)

jl.setForeground(ImageColor)

}catch(InterruptedException e)

{

e.printStackTrace()

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8165482.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存