java幻灯片效果_java中怎么实现幻灯片切换特效?详细实例展示

在java的使用过程中,我们会发现它的功能真的很强大,完成内容也极其丰富。今天就给大家介绍一下如何在java中实现幻灯片切换特效,以及通过实际的代码来为大家展示。

在最开始,需要说明的是,这个代码是实现淡入淡出、缓慢覆盖、旋转覆盖等10多种幻灯片的变换效果的。

它的功能实现主要包括以下方面:一、图片加载类ImageLoader实现:

⑴、用阻塞队列存储图片:BlockingQueue images=new ArrayBlockingQueue<>(2);

⑵、用图片eof表示图片队列结束:Image eof=new WritableImage(1, 1);

⑶、循环读取指定图片,由于是阻塞队列,所以当队列满的时候线程会自动阻塞。

具体代码如下:public void run()

{

int id = 0;

try

{

while (true)

{

String path = resources[id];

InputStream is = getClass()

.getResourceAsStream(path);

if (is != null)

{

Image image = new Image(is, width, height, true, true);

if (!image.isError())

{

images.put(image);

}

}

id++;

if (id >

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import java.io.*; public class SR_Slider extends Frame implements ActionListener, WindowListener, Runnable { public static void main(String Pagli[]) { new SR_Slider(); } Thread time; //new thread File fl; URL dir; MediaTracker mt; //here you can use an array Image p1, p2, p3, p4, p5, p6, p7; About ab; //instance of About class Panel bottom; AudioClip b_n, rfs; Toolkit tk; Dimension dim; Button back, next, refresh, auto, abt, ext; Color sky; Font f; int count, x; public SR_Slider() { setTitle("SR-Slider 1.0"); setLayout(new BorderLayout()); bottom = new Panel(); time = new Thread(this); sky = new Color(0,140,255); //color - skyblue f = new Font("Courier",Font.BOLD,16); count = 0; x = 0; mt=new MediaTracker(this); try { //getting all images and add them to the MediaTracker p1 = Toolkit.getDefaultToolkit().getImage("Image/p1.jpg"); mt.addImage(p1, 0); p2 = Toolkit.getDefaultToolkit().getImage("Image/p2.jpg"); mt.addImage(p2, 1); p3 = Toolkit.getDefaultToolkit().getImage("Image/p3.jpg"); mt.addImage(p3, 2); p4 = Toolkit.getDefaultToolkit().getImage("Image/p4.jpg"); mt.addImage(p4, 3); p5 = Toolkit.getDefaultToolkit().getImage("Image/p5.jpg"); mt.addImage(p5, 4); p6 = Toolkit.getDefaultToolkit().getImage("Image/p6.jpg"); mt.addImage(p6, 5); p7 = Toolkit.getDefaultToolkit().getImage("Image/p7.jpg"); mt.addImage(p7, 6); fl = new File("user.dir"); dir = fl.toURL(); b_n = Applet.newAudioClip(new URL(dir, "Sound/bk_nxt.au")); rfs = Applet.newAudioClip(new URL(dir, "Sound/rfsh.au")); } catch(MalformedURLException e) { //do nothing! } //designing back button back=new Button("Back"); back.setFont(f); back.setBackground(sky); back.setForeground(Color.white); //designing next button next=new Button("Next"); next.setFont(f); next.setBackground(sky); next.setForeground(Color.white); //designing refresh button refresh=new Button("Reset"); refresh.setFont(f); refresh.setBackground(sky); refresh.setForeground(Color.white); //designing auto button auto=new Button("SlideShow"); auto.setFont(f); auto.setBackground(sky); auto.setForeground(Color.white); //designing abt button abt=new Button("About"); abt.setFont(f); abt.setBackground(sky); abt.setForeground(Color.white); //designing ext button ext=new Button("Exit"); ext.setFont(f); ext.setBackground(sky); ext.setForeground(Color.white); //adding ActionListener to all buttons back.addActionListener(this); next.addActionListener(this); refresh.addActionListener(this); auto.addActionListener(this); abt.addActionListener(this); ext.addActionListener(this); //adding all components to the Frame bottom.add(back); bottom.add(next); bottom.add(refresh); bottom.add(auto); bottom.add(abt); bottom.add(ext); add(bottom, BorderLayout.SOUTH); addWindowListener(this); tk = this.getToolkit(); dim = tk.getScreenSize(); //getting the current screen resolution setBounds(dim.width/6, dim.height/6, 540, 420); setResizable(false); setVisible(true); } public void paint(Graphics g) { if(count < 1) g.drawImage(p1, 0, 0, this); if(count == 1) g.drawImage(p2, 0, 0, this); if(count == 2) g.drawImage(p3, 0, 0, this); if(count == 3) g.drawImage(p4, 0, 0, this); if(count == 4) g.drawImage(p5, 0, 0, this); if(count == 5) g.drawImage(p6, 0, 0, this); if(count == 6) g.drawImage(p7, 0, 0, this); } public void update(Graphics g) { paint(g); } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == ext) { dispose(); setVisible(false); System.exit(0); } if(ae.getSource() == next) { b_n.play(); if(count < 7) { count++; repaint(); } } if(ae.getSource() == back) { b_n.play(); if(count == 0) count=0; if(count > 0) { count--; repaint(); } } if(ae.getSource() == refresh) { rfs.play(); time.suspend(); //suspends the thread count=0; repaint(); } if(ae.getSource() == auto) { b_n.play(); if(x < 1) { time.start(); x++; } else { time.resume(); //resumes the thread } } if(ae.getSource() == abt) { ab = new About(); } } public void run() { try { for(int i=1;i>0;i++) { b_n.play(); time.sleep(3000); count++; if(count > 6) { time.suspend(); //suspends the thread count = 0; } repaint(); } } catch(InterruptedException e) { //do nothing! } } //handling window events public void windowClosing(WindowEvent we) { dispose(); setVisible(false); System.exit(0); } public void windowClosed(WindowEvent we) { //do nothing! } public void windowOpened(WindowEvent we) { //do nothing! } public void windowActivated(WindowEvent we) { //do nothing! } public void windowDeactivated(WindowEvent we) { //do nothing! } public void windowIconified(WindowEvent we) { //do nothing! } public void windowDeiconified(WindowEvent we) { //do nothing! } class About extends Frame implements WindowListener { Font f, f2; private About() { setTitle("About"); setLayout(new FlowLayout()); f = new Font("Verdana", Font.BOLD, 20); f2 = new Font("Verdana", Font.PLAIN, 12); addWindowListener(this); tk = this.getToolkit(); dim = tk.getScreenSize(); //getting the current screen resolution setBounds(dim.width/4, dim.height/4, 300, 190); setResizable(false); setVisible(true); } public void paint(Graphics g) { g.setColor(Color.red); g.setFont(f); g.drawString("SR-Slider 1.0", 10, 60); g.setColor(Color.black); g.setFont(f2); g.drawString("Author: 雪碧的朋友", 10, 90); g.drawString("email:jing_feng70@163.com", 10, 110); g.drawString("谢谢使用", 10, 130); g.drawString("(C) ShuvoRim Pvt. Ltd.", 10, 150); g.drawString("All rights reserved.", 10, 170); } //handling window events public void windowClosing(WindowEvent we) { dispose(); setVisible(false); } public void windowClosed(WindowEvent we) { //do nothing! } public void windowOpened(WindowEvent we) { //do nothing! } public void windowActivated(WindowEvent we) { //do nothing! } public void windowDeactivated(WindowEvent we) { //do nothing! } public void windowIconified(WindowEvent we) { //do nothing! } public void windowDeiconified(WindowEvent we) { //do nothing! } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值