SplashImage 图片闪烁

package com.easyfeeling.ui;

//public class SplashImage {
//
//}

import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.RescaleOp;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class SplashImage extends JFrame{

 JBrightenComponent r1=null;
 private BufferedImage imageCharactor;

 public SplashImage()
 {
  setTitle("旋转齿轮");
  setSize(400,600);

  Image imgCharactor=new ImageIcon(getClass().getClassLoader()
   .getResource("resource/images/bj.jpg")).getImage();
  imageCharactor = new BufferedImage(imgCharactor.getWidth(null), imgCharactor.getHeight(null),
    BufferedImage.TYPE_INT_RGB);
  imageCharactor.getGraphics().drawImage(imgCharactor, 0,0, null);


  r1=new JBrightenComponent();
  r1.setBounds(0,0, 640, 480);
  this.getContentPane().add(r1);

 


 }

 

 class JBrightenComponent extends JComponent
 {
  private BrightenRunnable brightenRunnable=null;
  private Thread brightenThread=null;
  public JBrightenComponent(){
   brightenRunnable=new BrightenRunnable();
   brightenThread=new Thread(brightenRunnable);
   brightenThread.start();
  }
  public void paintComponent(Graphics g)
  {
   if (imageCharactor != null) g.drawImage(imageCharactor, 0, 0, null);  
  }
//  处理图像闪烁,就是人物图片不停旋转
  class BrightenRunnable implements Runnable{

   public void run() {
    // TODO 自动生成方法存根
    go();
   }
   private void go(){
    rotate();

   }

   private void rotate(){
    int isBrighten=-1;
    float a = 1.01f;
    for(int i=1;true;i++){
     try {
      Thread.sleep(200);
     } catch (InterruptedException e1) {
      // TODO 自动生成 catch 块
      e1.printStackTrace();
     }
     if(i%10==0){
      isBrighten*=-1;
     }
     if(isBrighten>0){
      a = 1.05f;
     }else{
      a = 0.95f;
     }
   
     //float b = 20.0f;//此值为透明色是否闪烁
     float b = 0;
     RescaleOp op = new RescaleOp(a, b, null);
     filter(op);
    }   
   }
   private void filter(BufferedImageOp op)
   {
    if (imageCharactor == null) return;
    imageCharactor = op.filter(imageCharactor, null);
    repaint();
   }
  }

 }
 public static void main(String[] args)
 {

  JFrame frame = new SplashImage();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);

 }
}

 

 

你可能感兴趣的:(image)