java 编一个程序按下按钮(button)实现换下一张照片我写的 帮改一下

java 编一个程序按下按钮(button)实现换下一张照片我写的 帮改一下,第1张

加了这些代码

private static final long serialVersionUID = 1L

Image imgs[]=new Image[5]

repaint()

改后代码如判乱下:

import java.awt.*

import java.awt.event.*

import java.applet.Applet

public class PlayAudio extends Applet implements ActionListener {

private static final long serialVersionUID = 1L

Image imgs[]=new Image[5]

Button b2

public void init() {

setLayout(new FlowLayout())// 使用布局管理器

imgs[0]=getImage(getCodeBase(),"1.jpg")

imgs[1]=getImage(getCodeBase(),"2.jpg")

imgs[2]=getImage(getCodeBase(),"3.jpg")

imgs[3]=getImage(getCodeBase(),"4.jpg")

imgs[4]=getImage(getCodeBase(),"5.jpg")

b2=new Button("下一张")// 定义Play按钮

b2.addActionListener(this)// 给Play按钮添加一个监听事件

add(b2)

}

private static int count = 0

public void actionPerformed(ActionEvent e)

{

//if(e.getSource() == b2)

//{

count=++count%5

repaint()

//}

}

public void paint(Graphics g)

{

g.drawImage(imgs[count],0,40,this)

}

}

==============================

g.drawImage("m"+count,0,40,this)

这个地方需灶伍要传进image类型的对象:m"+count是字符串隐冲或类型

可以新建个扮悔面板,在面板里厅丛正放入带图片的JLabel,填满面板即可。

JPanel jp = new JPanel()//新建面板

jp.setLayout(new FlowLayout()) //设置面郑晌板布局

ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"))

JLabel uppicture=new JLabel(ii)//往面板里加入JLabel

this.setVisible(true)

供你参考一下:

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.awt.image.BufferedImage

public class lianxi3 extends JFrame implements ActionListener {

    JPanel jPanel

    JLabel jLabel

    Image image

    ImageIcon imageIcon

    JButton jButton

    float www = 1

    float hhh = 1

    public lianxi3() {

        super("图片")

        jButton = new JButton("变大")

        jButton.addActionListener(this)

        image = Toolkit.getDefaultToolkit().getImage("src/img1.jpg")

        jLabel = new JLabel()

        jPanel = new JPanel()

        imageIcon = new ImageIcon(image)

        jLabel.setIcon(imageIcon)

   备锋唯     jPanel.add(jLabel)

        this.add(jLabel, BorderLayout.NORTH)

        this.add(jButton, BorderLayout.SOUTH)

        this.setVisible(true)

        this.setSize(600, 600)

    }

    public static void main(String[] args) {

        new lianxi3()

    }

    public void big() {

     基猛   int width = image.getHeight(jLabel)

        int height = image.getWidth(jLabel)

        double bigger = 1.5

        www = (float) (width * bigger)

        hhh = (float) (height * bigger)

        

        

        BufferedImage bufImg = new BufferedImage(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_4BYTE_ABGR)   

         Graphics g = bufImg .createGraphics()   

         g.drawImage(image, 0, 0, null)   

         g.dispose()     

     仿培    imageIcon.setImage(rotateImage(bufImg,30))

        //imageIcon.setImage(imageIcon.getImage().getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT))

        jLabel.setIcon(imageIcon)

        jLabel.setSize((int)www, (int)hhh)

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == jButton) {

            big()

            System.out.println("aaa")

        }

    }

    

public static BufferedImage rotateImage(final BufferedImage bufferedimage,

            final int degree) {

        int w = bufferedimage.getWidth()

        int h = bufferedimage.getHeight()

        int nw = (int) (bufferedimage.getWidth()*1.414)

        int nh = (int) (bufferedimage.getHeight()*1.414)

        int type = bufferedimage.getColorModel().getTransparency()

        BufferedImage img

        Graphics2D graphics2d

        (graphics2d = (img = new BufferedImage(nw, nh, type))

                .createGraphics()).setRenderingHint(

                RenderingHints.KEY_INTERPOLATION,

                RenderingHints.VALUE_INTERPOLATION_BILINEAR)

        graphics2d.rotate(Math.toRadians(degree), nw / 2, nh / 2)

        graphics2d.translate(nw/2-w/2, nh/2-h/2)

        graphics2d.drawImage(bufferedimage, 0, 0, null)

        graphics2d.dispose()

        return img

    }

    

}


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

原文地址: http://outofmemory.cn/yw/12507732.html

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

发表评论

登录后才能评论

评论列表(0条)

保存