[ Pobierz całość w formacie PDF ]
.nextInt();return new Color(color);}}}Odtwarzanie z bazy danychUtworzenie bazy danych jako tablicy wspó³rzêdnych œrodków kó³ i kolorów.Ograniczenie liczby wykreœlanych kó³.Sygnalizowanie próby przekroczenialimitu.===============================================import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.util.Random;publicclass Master extends Applet {protected Random rand = new Random();protected Graphics gDC;protected final int r = 30;protected int count = 0;protected final int Size = 100;protected int xPos[], yPos[];protected Color color[];public void init(){gDC = getGraphics();xPos = new int [Size];yPos = new int [Size];color = new Color [Size];addMouseListener(new Watcher());}public void paint(Graphics gDC){for(int i = 0; i < count ; i++) {int x = xPos[i],y = yPos[i];Color c = color[i];gDC.setColor(c);gDC.fillOval(x-r, y-r, 2*r-1, 2*r-1);gDC.setColor(Color.black);gDC.drawOval(x-r, y-r, 2*r-1, 2*r-1);}}class Watcher extends MouseAdapter {public void mouseReleased(MouseEvent evt){if(count == Size) {Toolkit kit = Toolkit.getDefaultToolkit();kit.beep();return;}int x = evt.getX(),y = evt.getY();Color c = getColor();gDC.setColor(c);gDC.fillOval(x-r, y-r, 2*r-1, 2*r-1);gDC.setColor(Color.black);gDC.drawOval(x-r, y-r, 2*r-1, 2*r-1);xPos[count] = x;yPos[count] = y;color[count++] = c;}public Color getColor(){int color = rand.nextInt();return new Color(color);}}}Ko³a reprezentowane przez obiektyReprezentowanie kó³ przez obiekty klasy Circle.Utworzenie bazy danych jakodynamicznie rozszerzanej tablicy odnoœników do obiektów kó³.Zniesienieograniczenia co do liczby kó³.===============================================import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.util.Random;publicclass Master extends Applet {protected Random rand = new Random();protected Graphics gDC;protected int count = 0;protected int Size = 2;protected Circle circles[];public void init(){gDC = getGraphics();circles = new Circle [Size];addMouseListener(new Watcher());}public void paint(Graphics gDC){for(int i = 0; i < count ; i++) {Circle circle = circles[i];circle.draw(gDC);}}class Circle {protected int x, y;protected Color c;protected final int r = 30;public Circle(int x, int y, Color c){this.x = x;this.y = y;this.c = c;}public void draw(Graphics gDC){gDC.setColor(c);gDC.fillOval(x-r, y-r, 2*r-1, 2*r-1);gDC.setColor(Color.black);gDC.drawOval(x-r, y-r, 2*r-1, 2*r-1);}}class Watcher extends MouseAdapter {public void mouseReleased(MouseEvent evt){int x = evt.getX(),y = evt.getY();Color c = getColor();Circle circle = new Circle(x, y, c);circles[count++] = circle;circle.draw(gDC);if(count == Size)expand();}public void expand(){Circle[] oldCircles = circles;Size *= 2;circles = new Circle [Size];for(int i = 0; i < Size/2 ; i++) {circles[i] = oldCircles[i];}}public Color getColor(){int color = rand.nextInt();return new Color(color);}}}Baza w obiekcie klasy VectorUtworzenie bazy danych za pomoc¹ obiektu klasy Vector, stanowi¹cego dynamiczn¹kolekcjê obiektów klasy Circle.===============================================import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.util.*;publicclass Master extends Applet {protected Random rand = new Random();protected Graphics gDC;protected Vector objects;public void init(){gDC = getGraphics();objects = new Vector();addMouseListener(new Watcher());}public void paint(Graphics gDC){int count = objects.size();for(int i = 0; i < count ; i++) {Object object = objects
[ Pobierz całość w formacie PDF ]
Darmowy hosting zapewnia PRV.PL