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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D.Double;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelWithCircles extends JPanel implements MouseListener, MouseMotionListener{
private ArrayList<MyCircle> shapes;
private MyCircle selectedCircle;
private Point mousePos;
int dx;
int dy;
public PanelWithCircles() {
shapes = new ArrayList<MyCircle>();
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
Point p = e.getPoint();
for(MyCircle circle : shapes) {
if(circle.contains(p)) {
selectedCircle = circle;
break;
}
}
if(selectedCircle != null) {
System.out.println(selectedCircle.getPosition().x);
dx = (selectedCircle.getPosition().x-e.getX());
dy = (selectedCircle.getPosition().y-e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
selectedCircle = null;
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("drag");
if(selectedCircle != null) {
selectedCircle.setPosition(new Point(e.getX()+dx, e.getY()+dy));
repaint();
}
}
@Override
protected void paintComponent(Graphics arg0) {
// TODO Auto-generated method stub
super.paintComponent(arg0);
if(isShowing()) {
Graphics2D g2d = (Graphics2D) arg0;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//On affiche le damier
int caseWith = Math.min(getWidth(), getHeight())/10;
for(int i = 0; i<10;i++ ) {
for(int j = 0; j<10;j++ ) {
if((i+j)%2 == 1) {
g2d.setPaint(Color.black);
} else {
g2d.setPaint(Color.white);
}
g2d.fill(new Rectangle2D.Double(i*caseWith,j*caseWith,(i+1)*caseWith, (j+1)*caseWith));
}
}
//ON affiche les pions
for(MyCircle circle : shapes) {
circle.draw(g2d);
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
public void addCircle(MyCircle c) {
shapes.add(c);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PanelWithCircles p = new PanelWithCircles();
p.addCircle(new MyCircle(new Point(10,10),Color.blue, 20));
p.addCircle(new MyCircle(new Point(50,10),Color.red, 20));
p.addCircle(new MyCircle(new Point(50,50),Color.green, 20));
p.addCircle(new MyCircle(new Point(10,50),Color.yellow, 20));
JFrame f = new JFrame();
f.setSize(400,300);
f.add(p);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
} |