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
| 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,x0,y0;
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",true);
private JRadioButtonMenuItem radioOval = new JRadioButtonMenuItem("Ellipse");
private JRadioButtonMenuItem radioFree = new JRadioButtonMenuItem("Libre");
private JRadioButtonMenuItem radioLine = new JRadioButtonMenuItem("Ligne");
private JRadioButtonMenuItem radioPolygon = new JRadioButtonMenuItem("Polygone");
Graphics g = getGraphics();
//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);
}
public void paintComponent(Graphics g){
Collection figurecourante;
super.paintComponent(g);
}
public void mousePressed(MouseEvent e){
x0 = e.getX();
y0 = e.getY();
}
public void mouseDragged(MouseEvent e){
Graphics g = getGraphics();
x = e.getX();
y = e.getY();
g.drawLine(x0,y0,x,y);
x0 = x;
y0 = 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