2007 Finali Applet Sorusu:
Şafak arkadaşımız yazmış (o kim yaw )
SoruAlt.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class SoruAlt extends Applet
implements KeyListener, MouseListener, MouseMotionListener {
Point mouseCoord; // fareyi surekli takip eder
Color brush = Color.black; // firca rengi
ArrayList<Graphics> list = new ArrayList<Graphics>(); // grafik listesi
Label label = new Label("Toplam: 0"); // sekil sayisini gosteren etiket
public void init() {
/* olaylari dinle */
addKeyListener(this);
requestFocus();
addMouseListener(this);
addMouseMotionListener(this);
/* etiketi yerlestir */
this.setLayout(null);
add(label);
/* etiket klavye dinlemeli yoksa klavye hic dinlenmez */
label.addKeyListener(this);
label.requestFocus();
}
public void paint(Graphics g) {
/* Listedeki daireleri ciz */
Rectangle r;
int numOvals = list.size();
for(int i=0; i<numOvals; i++) {
g = list.get(i); // listeden nesneyi cek
/* grafigi ciz */
r = g.getClipBounds();
g.fillOval(r.x, r.y, r.width, r.height);
}
/* Toplam daire sayisini goster */
label.setBounds(getWidth()-80,0, 80, 20);
label.setText("Toplam: "+list.size());
}
/* farenin altında en ondeki direnin ve fircanin rengini degistirir */
public void ChangeBrush(Color c) {
Graphics g;
Rectangle r;
int i, numOvals = list.size(); // eleman sayisini bul
this.brush = c; // firca rengini degistir
/* fare altinda en ondeki daireyi bulur */
for(i=numOvals-1; i>=0; i--) {
g = list.get(i); // listeden nesneyi cek
r = g.getClipBounds();
if(isPointOnCircle(g, new Point(mouseCoord.x, mouseCoord.y))) {
g.setColor(brush); // dairenin rengini degistir
/* sadece gecerli daireyi yeniden ciz */
repaint(r.x, r.y, r.width, r.height);
break;
}
}
}
/* belirtilen nokta belirtilen daire uzerinde ise TRUE */
private boolean isPointOnCircle(Graphics oval, Point p) {
Rectangle r = oval.getClipBounds();
double cap = (r.width)/2;
double centerX = (r.x) + cap;
double centerY = (r.y) + cap;
double distance = Math.sqrt(Math.pow((centerX - p.x),2) + Math.pow((centerY - p.y),2));
if(distance <= cap)
return true;
else
return false;
}
public void keyPressed(KeyEvent ke) {
int keynum = ke.getKeyCode();
if(keynum == KeyEvent.VK_DELETE || keynum == KeyEvent.VK_D) {
Graphics g;
Rectangle r;
int i, numOvals = list.size(); // eleman sayisini bul
/* fare altinda en ondeki daireyi bulur */
for(i=numOvals-1; i>=0; i--) {
g = list.get(i); // listeden nesneyi cek
r = g.getClipBounds();
if(isPointOnCircle(g, new Point(mouseCoord.x, mouseCoord.y))) {
list.remove(g); // sekli listeden sil
/* silinen yeri tazele */
repaint(r.x, r.y, r.width, r.height);
break;
}
}
}
else if(keynum == KeyEvent.VK_F1)
ChangeBrush(Color.red);
else if(keynum == KeyEvent.VK_F2)
ChangeBrush(Color.blue);
else if(keynum == KeyEvent.VK_F3)
ChangeBrush(Color.green);
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
public void mouseClicked(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
Graphics g = getGraphics(); // yeni grafik nesnesi
g.setColor(brush); // nesnenin rengini ayarla
/* 20px yaricapli dairenin merkez koordinatlari */
int x = me.getX()-20;
int y = me.getY()-20;
g.setClip(x,y,40,40); // sekli ayarla
list.add(g); // sekli listeye ekle
paint(g); // olay bolgesini tazele
}
public void mouseReleased(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseDragged(MouseEvent me) {
}
public void mouseMoved(MouseEvent me) {
/* fare koordinatlarini izle */
mouseCoord = me.getPoint();
}
}
Let`s make this world a better place to live !
|