Retour

Code source de l'applet "COMPOSITION"

import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.DecimalFormat;

public class composition extends Applet implements MouseListener,MouseMotionListener,ItemListener,AdjustmentListener
{ private static final long serialVersionUID = 1420972181089580L;
Font font = new Font("SansSerif",1,12);
Font small = new Font("Serif",0,9);
DecimalFormat f20=new DecimalFormat("0.00");
Image ima;
Graphics h;
int la,ha;
int Xs,Ys,dX,dY;
int Xc=250,Yc=100,rule=3;
int Trule[]={1,9,11,6,8,4,2,10,5,7,3,12};
float alpha=0.5f;
FlowLayout fl= null;
Label lba=new Label("Alpha = "+f20.format(alpha),1);
Scrollbar sca= new Scrollbar(0,(int)(100*alpha),10,0,110);
Label lbr=new Label("Règle");
Choice chrule=new Choice();
Cursor hand=new Cursor(Cursor.HAND_CURSOR);
Cursor curs=new Cursor(Cursor.DEFAULT_CURSOR);
Shape rond=new Ellipse2D.Double(Xc,Yc,160,100);
Shape carre=new Rectangle2D.Double(50,70,120,120);
Shape rect=new Rectangle2D.Double(120,130,200,120);
boolean ok;

public void init()
{ resize(600,300);
setBackground(Color.lightGray);
setFont(font);
la=getWidth(); ha=getHeight();
ima=new BufferedImage(la,ha,BufferedImage.TYPE_INT_ARGB);
h=ima.getGraphics();
setLayout(fl);
addMouseListener(this);
addMouseMotionListener(this);
add(lbr); lbr.setBounds(400,10,40,25);
chrule.addItem("CLEAR");
chrule.addItem("DST");
chrule.addItem("DST_ATOP"); chrule.addItem("DST_IN");
chrule.addItem("DST_OUT"); chrule.addItem("DST_OVER");
chrule.addItem("SRC");
chrule.addItem("SRC_ATOP"); chrule.addItem("SRC_IN");
chrule.addItem("SRC_OUT"); chrule.addItem("SRC_OVER");
chrule.addItem("XOR");
add(chrule); chrule.setBounds(445,10,100,25);
chrule.addItemListener(this);
chrule.setBackground(Color.lightGray);
chrule.select(10);
add(lba); lba.setBounds(50,3,120,18);
add(sca); sca.setBounds(50,22,120,13);
sca.addAdjustmentListener(this);
sca.setBackground(Color.gray);}

public void itemStateChanged(ItemEvent evt)
{ if (evt.getSource()==chrule){
rule=Trule[chrule.getSelectedIndex()];}
repaint(); repaint();}

public void adjustmentValueChanged(AdjustmentEvent evt)
{ if (evt.getSource()==sca) {
alpha=sca.getValue()/100.f;
lba.setText("Alpha = "+f20.format(alpha));
repaint();}}

public void cadre3D(int x, int y, int w, int z)
{ Color blanc=new Color(230,230,230),gris =new Color(130,130,130);
h.setColor(gris); h.drawLine(x,y,w,y);
h.drawLine(x,y+1,w-1,y+1); h.drawLine(x,y,x,z);
h.drawLine(x+1,y,x+1,z-1); h.setColor(blanc);
h.drawLine(x,z,w,z); h.drawLine(w,y+1,w,z);
h.drawLine(x+1,z-1,w,z-1); h.drawLine(w-1,y+2,w-1,z);}

public void update(Graphics g)
{ paint(g);}

public void paint(Graphics g)
{ h.setColor(Color.lightGray); h.fillRect(0,0,la,ha);
h.setColor(Color.black); h.drawRect(0,0,la-1,ha-1);
h.setFont(small); h.drawString("jjR 01-2012",25,ha-25);
Graphics2D g2=(Graphics2D)h;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue); g2.fill(carre);
rond=new Ellipse2D.Double(Xc,Yc,160,100);
AlphaComposite alcomp = AlphaComposite.getInstance(rule,alpha);
AlphaComposite normal = AlphaComposite.getInstance(2,1.0f);
Composite ancien =(Composite) g2.getComposite();
g2.setComposite(alcomp);
g2.setPaint(Color.yellow); g2.fill(rect);
g2.setPaint(Color.red); g2.fill(rond);
g2.setComposite(ancien);
g2.setComposite(normal);
g2.setPaintMode();
h.setColor(Color.gray); h.drawOval(Xc,Yc,160,100);
h.drawRect(120,130,200,120);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
cadre3D(20,40,la-20,ha-20);
g.drawImage(ima,0,0,this);
showStatus("J. J. Rousseau 01-2012");}

public void mousePressed(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
if (rond.contains(Xs, Ys)){
dX=Xc-Xs; dY=Yc-Ys;
ok=true;}}

public void mouseReleased(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
dX=0; dY=0; ok=false;
repaint();}

public void mouseExited(MouseEvent evt)
{}

public void mouseClicked(MouseEvent evt)
{}

public void mouseEntered(MouseEvent evt)
{}

public void mouseDragged(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
if (ok){
Xc=Xs+dX; Yc=Ys+dY;
repaint();}}

public void mouseMoved(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
if (rond.contains(Xs, Ys))
setCursor(hand); else setCursor(curs);}

public void destroy()
{ h.dispose(); ima.flush();}
}

Retour