import java.awt.Graphics; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; import java.util.StringTokenizer; import javax.swing.JFrame; class Punkt { public int X; public int Y; public Punkt(int _X, int _Y) { X = _X; Y = _Y; } } public class Eksam2 extends JFrame { final int Suurus = 400; LinkedList L = new LinkedList(); int MinX = 0; int MinY = 0; int MaxX = 0; int MaxY = 0; float DeltaX; float DeltaY; int RingiX = -10; int RingiY = -10; public static void main(String[] args) { try { new Eksam2(); } catch (IOException e) { System.out.println("Hale draama -- datafaili pole :-(\n"); } } public void paint(Graphics G) { int tmpx; int tmpy; G.clearRect(0, 0, Suurus + 40, Suurus + 40); // Siin ja edaspidi on 10 ja 30 arvestatud selliselt, et koordinaatteljestiku ja // joonistatud punktide, ning akna a"a"rte vahele ja"a"ks pisuke tyhi ala G.drawLine( (int) ((-MinX) * DeltaX + 10), 30, (int) ((-MinX) * DeltaX + 10), Suurus + 30); G.drawLine( 10, (int) ((-MinY) * DeltaY + 40), Suurus, (int) ((-MinY) * DeltaY + 40)); // Joonistame punktid // Joonistame ringi [mis eeldatavasti peaks ekraanil liikuma] // System.out.println(RingiX + " " + RingiY); G.fillOval(RingiX, RingiY, 10, 10); Punkt P; for (int i = 0; i < L.size(); i++) { P = (Punkt) L.get(i); tmpx = (int) ((P.X - MinX) * DeltaX) + 10; tmpy = (int) ((P.Y - MinY) * DeltaY) + 40; // System.out.println(i + "(" + P.X + "; " + P.Y + ")"); G.drawString(String.valueOf(i), tmpx, tmpy); } } public Eksam2() throws IOException { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(Suurus + 40, Suurus + 40); setResizable(false); show(); BufferedReader F = new BufferedReader(new FileReader("data.txt")); String Rida; StringTokenizer T; int X; int Y; // Loeme data failist, tekitame iga punkti kohta objekti ning torkame listi while ((Rida = F.readLine()) != null) { Rida = Rida.trim(); T = new StringTokenizer(Rida); X = Integer.parseInt(T.nextToken()); Y = Integer.parseInt(T.nextToken()); // Uurime va"lja X ja Y maksimaalsed ja minimaalsed va"a"rtused, et saaksime // joonestada koordinaatteljestiku if (X > MaxX) MaxX = X; if (Y > MaxY) MaxY = Y; if (X < MinX) MinX = X; if (Y < MinY) MinY = Y; L.add(new Punkt(X, Y)); } F.close(); // System.out.println("Min/Max: " + MinX + " " + MinY + " " + MaxX + " " + MaxY + " "); // Arvestame deltad DeltaX = Suurus / (MaxX - MinX); DeltaY = Suurus / (MaxY - MinY); repaint(); // Paneme ringi ringi jooksma ;-) int i = 0; int j = 1; float dx; float dy; Punkt P1; Punkt P2; int tmpx1; int tmpx2; int tmpy1; int tmpy2; while (true) { // Ha"sti lihtne ja perversne animeerimine -- yhest punktist teise 20 // sammu, hoolimata punktide reaalsest vahekaugusest... P1 = (Punkt)L.get(i); P2 = (Punkt)L.get(j); tmpx1 = (int) ((P1.X - MinX) * DeltaX) + 10; tmpy1 = (int) ((P1.Y - MinY) * DeltaY) + 40; tmpx2 = (int) ((P2.X - MinX) * DeltaX) + 10; tmpy2 = (int) ((P2.Y - MinY) * DeltaY) + 40; System.out.println(tmpx1 + " " + tmpy1 + " " + tmpx2 + " " + tmpy2); dx = (tmpx2 - tmpx1) / 20; dy = (tmpy2 - tmpy1) / 20; for (int iter = 0; iter < 20; iter++) { RingiX = tmpx1 + (int)(iter * dx); RingiY = tmpy1 + (int)(iter * dy); System.out.println(RingiX + " " + RingiY); repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } i++; if (i >= L.size()) i = 0; j++; if (j >= L.size()) j = 0; } } }