Afficheur d'image accéléré java advanced imaging 1.1

Description

Cet afficheur est utilisé pour obtenir un affichage personnalisé de n'importe quel image supportée avec JAI/JIIO 1.1. Il s'implémente aisément dans n'importe quelle application awt/swing. Il dispose d'une capacité de redimensionnement avanée grace aux "transforms" Java2D.
C'est un composant JCSP actif.

b23:production GNU/GPL package installer du projet sf3jswing.
http://sourceforge.net/projects/sf3jswing/

Source / Exemple :


package installer;/*

  • Display.java
*
  • Created on 24 novembre 2006, 05:37
*
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
  • /
import com.sun.media.imageio.stream.FileChannelImageInputStream; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Composite; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.net.URISyntaxException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JComponent; import jcsp.lang.CSProcess; import jcsp.lang.Parallel; /**
  • This class allows to display an Image in a component that can be added to any Container.
  • Component will be scaled to Image size to fit it.
  • It is synchronized on the component with the MediaTracker.
  • @see #mt
  • @see Image
  • @see MediaTracker
  • @author www.b23prodtm.com
  • /
public class Display extends JComponent { /** the MediaTracker to manage full loading of the image data */ private MediaTracker mt; /** the image to display */ private Image display; /** phantom ref to image */ private PhantomReference phantomImage; /** Transform instance to scale the image */ private AffineTransform tx; /** background color
  • @default Color.LIGHT_GRAY */
private Color background = Color.LIGHT_GRAY; /***/ private Rectangle originalBox; /***/ private BufferedImage bgImg; /***/ private ReferenceQueue<? extends Reference> refQueue = new ReferenceQueue<Reference>(); /***/ public Display(String filename, AffineTransform tx) throws IOException { this(ImageIO.read( new FileChannelImageInputStream( new RandomAccessFile(filename, "rws").getChannel() )), tx); } /***/ public Display(URL filename, AffineTransform tx) throws IOException, URISyntaxException { this(ImageIO.read( new FileChannelImageInputStream( new RandomAccessFile(new File(filename.toURI()), "rws").getChannel() )), tx); } /***/ public Display(String filename, AffineTransform tx, Dimension size) throws IOException { this(ImageIO.read( new FileChannelImageInputStream( new RandomAccessFile(filename, "rws").getChannel() )), tx, size); } /***/ public Display(URL filename, AffineTransform tx, Dimension size) throws IOException, URISyntaxException { this(ImageIO.read( new FileChannelImageInputStream( new RandomAccessFile(new File(filename.toURI()), "rws").getChannel() )), tx, size); } /** the constructor of one Display
  • @param display the image to display
  • @param tx the transform to apply to the Display. if null, the component will fit the image size */
public Display(Image display, AffineTransform tx) { super(); phantomImage = new PhantomReference(display, refQueue); try { bgImg = ImageIO.read(getClass().getResourceAsStream("images/duke.gif")); mt = new MediaTracker(this); this.display = display; if(tx != null) { this.tx = tx; } else this.tx = AffineTransform.getScaleInstance(1.0, 1.0); originalBox = new Rectangle(0, 0, display.getWidth(this), display.getHeight(this)); setPreferredSize(originalBox.getBounds().getSize()); setSize(getPreferredSize()); validate(); repaint(); } catch (IOException ex) { ex.printStackTrace(); } } /** the constructor of one Display
  • @param display the image to display
  • @param tx the transform to apply to the Display. if null, the component will fit the image size */
public Display(Image display, AffineTransform tx, Dimension size) { super(); phantomImage = new PhantomReference(display, refQueue); try { bgImg = ImageIO.read(getClass().getResourceAsStream("images/duke.gif")); mt = new MediaTracker(this); this.display = display; if(tx != null) { this.tx = tx; } else this.tx = AffineTransform.getScaleInstance(1.0, 1.0); originalBox = new Rectangle(0, 0, display.getWidth(this), display.getHeight(this)); setPreferredSize(size); setSize(getPreferredSize()); repaint(); } catch (IOException ex) { ex.printStackTrace(); } } /***/ public void finalize() { try { super.finalize(); } catch (Throwable ex) { ex.printStackTrace(); } finally { Reference ref; while((ref = refQueue.poll()) instanceof Reference) { ref.clear(); } } } /** sets the background color
  • @param color the background color to use*/
public void setBGColor(Color color) { background = color; } /** returns the image displayed in this component
  • @return the displayed picture*/
public Image getPicture() { return display; } /***/ public void setTX(AffineTransform tx) { this.tx = tx; } /***/ public AffineTransform getTX() { return tx; } /** overriden to draw the image on the component graphics
  • @param g1 the Graphics instance*/
public void paintComponent(Graphics g1) { Graphics2D g = (Graphics2D)g1; originalBox = new Rectangle(0, 0, display.getWidth(this), display.getHeight(this)); Rectangle box = this.tx.createTransformedShape(new Rectangle(0, 0, getWidth(), getHeight())).getBounds(); AffineTransform resizeTX = AffineTransform.getScaleInstance( ((double)((float)box.width)/((float)originalBox.width)), ((double)((float)box.height)/((float)originalBox.height)) ); g.setClip(box); g.setBackground(background); g.clearRect(box.x, box.y, box.width, box.height); Composite cps = g.getComposite(); AffineTransform tx = AffineTransform.getScaleInstance( ((double)((float)box.width)/((float)bgImg.getWidth(this))), ((double)((float)box.height)/((float)bgImg.getWidth(this))) ); mt.addImage(bgImg, bgImg.hashCode(), box.width, box.height); mt.addImage(display, hashCode(), box.width, box.height); try { mt.waitForAll(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.33f)); g.drawImage(bgImg, tx, this); g.setComposite(cps); g.drawImage(display, resizeTX, this); super.paintComponent(g); } catch (InterruptedException ex) { ex.printStackTrace(); } } }

Conclusion :


Il y a une image de fond nommée images/duke.gif pour obtenir un fond personnalisé avec votre logo. Attention! le package JCSP doit être ajouté aux librairies de l'application pour compiler.
Annexe: JCSPclasses.zip

L'application Image Multi-Converter Ant utilise cet afficheur dans le sélecteur de fichiers intégré.
http://broumbroum84mirror3.ifrance.com/jnlp/demos/imc.jnlp

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.