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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.event.* ;
public class Panneau extends javax.swing.JPanel implements MouseListener {
public Panneau () {
this.setSize(500, 500);
this.setBackground(Color.white);
this.addMouseListener(new MouseAdapter() {
public Graphics s ;
Graphics2D g2dd = (Graphics2D) s;
@Override
public void mousePressed(MouseEvent ev) {
int xx = ev.getX() ;
int yy= ev.getY() ;
System.out.println ("clic au point de coordonnees " + xx + ", " + yy ) ;
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
final Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
Dimension size = getSize();
int w = size.width ;
System.out.append(" width"+w);
int h = size.height;
System.out.append(" height"+h);
for (int angle = 1; angle <= 100; angle++) {
Random r = new Random();
// int x = Math.abs(r.nextInt()) % w;
//int y = Math.abs(r.nextInt()) % h;
int x= 202 + (int)(Math.cos(angle)*150);
int y= 202+(int)(Math.sin(angle)*150);
if( y< size.width/2 ){
g2d.setColor(Color.red);
g2d.fillRect(x, y, 5, 5);
// repaint();
}
else{
g2d.setColor(Color.black);
g2d.fillRect(x, y, 5, 5);
// repaint();
}
}
for (int i = 1; i <= 5; i++) {
Random r = new Random();
int x = Math.abs(r.nextInt()) % w;
int y = Math.abs(r.nextInt()) % h;
if( y< size.width/2 ){
g2d.setColor(Color.red);
g2d.fillRect(x, y, 5, 5);
// repaint();
}
else{
g2d.setColor(Color.black);
g2d.fillRect(x, y, 5, 5);
//repaint();
}
}
}
public void TraceurForme(Graphics g){
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
@Override
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
} |
Partager