Advertisement
Guest User

BlueJ-Blubble Shooter

a guest
Jul 7th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.04 KB | None | 0 0
  1. Klasse KUGELLLAGER:--------------------------------------------------------------------------------------------------------------------
  2.  
  3.  
  4. public class KUGELLAGER
  5. {
  6. private BLUBBLE[] elemente;
  7.  
  8. private int anzahl;
  9.  
  10.  
  11. public KUGELLAGER()
  12. {
  13. anzahl = 0;
  14. elemente = new BLUBBLE[1000];
  15.  
  16.  
  17. }
  18.  
  19. public void zeichne()
  20. {
  21.  
  22. for (int i=0; i < anzahl; i++)
  23. {
  24. elemente[i].zeichne();
  25. }
  26.  
  27. }
  28.  
  29.  
  30. public void fuegeHinzu(BLUBBLE elementNeu)
  31. {
  32. if(anzahl < 1000)
  33. {
  34. elemente[anzahl] = elementNeu;
  35. anzahl = anzahl + 1;
  36. }
  37. }
  38.  
  39.  
  40. }
  41.  
  42.  
  43. KLASSE MANAGER:--------------------------------------------------------------------------------------------------------------------
  44.  
  45.  
  46. import javax.swing.Timer;
  47. import java.awt.event.*;
  48. import java.util.Random;
  49.  
  50. public class MANAGER
  51. {
  52. private KUGELLAGER lager;
  53. private Random zufall;
  54. private SHOOTER shooter;
  55. private Timer uhr;
  56.  
  57.  
  58. public MANAGER()
  59. {
  60. lager = new KUGELLAGER();
  61. zufall = new Random();
  62. shooter = new SHOOTER(10, 450, zufall.nextInt(8)+1, 5);
  63. uhr = new Timer(100, new ActionListener()
  64. {
  65. public void actionPerformed(ActionEvent e)
  66. {
  67. ticke();
  68. }
  69. });
  70.  
  71. }
  72.  
  73. private void ticke()
  74. {
  75. shooter.zeichne();
  76. shooter.bewege(0.1);
  77. }
  78.  
  79. public void starte()
  80. {
  81. uhr.start();
  82. }
  83.  
  84. public void stoppe()
  85. {
  86. uhr.stop();
  87. }
  88.  
  89. public void erzeugeBlubble()
  90. {
  91. for (int j = 0; j < 150; j++)
  92. {
  93. for (int i = 0; i < 150 ; i++)
  94. {
  95. BLUBBLE blubble = new BLUBBLE(10 + i*20, j*20 +20, zufall.nextInt(5)+1);
  96. lager.fuegeHinzu(blubble);
  97. }
  98. }
  99. lager.zeichne();
  100. }
  101. }
  102.  
  103.  
  104. Klasse SHOOTER:--------------------------------------------------------------------------------------------------------------------
  105.  
  106.  
  107. import javax.swing.Timer;
  108. import java.awt.event.*;
  109. import java.util.Random;
  110.  
  111. public class SHOOTER
  112. {
  113. private double xMitte;
  114. private double yMitte;
  115. private int radius;
  116. private int farbnr;
  117. private Random zufall;
  118. private double vx, vy;
  119. private static int lWand =10;
  120. private static int rWand =500;
  121.  
  122. public SHOOTER(double xStart, double yStart, int farbnrStart, int vxStart)
  123. {
  124. xMitte = xStart;
  125. yMitte = yStart;
  126. radius = 10;
  127. zufall = new Random();
  128. farbnr = farbnrStart;
  129. vy = 0;
  130. vx = vxStart;
  131. }
  132.  
  133.  
  134. public void zeichne()
  135. {
  136. int yAlt = 0, xAlt = 0;
  137. ZEICHENFENSTER.gibFenster().fuelleKreis((int)xMitte, (int)yMitte, 10, farbnr);
  138. xMitte=xAlt;
  139. yMitte=yAlt;
  140. ZEICHENFENSTER.gibFenster().fuelleKreis((int)xMitte, (int)yMitte, 10, 0);
  141.  
  142. }
  143.  
  144. protected static int giblWand()
  145. {
  146. return lWand;
  147. }
  148.  
  149. protected static int gibrWand()
  150. {
  151. return rWand;
  152. }
  153.  
  154. public void bewege(double zeit)
  155. {
  156. if (xMitte < SHOOTER.gibrWand())
  157. {
  158. xMitte = xMitte + vx * zeit;
  159. //yMitte = yMitte + vy * zeit;
  160. }
  161. else
  162. {
  163. vx = -vx;
  164. }
  165. }
  166.  
  167.  
  168. }
  169.  
  170.  
  171. Klasse BLUBBLE:--------------------------------------------------------------------------------------------------------------------
  172.  
  173.  
  174. import java.util.Random;
  175.  
  176. class BLUBBLE
  177. {
  178. protected double xMitte;
  179. protected double yMitte;
  180. protected int radius;
  181. protected int farbnr;
  182. protected Random zufall;
  183.  
  184.  
  185. public BLUBBLE(int xStart, int yStart, int farbnrStart)
  186. {
  187. xMitte = xStart;
  188. yMitte = yStart;
  189. radius = 10;
  190. zufall = new Random();
  191. farbnr = farbnrStart;
  192. }
  193.  
  194. public void zeichne()
  195. {
  196. ZEICHENFENSTER.gibFenster().fuelleKreis((int)xMitte, (int)yMitte, radius, farbnr);
  197. }
  198.  
  199.  
  200.  
  201. }
  202.  
  203. Klasse ZEICHENFENSTER:-----------------------------------------------------------------------------------------------------------------
  204.  
  205.  
  206. import javax.swing.*;
  207. import java.awt.*;
  208. import java.awt.geom.*;
  209.  
  210. /**
  211. * Class ZEICHENFENSTER - Eine Klasse, die einfache grafische Zeichnungen
  212. * in einem Programmfenster ermöglicht.
  213. *
  214. * @author Michael Kolling (mik)
  215. * @author Bruce Quig
  216. * @author Christian Heidrich
  217. *
  218. * @version 2007.05.07
  219. */
  220.  
  221. public class ZEICHENFENSTER
  222. {
  223. private JFrame frame;
  224. private CanvasPane canvas;
  225. private JPanel steuerungOst,steuerungSued;
  226. private Graphics2D graphic;
  227. private Color backgroundColor;
  228. private Image canvasImage;
  229.  
  230. private static ZEICHENFENSTER singleton;
  231.  
  232. /**
  233. * Erzeugt eine Zeichenfenster mit Standardmaßen 600*500 und Hintergrundfarbe weiß
  234. * @param titel Titel des Fensters
  235. */
  236. public ZEICHENFENSTER(String titel)
  237. {
  238. this(titel, 500, 500, Color.black);
  239. }
  240.  
  241. /**
  242. * Erzeugt ein Zeichenfenster mit weißem Hintergrund.
  243. * @param titel Fensterueberschirft
  244. * @param breite Breite des Fensters
  245. * @param hoehe Hoehe des Fensters
  246. */
  247. public ZEICHENFENSTER(String titel, int breite, int hoehe)
  248. {
  249. this(titel, breite, hoehe, Color.black);
  250. }
  251.  
  252. /**
  253. * Erzeugt ein Zeichenfenster.
  254. * @param titel Fensterueberschirft
  255. * @param breite Breite des Fensters
  256. * @param hoehe Hoehe des Fensters
  257. * @param hintergrundFarbe Hintergrundfarbe des Zeichenfensters
  258. */
  259. private ZEICHENFENSTER(String titel, int breite, int hoehe, Color hintergrundFarbe)
  260. {
  261. frame = new JFrame();
  262. canvas = new CanvasPane();
  263. canvas.setPreferredSize(new Dimension(breite, hoehe));
  264. frame.getContentPane().add(canvas,BorderLayout.CENTER);
  265. JPanel p1=new JPanel();
  266. p1.setLayout(new BorderLayout());
  267. steuerungOst = new JPanel();
  268. steuerungSued = new JPanel();
  269. steuerungOst.setLayout(new BoxLayout(steuerungOst,BoxLayout.Y_AXIS));
  270. steuerungSued.setLayout(new BoxLayout(steuerungSued,BoxLayout.X_AXIS));
  271. p1.add(steuerungOst,BorderLayout.NORTH);
  272. frame.getContentPane().add(p1,BorderLayout.EAST);
  273. frame.getContentPane().add(steuerungSued,BorderLayout.SOUTH);
  274. frame.setTitle(titel);
  275. backgroundColor = hintergrundFarbe;
  276. frame.pack();
  277. zeige();
  278. }
  279.  
  280. public static ZEICHENFENSTER gibFenster()
  281. {
  282. if (singleton==null){singleton=new ZEICHENFENSTER("Das Zeichenfenster");}
  283. singleton.zeige();
  284. return singleton;
  285. }
  286.  
  287. /**
  288. * Macht das Zeichenfenster sichtbar bzw. setzt es in den Vordergrund,
  289. * falls es bereits sichtbar ist.
  290. */
  291. public void zeige()
  292. {
  293. if(graphic == null) {
  294. // nur beim ersten Aufruf wird der Hintergrund mit der Hintergrundfarbe
  295. // gefuellt
  296. Dimension size = canvas.getSize();
  297. canvasImage = canvas.createImage(size.width, size.height);
  298. graphic = (Graphics2D)canvasImage.getGraphics();
  299. graphic.setColor(backgroundColor);
  300. graphic.fillRect(0, 0, size.width, size.height);
  301. graphic.setColor(Color.black);
  302. }
  303. frame.setVisible(true);
  304. }
  305.  
  306. /**
  307. * Gibt Information über die Sichtbarkeit.
  308. * @return true falls das Fenster sichtbar ist.
  309. */
  310. public boolean istSichtbar()
  311. {
  312. return frame.isVisible();
  313. }
  314.  
  315. /**
  316. * Zeichnet einen Elipsenbogen (Siehe Graphics.drawArc)
  317. * @param x x-Koordinate des Elipsenmittelpunkts
  318. * @param y y-Koordinate des Elipsenmittelpunkts
  319. * @param halbachseX Halbachse der Elipse in x-Richtung
  320. * @param halbachseY Halbachse der Elipse in y-Richtung
  321. * @param startWinkel Polarwinkel, an dem der Bogen anfängt
  322. * @param winkel Polarwinkel, welchen der Bogen durchläuft
  323. */
  324. public void zeichneBogen(int x, int y, int halbachseX, int halbachseY, int startWinkel, int winkel)
  325. {
  326. graphic.drawArc(x-halbachseX,y-halbachseY,2*halbachseX,2*halbachseY,startWinkel,winkel);
  327. canvas.repaint();
  328. }
  329.  
  330. /**
  331. * Zeichnet einen Kreis (Siehe Graphics.drawOval)
  332. * @param x x-Koordinate des Mittelpunkts
  333. * @param y y-Koordinate des Mittelpunkts
  334. * @param radius Kreisradius
  335. */
  336. public void zeichneKreis(int x, int y, int radius)
  337. {
  338. graphic.drawOval(x-radius,y-radius,2*radius,2*radius);
  339. canvas.repaint();
  340. }
  341.  
  342. /**
  343. * Füllt das Innere eines Kreises mit der angegebenen Farbe.
  344. * @param x x-Koordinate des Mittelpunkts
  345. * @param y y-Koordinate des Mittelpunkts
  346. * @param radius Kreisradius
  347. * @param farbe Füllfarbe für den Kreis, erlaubt sind "weiss" "schwarz" "rot"
  348. * "gruen" "blau" "gelb" "magenta" "cyan" "grau"
  349. */
  350. public void fuelleKreis(int x, int y, int radius, String farbe)
  351. {
  352. Color original=graphic.getColor();
  353. graphic.setColor(farbeZuColor(farbe));
  354. graphic.fillOval(x-radius,y-radius,2*radius,2*radius);
  355. canvas.repaint();
  356. graphic.setColor(original);
  357. }
  358.  
  359. /**
  360. * Füllt das Innere eines Kreises mit der angegebenen Farbe.
  361. * @param x x-Koordinate des Mittelpunkts
  362. * @param y y-Koordinate des Mittelpunkts
  363. * @param radius Kreisradius
  364. * @param farbnr Füllfarbnummer für den Kreis (0 bis 8)
  365. */
  366. public void fuelleKreis(int x, int y, int radius, int farbnr)
  367. {
  368. Color original=graphic.getColor();
  369. graphic.setColor(farbeZuColor(farbnr));
  370. graphic.fillOval(x-radius,y-radius,2*radius,2*radius);
  371. canvas.repaint();
  372. graphic.setColor(original);
  373. }
  374.  
  375. /**
  376. * Löscht das Innere eines Kreises
  377. * @param x x-Koordinate des Mittelpunkts
  378. * @param y y-Koordinate des Mittelpunkts
  379. * @param radius Kreisradius
  380. */
  381. public void loescheKreis(int x, int y, int radius)
  382. {
  383. Ellipse2D.Double circle = new Ellipse2D.Double(x-radius, y-radius, 2*radius, 2*radius);
  384. loesche(circle);
  385. }
  386.  
  387. /**
  388. * Zeichnet den Umriss eines Shape-Objekts.
  389. * @param shape das Shape-Object, welches gezeichnet werden soll
  390. */
  391. public void zeichne(Shape shape)
  392. {
  393. graphic.draw(shape);
  394. canvas.repaint();
  395. }
  396.  
  397. /**
  398. * Füllt das Innere eines Shape-Objekts mit der angegebenen Farbe.
  399. * @param shape das Shape-Objekt, welches gefüllt werden soll
  400. * @param farbe Füllfarbe für das Shape-Objekt, erlaubt sind "weiss" "schwarz" "rot"
  401. * "gruen" "blau" "gelb" "magenta" "cyan" "grau"
  402. */
  403. public void fuelle(Shape shape, String farbe)
  404. {
  405. Color original=graphic.getColor();
  406. graphic.setColor(farbeZuColor(farbe));
  407. graphic.fill(shape);
  408. canvas.repaint();
  409. graphic.setColor(original);
  410. }
  411.  
  412. /**
  413. * Füllt das Innere eines Shape-Objekts mit der angegebenen Farbe.
  414. * @param shape das Shape-Objekt, welches gefüllt werden soll
  415. * @param farbnr Füllfarbnummer für das Shape-Objekt (0 bis 8)
  416. */
  417. public void fuelle(Shape shape, int farbnr)
  418. {
  419. Color original=graphic.getColor();
  420. graphic.setColor(farbeZuColor(farbnr));
  421. graphic.fill(shape);
  422. canvas.repaint();
  423. graphic.setColor(original);
  424. }
  425.  
  426. /**
  427. * Löscht das Innere eines Shape-Objekts.
  428. * @param shape das Shape-Object, welches gelöscht werden soll
  429. */
  430. public void loesche(Shape shape)
  431. {
  432. Color original = graphic.getColor();
  433. graphic.setColor(backgroundColor);
  434. graphic.fill(shape); // erase by filling background color
  435. graphic.setColor(original);
  436. canvas.repaint();
  437. }
  438.  
  439. /**
  440. * Zeichnet den Rand des Rechtecks mit der aktuellen Farbe.
  441. * @param xPos,yPos Koordinaten der linken oberen Ecke
  442. * @param breite, hoehe Breite und Höhe des Rechtecks
  443. */
  444. public void zeichneRechteck(int xPos, int yPos, int breite, int hoehe)
  445. {
  446. graphic.drawRect(xPos, yPos, breite, hoehe);
  447. canvas.repaint();
  448. // fill(new Rectangle(xPos, yPos, breite, hoehe));
  449. }
  450.  
  451. /**
  452. * Füllt das Innere des Rechtecks mit der angegebenen Farbe.
  453. * @param xPos,yPos Koordinaten der linken oberen Ecke
  454. * @param breite, hoehe Breite und Höhe des Rechtecks
  455. * @param farbe Füllfarbe für das Rechteck, erlaubt sind "weiss" "schwarz" "rot"
  456. * "gruen" "blau" "gelb" "magenta" "cyan" "grau"
  457. */
  458. public void fuelleRechteck(int xPos, int yPos, int breite, int hoehe, String farbe)
  459. {
  460. Color original=graphic.getColor();
  461. graphic.setColor(farbeZuColor(farbe));
  462. graphic.fillRect(xPos, yPos, breite, hoehe);
  463. canvas.repaint();
  464. graphic.setColor(original);
  465. }
  466.  
  467. /**
  468. * Füllt das Innere des Rechtecks mit der angegebenen Farbe.
  469. * @param xPos,yPos Koordinaten der linken oberen Ecke
  470. * @param breite, hoehe Breite und Höhe des Rechtecks
  471. * @param farbnr Füllfarbnummer für das Rechteck (0 bis 8)
  472. */
  473. public void fuelleRechteck(int xPos, int yPos, int breite, int hoehe, int farbnr)
  474. {
  475. Color original=graphic.getColor();
  476. graphic.setColor(farbeZuColor(farbnr));
  477. graphic.fillRect(xPos, yPos, breite, hoehe);
  478. canvas.repaint();
  479. graphic.setColor(original);
  480. }
  481.  
  482. /**
  483. * Löscht das Innere eines Rechtecks.
  484. * @param xPos,yPos Koordinaten der linken oberen Ecke
  485. * @param breite, hoehe Breite und Höhe des Rechtecks
  486. */
  487. public void loescheRechteck(int xPos, int yPos, int breite, int hoehe)
  488. {
  489. loesche(new Rectangle(xPos, yPos, breite, hoehe));
  490. }
  491.  
  492. private Polygon gibDreieck(int x1, int y1, int x2, int y2, int x3, int y3)
  493. {
  494. Polygon p=new Polygon();
  495. p.addPoint(x1,y1);
  496. p.addPoint(x2,y3);
  497. p.addPoint(x3,y3);
  498. return p;
  499. }
  500.  
  501. /**
  502. * Zeichnet den Rand eines Dreiecks mit der aktuellen Farbe.
  503. * @param x1,y1 Koordinaten des ersten Eckpunkts
  504. * @param x2,y2 Koordinaten des zweiten Eckpunkts
  505. * @param x3,y3 Koordinaten des dritten Eckpunkts
  506. */
  507. public void zeichneDreieck(int x1, int y1, int x2, int y2, int x3, int y3)
  508. {
  509. graphic.drawPolygon(gibDreieck(x1, y1, x2, y2, x3, y3));
  510. canvas.repaint();
  511. }
  512.  
  513. /**
  514. * Füllt das Innere eines Dreiecks mit der angegebenen Farbe.
  515. * @param x1,y1 Koordinaten des ersten Eckpunkts
  516. * @param x2,y2 Koordinaten des zweiten Eckpunkts
  517. * @param x3,y3 Koordinaten des dritten Eckpunkts
  518. * @param farbe Füllfarbe für das Dreieck, erlaubt sind "weiss" "schwarz" "rot"
  519. * "gruen" "blau" "gelb" "magenta" "cyan" "grau"
  520. */
  521. public void fuelleDreieck(int x1, int y1, int x2, int y2, int x3, int y3, String farbe)
  522. {
  523. Color original=graphic.getColor();
  524. graphic.setColor(farbeZuColor(farbe));
  525. graphic.fillPolygon(gibDreieck(x1, y1, x2, y2, x3, y3));
  526. canvas.repaint();
  527. graphic.setColor(original);
  528. }
  529.  
  530. /**
  531. * Füllt das Innere eines Dreiecks mit der angegebenen Farbe.
  532. * @param x1,y1 Koordinaten des ersten Eckpunkts
  533. * @param x2,y2 Koordinaten des zweiten Eckpunkts
  534. * @param x3,y3 Koordinaten des dritten Eckpunkts
  535. * @param farbnr Füllfarbnummer für das Dreieck (0 bis 8)
  536. */
  537. public void fuelleDreieck(int x1, int y1, int x2, int y2, int x3, int y3, int farbnr)
  538. {
  539. Color original=graphic.getColor();
  540. graphic.setColor(farbeZuColor(farbnr));
  541. graphic.fillPolygon(gibDreieck(x1, y1, x2, y2, x3, y3));
  542. canvas.repaint();
  543. graphic.setColor(original);
  544. }
  545.  
  546. /**
  547. * Löscht das Innere eines Dreicks
  548. * @param x1,y1 Koordinaten des ersten Eckpunkts
  549. * @param x2,y2 Koordinaten des zweiten Eckpunkts
  550. * @param x3,y3 Koordinaten des dritten Eckpunkts
  551. */
  552. public void loescheDreieck(int x1, int y1, int x2, int y2, int x3, int y3)
  553. {
  554. loesche(gibDreieck(x1, y1, x2, y2, x3, y3));
  555. }
  556.  
  557. /**
  558. * Löscht den Inhalt des Zeichenfensters.
  559. */
  560. public void loescheAlles()
  561. {
  562. Color original = graphic.getColor();
  563. graphic.setColor(backgroundColor);
  564. Dimension size = canvas.getSize();
  565. graphic.fill(new Rectangle(0, 0, size.width, size.height));
  566. graphic.setColor(original);
  567. canvas.repaint();
  568. }
  569.  
  570. /**
  571. * Löscht den Umriss eines Shape-Objekts.
  572. * @param shape das Shape-Object, dessen Umriss gelöscht werden soll
  573. */
  574. public void loescheRand(Shape shape)
  575. {
  576. Color original = graphic.getColor();
  577. graphic.setColor(backgroundColor);
  578. graphic.draw(shape); // Löschen durch übermalen mit Hintergrundfarbe
  579. graphic.setColor(original);
  580. canvas.repaint();
  581. }
  582.  
  583. /**
  584. * Zeichnet ein Bild in das Zeichnenfenster .
  585. * @param bild das anzuzeigende Bild
  586. * @param x x-Koordinate des linken Bildrands
  587. * @param y y-Koordinate des oberen Bildrands
  588. * @return gibt eines booleschen Wert zurück, der angibt, ob das Bild vollständig geladen
  589. * werden konnte
  590. */
  591. public boolean zeichneBild(Image bild, int x, int y)
  592. {
  593. boolean result = graphic.drawImage(bild, x, y, null);
  594. canvas.repaint();
  595. return result;
  596. }
  597.  
  598. /**
  599. * Zeichnet einen Text.
  600. * @param text die anzuzeigende Zeichenkette
  601. * @param x x-Koordinate des linken Rands
  602. * @param y y-Koordinate des oberen Rands
  603. */
  604. public void zeichneText(String text, int x, int y)
  605. {
  606. graphic.drawString(text, x, y);
  607. canvas.repaint();
  608. }
  609.  
  610. /**
  611. * Löscht einen Text vom Zeichenfenster.
  612. * @param text die zu löschende Zeichenkette
  613. * @param x x-Koordinate des linken Rands
  614. * @param y y-Koordinate des oberen Rands
  615. */
  616. public void loescheText(String text, int x, int y)
  617. {
  618. Color original = graphic.getColor();
  619. graphic.setColor(backgroundColor);
  620. graphic.drawString(text, x, y);
  621. graphic.setColor(original);
  622. canvas.repaint();
  623. }
  624.  
  625. /**
  626. * Zeichnet eine Strecke ins Zeichenfenster.
  627. * @param x1 x-Koordinate des Anfangspunkts der Strecke
  628. * @param y1 y-Koordinate des Anfangspunkts der Strecke
  629. * @param x2 x-Koordinate des Endpunkts der Strecke
  630. * @param y2 y-Koordinate des Endpunkts der Strecke
  631. */
  632. public void zeichneStrecke(int x1, int y1, int x2, int y2)
  633. {
  634. graphic.drawLine(x1, y1, x2, y2);
  635. canvas.repaint();
  636. }
  637.  
  638. /**
  639. * Setzt die Vordergrundfarbe des Zeichenfensters.
  640. * @param neueFarbe neue Vordergrundfarbe
  641. */
  642. public void setzeVordergrundFarbe(String neueFarbe)
  643. {
  644. graphic.setColor(farbeZuColor(neueFarbe));
  645. }
  646. private void setzeVordergrundFarbe(Color neueFarbe)
  647. {
  648. graphic.setColor(neueFarbe);
  649. }
  650.  
  651. private Color farbeZuColor(int farbnr)
  652. {
  653. switch (farbnr)
  654. {
  655. case 0: return Color.black;
  656. case 1: return Color.blue;
  657. case 2: return Color.green;
  658. case 3: return Color.cyan;
  659. case 4: return Color.red;
  660. case 5: return Color.magenta;
  661. case 6: return Color.yellow;
  662. case 7: return Color.gray;
  663. case 8: return Color.white;
  664. default: return graphic.getColor();
  665. }
  666.  
  667. }
  668.  
  669. private Color farbeZuColor(String farbe)
  670. {
  671. if (farbe=="weiss") return Color.white;
  672. if (farbe=="schwarz") return Color.black;
  673. if (farbe=="rot") return Color.red;
  674. if (farbe=="gruen") return Color.green;
  675. if (farbe=="blau") return Color.blue;
  676. if (farbe=="gelb") return Color.yellow;
  677. if (farbe=="magenta") return Color.magenta;
  678. if (farbe=="cyan") return Color.cyan;
  679. if (farbe=="grau") return Color.gray;
  680. return graphic.getColor();
  681. }
  682.  
  683. private String colorZuFarbe(Color color)
  684. {
  685. if (color==Color.white) return "weiss";
  686. if (color==Color.black) return "schwarz";
  687. if (color==Color.red) return "rot";
  688. if (color==Color.green) return "gruen";
  689. if (color==Color.blue) return "blau";
  690. if (color==Color.yellow) return "gelb";
  691. if (color==Color.magenta) return "magenta";
  692. if (color==Color.cyan) return "cyan";
  693. if (color==Color.gray) return "gruen";
  694. return "";
  695. }
  696.  
  697. /**
  698. * Gibt die aktuelle Vordergrundfarbe des Zeichenfensters zurück.
  699. * @return die aktuelle Vordergrundfarbe
  700. */
  701. public String gibVordergrundFarbe()
  702. {
  703. return colorZuFarbe(graphic.getColor());
  704. }
  705. //public Color gibVordergrundFarbe()
  706. //{
  707. // return graphic.getColor();
  708. //}
  709.  
  710. /**
  711. * Setzt die Hintergrundfarbe des Zeichenfensters.
  712. * @param neueFarbe neue Hintergrundfarbe
  713. */
  714. public void setzeHintergrundFarbe(String neueFarbe)
  715. {
  716. backgroundColor = farbeZuColor(neueFarbe);
  717. graphic.setBackground(backgroundColor);
  718. }
  719. private void setzeHintergrundFarbe(Color neueFarbe)
  720. {
  721. backgroundColor = neueFarbe;
  722. graphic.setBackground(neueFarbe);
  723. }
  724.  
  725. /**
  726. * Gibt die aktuelle Hintergrundfarbe des Zeichenfensters zurück.
  727. * @return die aktuelle Hintergrundfarbe
  728. */
  729. public String gibHintergrundFarbe()
  730. {
  731. return colorZuFarbe(backgroundColor);
  732. }
  733. //public Color gibHintergrundFarbe()
  734. //{
  735. // return backgroundColor;
  736. //}
  737.  
  738. /**
  739. * Ändert den aktuellen Zeichensatz des Zeichenfensters.
  740. * @param neuerZeichensatz Zeichensatz, der künftig für Zeichenkettenausgaben verwendet wird
  741. */
  742. public void setzeZeichensatz(Font neuerZeichensatz)
  743. {
  744. graphic.setFont(neuerZeichensatz);
  745. }
  746.  
  747. /**
  748. * Gibt den aktuellen Zeichensatz des Zeichenfensters zurück.
  749. * @return den aktuellen Zeichensatz
  750. **/
  751. public Font gibZeichensatz()
  752. {
  753. return graphic.getFont();
  754. }
  755.  
  756. /**
  757. * Ändert die Abmessungen des Zeichenfensters.
  758. * @param breite neue Breite
  759. * @param hoehe neue Höhe
  760. */
  761. public void setzeMasse(int breite, int hoehe)
  762. {
  763. canvas.setPreferredSize(new Dimension(breite, hoehe));
  764. Image oldImage = canvasImage;
  765. canvasImage = canvas.createImage(breite, hoehe);
  766. graphic = (Graphics2D)canvasImage.getGraphics();
  767. graphic.drawImage(oldImage, 0, 0, null);
  768. frame.pack();
  769. }
  770.  
  771. /**
  772. * Gibt die Abmessungen des Zeichenfensters zurück.
  773. * @return die aktuellen Abmessungen des Zeichenfensters
  774. */
  775. public Dimension gibMasse()
  776. {
  777. return canvas.getSize();
  778. }
  779.  
  780. /**
  781. * Wartet eine bestimmte Zeit.
  782. * Eine kurze Verzögerung kann z. B. für Animationen verwendet werden.
  783. * @param zeit Wartezeit in Millisekunden
  784. */
  785. public void warte(int zeit)
  786. {
  787. try
  788. {
  789. Thread.sleep(zeit);
  790. }
  791. catch (InterruptedException e)
  792. {
  793. // ignoring exception at the moment
  794. }
  795. }
  796.  
  797. /**
  798. * Fügt ein weiteres Steuerungselement in die rechte Steuerungsleiste ein.
  799. * @param element Das einzufügende Steuerungselement muss aus JComponent abgeleitet
  800. * sein. z. B. JButton, JComboBox.
  801. */
  802. public void komponenteHinzufuegen(JComponent element, String position)
  803. {
  804. if (position=="rechts") steuerungOst.add(element);
  805. else if (position=="unten") steuerungSued.add(element);
  806. frame.pack();
  807. }
  808.  
  809. /**
  810. * Beschriftet den Titel des Zeichenfensters neu.
  811. * @param titelNeu Text der neuen Fensterüberschrift
  812. */
  813. public void setzeTitel(String titelNeu)
  814. {
  815. frame.setTitle(titelNeu);
  816. }
  817.  
  818. /************************************************************************
  819. * Nested class CanvasPane - the actual canvas component contained in the
  820. * Canvas frame. This is essentially a JPanel with added capability to
  821. * refresh the image drawn on it.
  822. */
  823. private class CanvasPane extends JPanel
  824. {
  825. private static final long serialVersionUID = 20060330L;
  826.  
  827. public void paint(Graphics g)
  828. {
  829. g.drawImage(canvasImage, 0, 0, null);
  830. }
  831. }
  832. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement