/**
*
* Besserwisser
*
* Copyright (c) 2007 Luca Schneider
*
*
*/
//Packet-Import
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Besserwisser extends JPanel implements MouseListener {
static long distanz;
static double x, y;
static String distanzausgabe;
static int xpin, ypin;
static Image imgpin;
private static final long serialVersionUID = 1L;
// Bild für Hintergrund (Karte)
Image imgkarte = new ImageIcon("europa-karte-polo.jpg").getImage();
// Zeichnen
public void paint(Graphics g) {
// Schwarzer Rand
g.setColor(Color.black);
g.fillRect(0, 0, 876, 802);
// Weisser Hintergrund
g.setColor(Color.white);
g.fillRect(1, 1, 874, 800);
// Schriftart, -grösse und -farbe für "Loading...-Text"
Font font1 = new Font("Arial", Font.PLAIN, 20);
g.setFont(font1);
g.setColor(Color.black);
g.drawString("Loading...", 350, 400);
// Hintergrundkarte zeichnen
g.drawImage(imgkarte, 1, 1, this);
// Schriftart, -grösse und -farbe für Überschriften (FETT)
Font font2 = new Font("Arial", Font.BOLD, 16);
g.setFont(font2);
g.setColor(Color.black);
// Aufgabenstellung
g.drawString("Aufgabe:", 657, 65);
// Hilfe
g.drawString("Hilfe:", 657, 249);
// Ergebnis
g.drawString("Ergebnis:", 657, 155);
// Schriftart, -grösse und -farbe für Text (Normal)
Font font3 = new Font("Arial", Font.PLAIN, 16);
g.setFont(font3);
g.setColor(Color.black);
// Aufgabenstellung
g.drawString("Finden Sie " + stadt + ",", 657, 85);
g.drawString("die Hauptstadt", 657, 105);
g.drawString(land + ".", 657, 125);
// Ergebnis
g.drawString("Distanz zu", 657, 175);
g.drawString(stadt + " beträgt: ", 657, 195);
// Hilfe
g.drawString("Klicken Sie einfach", 657, 269);
g.drawString("auf die Karte.", 657, 289);
// Zeichnen der Distanz und des Pins
if (distanzausgabe != null)
g.drawString(distanzausgabe, 657, 215);
if (imgpin != null)
g.drawImage(imgpin, xpin, ypin, this);
}
// Zufallszahl zwischen 1 und 49 erzeugen (für Zufallsstadt)
long start = Math.round(Math.random() * 48 + 1);
// Variablen
double stadty = 0, stadtx = 0;
String stadt = "Fehler", land = "Fehler"; // Falls keine Stadt angegeben wird
{
// Start Städte-Zuordnung mit Koordinaten und String mit Stadt + Land
// Reykjavik
if (start == 1) {
stadtx = 105;
stadty = 47;
stadt = "Reykjavik";
land = "von Island"; // Verschiedene Varianten (von, der...)
}
// Athen
if (start == 2) {
stadtx = 499;
stadty = 650;
stadt = "Athen";
land = "von Griechenland";
}
// Andorra
if (start == 3) {
stadtx = 152;
stadty = 554;
stadt = "Andorra";
land = "von Andorra";
}
// Rom
if (start == 4) {
stadtx = 336;
stadty = 571;
stadt = "Rom";
land = "von Italien";
}
// San Marino
if (start == 5) {
stadtx = 337;
stadty = 534;
stadt = "San Marino";
land = "von San Marino";
}
// Paris
if (start == 6) {
stadtx = 222;
stadty = 407;
stadt = "Paris";
land = "von Frankreich";
}
// Valletta
if (start == 7) {
stadtx = 354;
stadty = 689;
stadt = "Valletta";
land = "von Malta";
}
// Monaco
if (start == 8) {
stadtx = 268;
stadty = 526;
stadt = "Monaco";
land = "von Monaco";
}
// Vatikan Stadt
if (start == 9) {
stadtx = 336;
stadty = 571;
stadt = "Vatikan Stadt";
land = "vom Vatikan";
}
// Wilna
if (start == 10) {
stadtx = 501;
stadty = 325;
stadt = "Wilna";
land = "von Littauen";
}
// Madrid
if (start == 11) {
stadtx = 99;
stadty = 566;
stadt = "Madrid";
land = "von Spanien";
}
// Budapest
if (start == 12) {
stadtx = 423;
stadty = 450;
stadt = "Budapest";
land = "von Ungarn";
}
// Bukarest
if (start == 13) {
stadtx = 535;
stadty = 522;
stadt = "Bukarest";
land = "von Rumänien";
}
// Sofia
if (start == 14) {
stadtx = 497;
stadty = 558;
stadt = "Sofia";
land = "von Bulgarien";
}
// Helsinki
if (start == 15) {
stadtx = 480;
stadty = 223;
stadt = "Helsinki";
land = "von Finnland";
}
// Kiew
if (start == 16) {
stadtx = 558;
stadty = 390;
stadt = "Kiew";
land = "der Ukraine";
}
// Riga
if (start == 17) {
stadtx = 480;
stadty = 285;
stadt = "Riga";
land = "von Lettland";
}
// Tiflis
if (start == 18) {
stadtx = 795;
stadty = 510;
stadt = "Tiflis";
land = "von Georgien";
}
// Minsk
if (start == 19) {
stadtx = 546;
stadty = 348;
stadt = "Minsk";
land = "von Weissrussland";
}
// Bern
if (start == 20) {
stadtx = 258;
stadty = 453;
stadt = "Bern";
land = "der Schweiz";
}
// Kischinew
if (start == 21) {
stadtx = 558;
stadty = 479;
stadt = "Kischinew";
land = "von Moldavien";
}
// Tirana
if (start == 22) {
stadtx = 438;
stadty = 585;
stadt = "Tirana";
land = "von Albanien";
}
// Moskau
if (start == 23) {
stadtx = 671;
stadty = 313;
stadt = "Moskau";
land = "von Russland";
}
// Berlin
if (start == 24) {
stadtx = 368;
stadty = 363;
stadt = "Berlin";
land = "von Deutschland";
}
// Edinburgh
if (start == 25) {
stadtx = 189;
stadty = 268;
stadt = "Edinburgh";
land = "von Schottland";
}
// Dublin
if (start == 26) {
stadtx = 143;
stadty = 295;
stadt = "Dublin";
land = "von Irland";
}
// Wien
if (start == 27) {
stadtx = 400;
stadty = 420;
stadt = "Wien";
land = "von Österreich";
}
// Pressburg
if (start == 28) {
stadtx = 406;
stadty = 438;
stadt = "Pressburg";
land = "der Slowakei";
}
// Prag
if (start == 29) {
stadtx = 372;
stadty = 382;
stadt = "Prag";
land = "von Tschechien";
}
// Ankara
if (start == 30) {
stadtx = 630;
stadty = 609;
stadt = "Ankara";
land = "der Türkei";
}
// Warschau
if (start == 31) {
stadtx = 446;
stadty = 376;
stadt = "Warschau";
land = "von Polen";
}
// Luxemburg
if (start == 32) {
stadtx = 275;
stadty = 274;
stadt = "Luxemburg";
land = "von Luxemburg";
}
// Tallinn
if (start == 33) {
stadtx = 487;
stadty = 237;
stadt = "Tallinn";
land = "von Estland";
}
// Stockholm
if (start == 34) {
stadtx = 416;
stadty = 241;
stadt = "Stockholm";
land = "von Schweden";
}
// Vaduz
if (start == 35) {
stadtx = 287;
stadty = 450;
stadt = "Vaduz";
land = "vom Lichtenstein";
}
// Kopenhagen
if (start == 36) {
stadtx = 365;
stadty = 296;
stadt = "Kopenhagen";
land = "von Dänemark";
}
// Amsterdamm
if (start == 37) {
stadtx = 275;
stadty = 348;
stadt = "Amsterdamm";
land = "der Niederlande";
}
// Zagreb
if (start == 38) {
stadtx = 385;
stadty = 491;
stadt = "Zagreb";
land = "von Kroatien";
}
// Oslo
if (start == 39) {
stadtx = 351;
stadty = 222;
stadt = "Oslo";
land = "von Norwegen";
}
// Cardiff
if (start == 40) {
stadtx = 171;
stadty = 351;
stadt = "Cardiff";
land = "von Wales";
}
// Belfast
if (start == 41) {
stadtx = 156;
stadty = 279;
stadt = "Belfast";
land = "von Nordirland";
}
// Brüssel
if (start == 42) {
stadtx = 252;
stadty = 379;
stadt = "Brüssel";
land = "von Belgien";
}
// Lissabon
if (start == 43) {
stadtx = 12;
stadty = 561;
stadt = "Lissabon";
land = "von Portugal";
}
// Ljubljana
if (start == 44) {
stadtx = 364;
stadty = 487;
stadt = "Ljubljana";
land = "von Slovenien";
}
// Skopje
if (start == 45) {
stadtx = 468;
stadty = 572;
stadt = "Skopje";
land = "von Mazedonien";
}
// London
if (start == 46) {
stadtx = 199;
stadty = 359;
stadt = "London";
land = "von England";
}
// Sarajevo
if (start == 47) {
stadtx = 414;
stadty = 542;
stadt = "Sarajevo";
land = "von Bosnien-Herzegowina";
}
// Belgrad
if (start == 48) {
stadtx = 456;
stadty = 515;
stadt = "Belgrad";
land = "von Serbien";
}
// Gibraltar
if (start == 49) {
stadtx = 50;
stadty = 621;
stadt = "Gibraltar";
land = "von Gibraltar";
}
} // Ende Städte-Zuordnung
// Fenster erzeugen
public static void main(String[] args) {
JFrame frame = new JFrame("Java Karten-Spiel"); // Titel
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Besserwisser();
frame.setContentPane(newContentPane);
frame.setSize(884, 836); // Grösse
frame.setVisible(true);
}
public Besserwisser() {
addMouseListener(this);// MouseListener zum Panel hinzufügen
}
public void mousePressed(MouseEvent e) {
// Was machen, wenn Maus gedrückt?
}
public void mouseReleased(MouseEvent e) {
// Was machen, wenn Maus losgelassen?
}
public void mouseEntered(MouseEvent e) {
// Was machen, wenn Maus in Panel kommt?
}
public void mouseExited(MouseEvent e) {
// Was machen, wenn Maus aus Panel geht?
}
public void mouseClicked(MouseEvent e) {
// Variablen
double ux, uy, v, d;
// Geklickte X und Y Koordianten
x = e.getPoint().getX();
y = e.getPoint().getY();
// Unterschied der verschiedenen X und Y Koordinaten
if (x > stadtx) {
ux = x - stadtx;
if (y > stadty) {
uy = y - stadty;
} else {
uy = stadty - y;
}
} else {
ux = stadtx - x;
if (y > stadty) {
uy = y - stadty;
} else {
uy = stadty - y;
}
}
// Satz des Pythagoras für Vektorberechnung der 2 Punkte (in Pixel)
v = Math.sqrt((ux * ux) + (uy * uy));
// Pixel in Kilometer
d = v * 5.5075; // Geschätzt und berechnet anhand der Karte
distanz = Math.round(d); // Auf long runden
// Distanzanzeige (String) berechnen
distanzausgabe = distanz + " Kilometer";
// Bild für Pin
Image imgpin = new ImageIcon("pin.png").getImage();
xpin = (int) (x - 10);
ypin = (int) (y - 32);
repaint();
}
}