1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| package wComposant;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import wOutils.Gp;
public class JPanelImage extends JPanel {
private static final long serialVersionUID = 1L;
Gp gp=null;
String image=null;
private int mode=Gp.JPGNULL;
private TexturePaint texture;
private TexturePaint textureGrayed;
private BufferedImage bufferedImage;
private BufferedImage bufferedImageGrayed;
public JPanelImage(){
super();
}
public JPanelImage(BorderLayout borderLayout) {
super(borderLayout);
}
public JPanelImage(GridBagLayout gridbaglayout) {
super(gridbaglayout);
}
public JPanelImage(Gp gp,String image,int mode){
super();
setImage(gp,image,mode);
}
public void setMode(int mode){
this.mode=mode;
}
public void setImage(Gp gp,String image,int mode){
this.gp=gp;
this.image=image;
this.mode = mode;
Image jpg=gp.getImage(image,false);
if (jpg!=null){
this.bufferedImage = gp.toBufferedImage(jpg);
this.bufferedImageGrayed = gp.grayedImage(gp.toBufferedImage(jpg));
this.texture = new TexturePaint(bufferedImage,new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()));
this.textureGrayed= new TexturePaint(bufferedImageGrayed,new Rectangle(0, 0, bufferedImageGrayed.getWidth(), bufferedImageGrayed.getHeight()));
jpg=null;
} else {
mode=Gp.JPGNULL;
texture=null;
textureGrayed=null;
bufferedImage=null;
bufferedImageGrayed=null;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
switch( mode )
{ case Gp.JPGTEXTURE :
if(texture!=null){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(), getHeight() );
Graphics2D g2d = (Graphics2D)g;
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.f);
g2d.setComposite(c);
if (this.isEnabled())
g2d.setPaint(texture);
else
g2d.setPaint(textureGrayed);
g2d.fillRect(0, 0, getWidth(), getHeight() );
c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.f);
g2d.setComposite(c);
}
break;
case Gp.JPGFILL :
if(bufferedImage!=null){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(), getHeight() );
Graphics2D g2d=(Graphics2D) g;
double w=(double) getWidth()/ (double) bufferedImage.getWidth();
double h=(double) getHeight()/ (double)bufferedImage.getHeight();
g2d.scale(w,h);
if (this.isEnabled())
g2d.drawImage(bufferedImage,0,0,null);
else
g2d.drawImage(bufferedImageGrayed,0,0,null);
w=(double) bufferedImage.getWidth()/ (double) getWidth();
h=(double) bufferedImage.getHeight()/ (double)getHeight();
g2d.scale(w,h);
}
break;
case Gp.JPGCENTER:
if(bufferedImage!=null){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(), getHeight() );
if (this.isEnabled())
g.drawImage(bufferedImage,(getWidth()-bufferedImage.getWidth())/2,(getHeight()-bufferedImage.getHeight())/2,null);
else
g.drawImage(bufferedImageGrayed,(getWidth()-bufferedImageGrayed.getWidth())/2,(getHeight()-bufferedImageGrayed.getHeight())/2,null);
}
break;
case Gp.JPGGRADIENT:
if (Gp.USEPANELGRADIENT)
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(), getHeight() );
if (this.isEnabled()){
gp.setBackgroundGradient(g,this);
}
break;
}
}
} |
Partager