Retour

Code source de l'applet "TRAITS"

import java.applet.*;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;

public class traits extends Applet implements ItemListener,AdjustmentListener
{ private static final long serialVersionUID = 1420972181089577L;
Font font = new Font("SansSerif",1,12);
Font small = new Font("Serif",0,9);
Image ima; Graphics h;
int la,ha;
FlowLayout fl= null;
CheckboxGroup bg1 = new CheckboxGroup();
Checkbox cb1,cb2;
Label lbcap=new Label("Cap",1);
Choice chcap = new Choice();
Label lbjoin=new Label("Join",1);
Choice chjoin = new Choice();
Label lblar=new Label("Largeur = 2",1);
Scrollbar scl= new Scrollbar(0,20,10,10,110);
Choice chpoint = new Choice();
Checkbox cb3;
int[]Tcap={0,1,2},Tjoin={0,1,2};
int []X={300,350,500,550,360},Y={80,180,150,90,110};
int cap,join,tiret;
Object cren;
float motif [][]= {{8.0f,4.0f},{20.0f,4.0f},{10.0f,2.0f,2f,2f},{15.0f,2.0f,8f,2f,5.0f,2.0f}};
float lar=2.0f;
BasicStroke stroke=new BasicStroke(lar,0,0);
BasicStroke normal= new BasicStroke();
DecimalFormat f10=new DecimalFormat("0.0");
boolean anticren=false,continu=true;

public void init()
{ resize(600,260);
setBackground(Color.lightGray);
setFont(font);
la=getWidth(); ha=getHeight();
ima=createImage(la,ha);
h=ima.getGraphics();
setLayout(fl);
add(cb1 = new Checkbox("Continu",bg1,true));
cb1.addItemListener(this);
cb1.setBounds(20,5,80,20);
add(cb2 = new Checkbox("Pointillés",bg1,false));
cb2.addItemListener(this);
cb2.setBounds(20,25,80,20);
add(lbcap); lbcap.setBounds(100,3,110,18);
chcap.addItem("CAP_BUTT");
chcap.addItem("CAP_ROUND");
chcap.addItem("CAP_SQUARE");
add(chcap);
chcap.addItemListener(this);
chcap.setBackground(Color.lightGray);
chcap.setBounds(100,22,110,20);
add(lbjoin); lbjoin.setBounds(220,3,110,18);
chjoin.addItem("JOIN_MITTER");
chjoin.addItem("JOIN_ROUND");
chjoin.addItem("JOIN_BEVEL");
add(chjoin);
chjoin.addItemListener(this);
chjoin.setBackground(Color.lightGray);
chjoin.setBounds(220,22,110,20);
add(lblar); lblar.setBounds(340,3,100,18);
add(scl); scl.setBounds(340,23,100,15);
scl.addAdjustmentListener(this);
scl.setBackground(Color.gray);
add(cb3 = new Checkbox("Anticrénelage",false));
cb3.addItemListener(this);
cb3.setBounds(460,3,100,20);
chpoint.addItem("Tirets 1");
chpoint.addItem("Tirets 2");
chpoint.addItem("Tirets 3");
chpoint.addItem("Tirets 4");
add(chpoint); chpoint.setBounds(460,25,100,20);
chpoint.setBackground(Color.lightGray);
chpoint.addItemListener(this);
initStroke();}

public void itemStateChanged(ItemEvent evt)
{ if (evt.getSource()==cb1)continu=true;
else if (evt.getSource()==cb2)continu=false;
else if (evt.getSource()==chcap){
cap=Tcap[chcap.getSelectedIndex()];}
else if (evt.getSource()==chjoin){
join=Tjoin[chjoin.getSelectedIndex()];}
else if (evt.getSource()==cb3)anticren=!anticren;
else if (evt.getSource()==chpoint){
tiret=chpoint.getSelectedIndex();}
initStroke(); repaint();}

public void adjustmentValueChanged(AdjustmentEvent evt)
{ if (evt.getSource()==scl) {
lar=(float)(scl.getValue()/10.);
lblar.setText("Largeur = "+f10.format(lar));
initStroke();}
repaint();}

public void initStroke()
{ if (continu)stroke=new BasicStroke(lar,cap,join);
else stroke=new BasicStroke(lar,cap,join,10.f,motif[tiret],0);
if (anticren)cren=RenderingHints.VALUE_ANTIALIAS_ON;
else cren=RenderingHints.VALUE_ANTIALIAS_OFF;}

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.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,cren);
h.drawRect(100,62,140,50);
h.drawLine(50,80,60,200);
h.drawLine(500,200,100,220);
h.drawOval(150,130,100,70);
h.drawPolygon(X,Y,5);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
g2.setStroke(normal);
cadre3D(20,50,la-20,ha-20);
g.drawImage(ima,0,0,this);
showStatus("J. J. Rousseau 01-2012");}

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