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 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
class MyFrame extends JFrame{
private JPanel myPanel = new MyPanel();
public MyFrame(int w, int h, String titre){
super();
setTitle(titre);
setSize(w,h);
Container contenu = getContentPane();
contenu.add(myPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements MouseListener,MouseMotionListener,ActionListener{
private int x,y,xDeb,yDeb,xFin,yFin;
private Collection figures = new ArrayList();
private Collection figurecourante;
private JPanel northPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JPanel southPanel = new JPanel();
private JButton btEfface = new JButton("Effacer");
private ButtonGroup choixFonction = new ButtonGroup();
private JRadioButtonMenuItem radioRect = new JRadioButtonMenuItem("Rectangle");
private JRadioButtonMenuItem radioOval = new JRadioButtonMenuItem("Ellipse");
private JRadioButtonMenuItem radioFree = new JRadioButtonMenuItem("Libre", true);
private JRadioButtonMenuItem radioLine = new JRadioButtonMenuItem("Ligne");
private JRadioButtonMenuItem radioPolygon = new JRadioButtonMenuItem("Polygone");
//Constructeur
public MyPanel(){
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
addMouseListener(this);
addMouseMotionListener(this);
northPanel.add(btEfface);
btEfface.addActionListener(this);
choixFonction.add(radioRect);
choixFonction.add(radioOval);
choixFonction.add(radioFree);
choixFonction.add(radioLine);
choixFonction.add(radioPolygon);
radioRect.addActionListener(this);
radioOval.addActionListener(this);
radioFree.addActionListener(this);
radioLine.addActionListener(this);
radioPolygon.addActionListener(this);
southPanel.add(radioRect);
southPanel.add(radioOval);
southPanel.add(radioFree);
southPanel.add(radioLine);
southPanel.add(radioPolygon);
}
private void dessinerDynamique (int x, int y){
this.x = x;
this.y = y;
Graphics g = getGraphics ();
g.setXORMode(getBackground());
if ( xDeb != x && yDeb != y){
if (radioRect.isSelected()){
g.drawRect(Math.min(xDeb, x), Math.min(yDeb, y), Math.abs(xDeb - x), Math.abs(yDeb - y));
}
else if (radioOval.isSelected()){
g.drawOval(Math.min(xDeb, x), Math.min(yDeb, y), Math.abs(xDeb - x), Math.abs(yDeb - y));
}
else g.drawLine(x,y,xFin,yFin);
}
}
public void paintComponent(Graphics g){
Collection figurecourante;
super.paintComponent(g);
// /!\.......... Ici le code concerné ........../!\
Iterator it = figures.iterator();
while (it.hasNext()){
Figure fig = (Figure) it.next();
fig.draw(g);
}
}
public void mousePressed(MouseEvent e){
xDeb = e.getX();
yDeb = e.getY();
figurecourante = new ArrayList();
figurecourante.add(new Point(xDeb,yDeb));
x = xDeb;
y = yDeb;
}
public void mouseDragged(MouseEvent e){
xFin = e.getX();
yFin = e.getY();
dessinerDynamique(x,y);
dessinerDynamique(xFin,yFin);
x = xFin;
y = yFin;
if (radioFree.isSelected()){
figurecourante.add(new Point(x,y));
}
}
public void mouseReleased(MouseEvent e){
figures.add(figurecourante);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e){
figures.clear();
this.repaint();
}
}
//**********************************************************
public class TPDessin{
public static void main(String[] args){
JFrame frame = new MyFrame(600,400,"Dessin sur le panel");
frame.show();
}
} |
Partager