Initial commit
|
@ -0,0 +1,78 @@
|
|||
# Generelle Informationen
|
||||
- Englische Kommentare Stammen von mir
|
||||
- Ich hab sehr viel Zeit zuhause verbracht die Sprache zu lernen und verstehen
|
||||
|
||||
### Das Programm bietet folgende Funktionen:
|
||||
- Speichern und Öffnen der Möbelobjekte
|
||||
- Selektion von Möbelobjekten durch einfaches klicken
|
||||
- Bewegen der Möbelstücke durch Drag and Drop
|
||||
- Durch scrollen können Möbelobjekte rotiert werden
|
||||
- Bei betätigter STRG-Taste beim scrollen kann die Gräöße verändert werden
|
||||
- Alle manuell veränderten Attribute können auf den Standardwwer zurückgesetzt werden
|
||||
- Menüleiste / Menubar mit Icons
|
||||
- Das Ändern der Farbe eines Möbelonjekts
|
||||
- Und vieles mehr...
|
||||
|
||||
(In den Logbucheinträgen sind nicht alle Details festgehalten; die meisten Erklärungen befinden sich im Code selbst)
|
||||
# Logbucheinträge
|
||||
## Donnerstag 09/01/2020 Informatikunterricht
|
||||
Änderungen:
|
||||
- Farbänderungen bei Auswahl von einem Objekt zu schwarz
|
||||
|
||||
Heute habe ich versucht die Funktion des Fabwechsels mit einhergehender Auswahl fertigzustellen. Diese sind noch nicht voll funktionsfähig.
|
||||
Leider bin ich an einen Kopellierfehler hängen geblieben. Ich habe noch nicht ganz verstanden, was den Fehler auslöst.
|
||||
Deshalb bin ich leider etwas frustriert.
|
||||
|
||||
## 1Donnerstag 6/01/2020 Informatikunterricht
|
||||
Den Fehler aus der letzen Unterricht konnte ich beheben: Der Fehler lag in der Änderung der Farbe, welche ohne weiteres nicht direkt geändert werden konnte. Deshalb hab ich eine Methode zum Ändern der Farbe in der Furniture Klasse erstellt. Ich habe eine neue Variable erstellt, welche den Farbzustand des Objekts speichert, sodass ich bei der Deselektion des Objekts den vorherigen Farbzustand wiederherstellen kann.
|
||||
|
||||
|
||||
Den Rest der Unterrichtszeit habe ich damit verbracht mir anzuschauen wie die JMenuBar funktioniert und die Bar in meinem Programm implementiert.
|
||||
Dabei habe ich noch nicht ganz verstanden, wie die dahinterstehende Funktion ausgelöst wird. In den Beispielen wird nämlich eine kompliziert aussehende eigene Klasse erstellt, welche mit einem ActionListener erweitert wird. Die JMenuItems führen also noch nichts aus und sind somit "dumm".
|
||||
|
||||
|
||||
## Dienstag 21/01/2020
|
||||
|
||||
Ich habe heute die grundlegenden Funktionen der GUI.java in Leinwand.java implementiert (sprich main function und JPanel).
|
||||
Da die Funktionen von dem vorherigen JButton Panel (u.a. Objekte erzeugen und Objekte bewegen) nicht mehr benötigt werden und diese Funktionalität durch JMenuBAr übernommen wurde. Bei dem Prozess musste ich einige Methoden anpassen oder teilweise auch neue, bessere oder effizientere Methoden schreiben.
|
||||
Bei der Übernahme hatte ich teilweise Probleme mit statischen und nicht-statischen Funktionen und da häufig eine Kette von Methoden voneinander abhängig ist wirkt sich eine Veränderung (z.B. Veränderung von statisch zu nicht-statisch) sofort auf alle anderen aus. Dies macht die Fehlersuche schwierig.
|
||||
|
||||
## Datum unbekannt: Ab hier habe ich mich regelmäßig nachmittags hingesetzt und weitergearbeitetet
|
||||
Ich habe heute ziemlich viel Zeit mit der Drag and Drop Funktion verrbracht. Die Schwierigkeit liegt in dem sofortigen verschieben des Objects, da mit einer Bewegung der Maus auch eine Bewegung des dementsprechenden Objekts auslösen soll. Ich habe mich an einem Codeblock von Stackoverflow orientiert, welchen ich auch als Demo in meinem Programm eingebaut habe. Jedoch musste ich noch eine Menge Modifikationen durchführen, um diesen mit meinen Raumplaner funktioniert, da der Codeblock davon ausgeht, dass das verschobene Objekt nach dem verschieben an einer neuen Stelle ist. Beim Raumplaner kann man allerdings den ActionListener nur zur Leinwand hinzufügen und da sich dieses Objekt nicht bewegt musste ich eine Veränderung duchführen. Beim Klicken wird die Position des Möbelstücks sowie die Position des Cursors in einer Variable festgehalten. Sobald man die Maus bewegt wird die neue Position des ausgewählten Objekts wie folgt festgelegt: Position des Objekts beim Klickvorgang + Aktuelle Cursorposition - Cursorposition beim Klickvorgang. Somit wird also im Prinzip die Differenz der von der Maus zurückgelegten Strecke zu der Position addiert/subtrahiert. Und dies passiert bei jedem Mal, wenn der MouseListener eine Bewegung registriert - so kommt das Drag and Drop Gefühl zu Stande.
|
||||
|
||||
## Neuer Tag (Datum unbekannt)
|
||||
Ich habe heute den ColorChanger implementiert, dabei habe ich ein kurzes YouTube Tutorial geschaut und danach verstanden wie dies funktioniert.
|
||||
Um die Farbe auch abspeichern zu können, habe ich den Typ des color Attributs von Typ String auf Typ Color geändert.
|
||||
Damit man wieder auf die ursprüngliche Farbe zurückkehren kann, speichert jedes Objekt zudem noch die Farbe beim erstellen des Objekts.
|
||||
|
||||
Zudem habe ich heute den MouseWheelListener so implementiert, sodass bei Bewegung des Mausrads das Objekt gedreht wird. Die Idee stammt von dem Raumplaner, welcher im Informatik Ordner als Vorbild abgelegt war. Der MouseWheelListener gibt entweder 1 oder -1 aus, abhängig von der Richtung. Diesen Wert kann ich einfach mit 10 multiplizieren und diesen dann zum aktuellen Rotationswert addiereDamit auch die Größe geändert werden kann habe ich die einfache Option hinzugeüft, dass beim gedrückt halten des STRG-Knopfes die Größe entweder vergrößert oder verkleinert wird. Dabei wird der aktuelle Größenwert des Objekts durch 10 geteilt und dann entweder addiert oder subtrahiert. Das bedeutet also, dass entweder 10 addiert oder subtrahiert wird.
|
||||
|
||||
|
||||
## Neuer Tag (Datum unbekannt)
|
||||
Heute habe ich die Speicherfunktion implementiert. Zur Hilfe habe ich einige Anleitungen sowie Beispiele mir angeschaut.
|
||||
Dabei habe ich auch einen Codeblock übernommen und stark modifiziert, sodass er passt. Vor dem Speichern wird überprüft, ob es eine derartige Datei bereits gibt und es wird eine Dateiendung angehängt, sofern dies nicht bereits von Nutzer passiert ist. Zudem müssen hier einige try/catch Blöcke verwendet werden, damit bei einem Fehler das Programm nicht abstürzt, sondern nur die jeweilige Funktion mit einem Fehler beendet. Beim Speichern werden einfach alle Objekte mit einem for-loop abgespeichert. Das Speichern funktionierte am Ende auch taddellos, allerdings wird beim laden nur das erste Objekt geladen.
|
||||
|
||||
|
||||
|
||||
|
||||
## Freitag 21/02/2020
|
||||
Ich konnte heute den Fehler des Abspeicherns lösen: Der EOFFile Boolean löste zu früh aus und somit wurde immmer nur das erste Objekt importiert. Durch Veränderung des Mechanismus zur Erkennung, ob die Datei zu Ende ist konnte ich das Problem lösen.
|
||||
Heute habe ich das Aussehen etwas durch das hinzufügen von Icons aufpoliert. Damit die Icons nicht gigantisch angezeigt werden, musste ich mithilfe eines Codeblocks von Stackoverflow die Icons vorher auf die richtige Auflösung herunterskalieren. Die Icons verbessern das Erlebnis erheblich.
|
||||
Zudem habe ich noch ein About Reiter sowie die MouseMovementDemo in mein Programm integriert.
|
||||
|
||||
|
||||
# Verwendete Hilfestellung
|
||||
Dabei habe ich mithilfe folgender (Video-) Tutorials gearbeitet und teilweise Code Blöcke daraus übernommen:
|
||||
|
||||
* Die offiziellen Java Dokumentation [Java Docs](https://docs.oracle.com/javase/7/docs/api/)
|
||||
* Dieses Video half mit mit dem JMenu [Youtube](https://www.youtube.com/watch?v=HY8jYkiBPU8)
|
||||
* Ich habe den Codeblock vom MouseListener hier entnommen: [Stackoverflow](https://stackoverflow.com/questions/9036730/)
|
||||
* Hilfe mit dem JFileChooser und Codeblöcke [Youtube](https://www.youtube.com/watch?v=rOrJ5TGzyeA)
|
||||
* Hilfe mit dem JFileChooser-Filtering [Stackoverflow](https://stackoverflow.com/questions/18575655/)
|
||||
* Codeblock für die infoBox [Stackoverflow](https://stackoverflow.com/questions/7080205/)
|
||||
* Zum speichern in einer Datei von [Coderanch](https://coderanch.com/t/524426/java/save-array-file)
|
||||
* Grundlegende Erkennung der MouseRadRotation abgeschaut von [Stackoverflow](https://stackoverflow.com/questions/35259118/)
|
||||
* Filefilter, welcher die Endung anhängt: [Stackoverflow](https://stackoverflow.com/questions/8713968/)
|
||||
* Hilfe mit der JMenuBar von [Stackoverflow](https://stackoverflow.com/questions/12527092/)
|
||||
* Dieser Codeblock transformiert die Icons auf die richtige Größe [Stackoverflow](https://stackoverflow.com/questions/25593949/)
|
||||
* Die Icons stammen von [Icons8](https://icons8.com)
|
|
@ -0,0 +1,66 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.geom.Line2D;
|
||||
|
||||
/**
|
||||
* Ein Bett, das manipuliert werden kann und sich selbst auf einer Leinwand
|
||||
* zeichnet.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.3 (sept 07)
|
||||
*/
|
||||
public class Bed extends Furniture {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Erzeuge ein neues Bett mit einer Standardfarbe und Standardgroesse an einer
|
||||
* Standardposition. (Standardkonstruktor)
|
||||
*/
|
||||
public Bed() {
|
||||
xPosition = 10;
|
||||
yPosition = 180;
|
||||
color = Color.magenta;
|
||||
orientation = 0;
|
||||
isVisible = false;
|
||||
width = 200;
|
||||
depth = 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeuge ein neues Bett.
|
||||
*/
|
||||
public Bed(int xPosition, int yPosition, Color color, int orientation, int width, int depth) {
|
||||
this.xPosition = xPosition;
|
||||
this.yPosition = yPosition;
|
||||
this.color = color;
|
||||
this.currentColor = color; // the default color is set to the initial color
|
||||
this.defaultColor = color; // the default color is set to the initial color
|
||||
this.defaultWidth = width; // the default width is set to the initial width
|
||||
this.defaultDepth = depth; // the default width is set to the initial depth
|
||||
this.orientation = orientation;
|
||||
isVisible = false;
|
||||
this.width = width;
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das zu zeichnende Shape anhand der gegebenen Daten [ Zum Anzeigen
|
||||
* der Attributwerte ueber Inspect muss hier die Sichtbarkeit auf public gesetzt
|
||||
* werden. ]
|
||||
*/
|
||||
protected Shape getCurrentFigure() {
|
||||
// einen GeneralPath definieren
|
||||
GeneralPath bed = new GeneralPath();
|
||||
Rectangle2D frame = new Rectangle2D.Double(0, 0, width, depth);
|
||||
bed.append(frame, false);
|
||||
Line2D fold1 = new Line2D.Double(0, 0, width, depth / 2);
|
||||
bed.append(fold1, false);
|
||||
Line2D mid = new Line2D.Double(0, depth / 2, width, depth / 2);
|
||||
bed.append(mid, false);
|
||||
Line2D fold2 = new Line2D.Double(0, depth, width, depth / 2);
|
||||
bed.append(fold2, false);
|
||||
return transform(bed);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,671 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
import java.awt.event.*;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JColorChooser;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
/**
|
||||
* Leinwand ist eine Klasse, die einfache Zeichenoperationen auf einer
|
||||
* leinwandartigen Zeichenfl<EFBFBD>che erm<EFBFBD>glicht. Sie ist eine vereinfachte Version
|
||||
* der Klasse Canvas (englisch f<EFBFBD>r Leinwand) des JDK und wurde speziell f<EFBFBD>r das
|
||||
* Projekt "Figuren" geschrieben.
|
||||
*
|
||||
*
|
||||
* @author: Bruce Quig
|
||||
* @author: Michael K<EFBFBD>lling (mik)
|
||||
* @author: Axel Schmolitzky
|
||||
*
|
||||
* @author: <EFBFBD>nderungen von
|
||||
* @Java-MS Groupies
|
||||
* @hier: Uwe Debacher
|
||||
*
|
||||
* @version: 3.0 (2020/02/18)
|
||||
*
|
||||
* Finale Änderungen durch:
|
||||
* @author: Kevin Dorner
|
||||
*/
|
||||
public class Canvas {
|
||||
// Hinweis: Die Implementierung dieser Klasse (insbesondere die
|
||||
// Verwaltung der Farben und Identit<EFBFBD>ten der Figuren) ist etwas
|
||||
// komplizierter als notwendig. Dies ist absichtlich so, weil damit
|
||||
// die Schnittstellen und Exemplarvariablen der Figuren-Klassen
|
||||
// f<EFBFBD>r den Lernanspruch dieses Projekts einfacher und klarer
|
||||
// sein k<EFBFBD>nnen.
|
||||
|
||||
private static Canvas leinwandSingleton;
|
||||
public static ArrayList<Furniture> allFurniture = new ArrayList<>(); // array that contains all furniture
|
||||
private volatile int draggedAtX, draggedAtY; // variables for drag and drop
|
||||
|
||||
/**
|
||||
* Fabrikmetlor color;hode, die eine Referenz auf das einzige Exemplar dieser
|
||||
* Klasse zur<EFBFBD>ckliefert. Wenn es von einer Klasse nur genau ein Exemplar gibt,
|
||||
* wird dieses als 'Singleton' bezeichnet.
|
||||
*/
|
||||
public static Canvas getCanvas() {
|
||||
if (leinwandSingleton == null) {
|
||||
leinwandSingleton = new Canvas("Furniture Planner", Color.white);
|
||||
|
||||
}
|
||||
leinwandSingleton.setVisibility(true);
|
||||
return leinwandSingleton;
|
||||
}
|
||||
|
||||
// ----- Exemplarvariablen -----
|
||||
|
||||
private JFrame window;
|
||||
private DrawingSurface drawingSurface;
|
||||
private Graphics2D graphic;
|
||||
private Color backgroundcolor;
|
||||
private Image canvasImage;
|
||||
private List figures;
|
||||
private Map figureToShape; // Abbildung von Figuren zu Shapes
|
||||
private Furniture selectedFurniture = null; // used to track the selected furniture
|
||||
private Furniture previousSelectedFurniture = null; // used to track the previously selected furniture
|
||||
private volatile int selectedFurniturePosX, selectedFurniturePosY;
|
||||
|
||||
/**
|
||||
* Erzeuge eine Leinwand.
|
||||
*
|
||||
* @param title Titel, der im Rahmen der Leinwand angezeigt wird
|
||||
* @param width die gew<EFBFBD>nschte Breite der Leinwand
|
||||
* @param height die gew<EFBFBD>nschte H<EFBFBD>he der Leinwand
|
||||
* @param baseColor die Hintergrundfarbe der Leinwand
|
||||
*/
|
||||
private Canvas(String title, Color baseColor) {
|
||||
window = new JFrame();
|
||||
drawingSurface = new DrawingSurface();
|
||||
int width = 600;
|
||||
int height = 600;
|
||||
window.setTitle(title);
|
||||
window.setLocationRelativeTo(null); // spawns the window in the center of the screen
|
||||
drawingSurface.setPreferredSize(new Dimension(width, height));
|
||||
window.setLayout(new BorderLayout());
|
||||
window.add(drawingSurface, BorderLayout.CENTER);
|
||||
backgroundcolor = baseColor;
|
||||
window.pack();
|
||||
figures = new ArrayList(); // contains the figure objects in an array
|
||||
figureToShape = new HashMap(); // contains the shapes
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exits when window is closed
|
||||
createmenubar(); // adds menubar
|
||||
MouseListener(); // adds MouseListener for drag and drop functionality
|
||||
WheelListener(); // adds WheelListener for resize/rotate functionality
|
||||
}
|
||||
|
||||
/**
|
||||
* Setze, ob diese Leinwand sichtbar sein soll oder nicht. Wenn die Leinwand
|
||||
* sichtbar gemacht wird, wird ihr Fenster in den Vordergrund geholt. Diese
|
||||
* Operation kann auch benutzt werden, um ein bereits sichtbares Leinwandfenster
|
||||
* in den Vordergrund (vor andere Fenster) zu holen.
|
||||
*
|
||||
* @param sichtbar boolean f<EFBFBD>r die gew<EFBFBD>nschte Sichtbarkeit: true f<EFBFBD>r sichtbar,
|
||||
* false f<EFBFBD>r nicht sichtbar.
|
||||
*/
|
||||
public void setVisibility(boolean sichtbar) {
|
||||
if (graphic == null) {
|
||||
// erstmaliger Aufruf: erzeuge das Bildschirm-Image und f<EFBFBD>lle
|
||||
// es mit der Hintergrundfarb e
|
||||
Dimension size = drawingSurface.getSize();
|
||||
canvasImage = drawingSurface.createImage(size.width, size.height);
|
||||
graphic = (Graphics2D) canvasImage.getGraphics();
|
||||
graphic.setColor(backgroundcolor);
|
||||
graphic.fillRect(0, 0, size.width, size.height);
|
||||
graphic.setColor(Color.black);
|
||||
}
|
||||
window.setVisible(sichtbar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeichne f<EFBFBD>r das gegebene Figur-Objekt eine Java-Figur (einen Shape) auf die
|
||||
* Leinwand.
|
||||
*
|
||||
* @param figure das Figur-Objekt, f<EFBFBD>r das ein Shape gezeichnet werden soll
|
||||
* @param color die Farbe der Figur
|
||||
* @param shape ein Objekt der Klasse Shape, das tats<EFBFBD>chlich gezeichnet wird
|
||||
*/
|
||||
public void drawFigure(Object figure, Color color, Shape shape) {
|
||||
figures.remove(figure); // entfernen, falls schon eingetragen
|
||||
figures.add(figure); // am Ende hinzuf<EFBFBD>gen
|
||||
figureToShape.put(figure, new ShapeWithColor(shape, color));
|
||||
reDraw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Entferne die gegebene Figur von der Leinwand.
|
||||
*
|
||||
* @param figure die Figur, deren Shape entfernt werden soll
|
||||
*/
|
||||
public void removeFigure(Object figure) {
|
||||
figures.remove(figure); // entfernen,falls schon eingetragen
|
||||
figureToShape.remove(figure);
|
||||
reDraw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Warte f<EFBFBD>r die angegebenen Millisekunden. Mit dieser Operation wird eine
|
||||
* Verz<EFBFBD>gerung definiert, die f<EFBFBD>r animierte Zeichnungen benutzt werden kann.
|
||||
*
|
||||
* @param milliseconds die zu wartenden Millisekunden
|
||||
*/
|
||||
public void wait(int milliseconds) {
|
||||
try {
|
||||
Thread.sleep(milliseconds);
|
||||
} catch (Exception e) {
|
||||
// Exception ignorieren
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeichne erneut alle Figuren auf der Leinwand.
|
||||
*/
|
||||
private void reDraw() {
|
||||
deleteCanvas();
|
||||
for (Iterator i = figures.iterator(); i.hasNext();) {
|
||||
((ShapeWithColor) figureToShape.get(i.next())).draw(graphic);
|
||||
}
|
||||
drawingSurface.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* L<EFBFBD>sche die gesamte Leinwand.
|
||||
*/
|
||||
private void deleteCanvas() {
|
||||
Color original = graphic.getColor();
|
||||
graphic.setColor(backgroundcolor);
|
||||
Dimension size = drawingSurface.getSize();
|
||||
graphic.fill(new Rectangle(0, 0, size.width, size.height));
|
||||
graphic.setColor(original);
|
||||
}
|
||||
|
||||
public void MouseListener() {
|
||||
drawingSurface.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) { // When the mouse button is pressed the x and y values are parsed to
|
||||
// variable
|
||||
draggedAtX = e.getX(); // remembers position when mouse button is pressed
|
||||
draggedAtY = e.getY(); // remembers position when mouse button is pressed
|
||||
if (allFurniture.size() != 0) { // only executes once the array is not empty
|
||||
objectClickCheck();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
drawingSurface.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
selectedFurniture.setPos(selectedFurniturePosX + e.getX() - draggedAtX, //
|
||||
selectedFurniturePosY + e.getY() - draggedAtY);
|
||||
// DEBUG:
|
||||
System.out.println("x: " + (selectedFurniturePosX + e.getX() - draggedAtX) + " y: "
|
||||
+ (selectedFurniturePosY + e.getY() - draggedAtY));
|
||||
System.out.println("x calc: " + selectedFurniturePosX + " + " + e.getX() + " - " + draggedAtX);
|
||||
System.out.println("y calc: " + selectedFurniturePosY + " + " + e.getY() + " - " + draggedAtY);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void WheelListener() {
|
||||
drawingSurface.addMouseWheelListener(new MouseWheelListener() {
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
if (!e.isControlDown()) { // only execute when ctrl is not pressed
|
||||
selectedFurniture.rotateAdd((e.getWheelRotation() * 10)); // the wheel rotation is multiplied with 10 and
|
||||
// added to the selectedFurniture
|
||||
} else { // the wheelRotation returns either 1 or -1 depending on the direction
|
||||
selectedFurniture.changeObjectSize(e.getWheelRotation());
|
||||
}
|
||||
} else {
|
||||
System.out.println("Nothing selected.");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void objectClickCheck() {
|
||||
for (Furniture furniture : allFurniture) { // check is executed for the number of furniture in the array
|
||||
if (furniture.getCurrentFigure().contains(draggedAtX, draggedAtY)) {
|
||||
selectFurniture(furniture);
|
||||
return;
|
||||
} else {
|
||||
unselectFurniture();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void selectFurniture(Furniture furniture) {
|
||||
selectedFurniture = furniture; // selects the furniture by moving its id to the selected variable
|
||||
restoreColor(previousSelectedFurniture); // prevents the color from not changing when clicking between objects
|
||||
previousSelectedFurniture = furniture; // remembers the previously selected furniture
|
||||
System.out.println("SELECTED: " + furniture); //// DEBUG
|
||||
selectedFurniturePosX = furniture.xPosition; // remembers the furnitures position when clicked
|
||||
selectedFurniturePosY = furniture.yPosition;
|
||||
furniture.changeCurrentColor(Color.black);
|
||||
}
|
||||
|
||||
public void unselectFurniture() {
|
||||
selectedFurniture = null;
|
||||
System.out.println("UNSELECTED"); //// DEBUG
|
||||
restoreColor(previousSelectedFurniture);
|
||||
}
|
||||
|
||||
public void restoreColor(Furniture furniture) {
|
||||
if (furniture != null) {
|
||||
furniture.changeCurrentColor(furniture.getColor()); // restores the color to the previous one
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* Interne Klasse Zeichenflaeche - die Klasse f<EFBFBD>r die GUI-Komponente, die
|
||||
* tats<EFBFBD>chlich im Leinwand-Fenster angezeigt wird. Diese Klasse definiert ein
|
||||
* JPanel mit der zus<EFBFBD>tzlichen M<EFBFBD>glichkeit, das auf ihm gezeichnet Image
|
||||
* aufzufrischen (erneut zu zeichnen).
|
||||
*/
|
||||
private class DrawingSurface extends JPanel {
|
||||
private static final long serialVersionUID = 1L; // Sets serial version that prevents file version incompatibility
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawImage(canvasImage, 0, 0, null);
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* Interne Klasse ShapeMitFarbe - Da die Klasse Shape des JDK nicht auch eine
|
||||
* Farbe mitverwalten kann, muss mit dieser Klasse die Verkn<EFBFBD>pfung modelliert
|
||||
* werden. graphic.fill() durch graphic.draw() ersetzt von Uwe Debacher am
|
||||
* 5.12.2003
|
||||
*/
|
||||
private class ShapeWithColor {
|
||||
private Shape shape;
|
||||
private Color color;
|
||||
|
||||
public ShapeWithColor(Shape shape, Color color) {
|
||||
this.shape = shape;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D graphic) {
|
||||
graphic.setColor(color);
|
||||
graphic.draw(shape);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveProject() {
|
||||
FileNameExtensionFilter filter = new FileNameExtensionFilter("Furniture Planner Projects (*.fpp)", "fpp");
|
||||
JFileChooser filechooser = new JFileChooser(); // creates the Filechoose object
|
||||
filechooser.setFileFilter(filter); // sets the file filter for the file chooser
|
||||
File file = filechooser.getSelectedFile();
|
||||
int returnVal = filechooser.showSaveDialog(null); // opens the file chooser dialog -- null centers the dialog
|
||||
if (!filechooser.getSelectedFile().getAbsolutePath().endsWith(".fpp")) {
|
||||
file = new File(filechooser.getSelectedFile() + ".fpp"); // adds the file name extension to the
|
||||
} else {
|
||||
file = filechooser.getSelectedFile();
|
||||
}
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) { // executed once open button is pressed
|
||||
String fileName = file.getName(); // filename to string
|
||||
System.out.println("FileName: " + fileName);
|
||||
System.out.println("Saving to " + file.getAbsolutePath() + " ...");
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
write(file.getAbsolutePath());
|
||||
} else if (confirmDialog("The file " + fileName + " already exsists. Do you want to overwrite it?",
|
||||
"Warning")) {
|
||||
file.delete();
|
||||
write(file.getAbsolutePath());
|
||||
} else {
|
||||
System.out.println("Operation denied!");
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
infoBox("An Error occured. Please try again. (IOException)", "Furniture Planner");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (returnVal == JFileChooser.CANCEL_OPTION) { // executed when the operation is cancelled
|
||||
return;
|
||||
} else if (returnVal == JFileChooser.ERROR_OPTION) { // executed on error
|
||||
infoBox("An Error occured. Please try again.", "Furniture Planner");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void write(String filepath) throws IOException {
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(filepath));
|
||||
|
||||
for (Furniture furniture : allFurniture) {
|
||||
objOut.writeObject(furniture);
|
||||
System.out.println("Writing object: " + furniture);
|
||||
}
|
||||
objOut.close(); // prevents any leakage
|
||||
}
|
||||
|
||||
public void openProject() {
|
||||
FileNameExtensionFilter filter = new FileNameExtensionFilter("Furniture Planner Projects (*.fpp)", "fpp");
|
||||
JFileChooser filechooser = new JFileChooser(); // creates the Filechoose object
|
||||
filechooser.setFileFilter(filter);
|
||||
|
||||
int returnVal = filechooser.showOpenDialog(null); // opens the file chooser dialog
|
||||
File file = filechooser.getSelectedFile();
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) { // executed once open button is pressed
|
||||
System.out.println(file.getPath());
|
||||
try {
|
||||
try {
|
||||
read(file.getAbsolutePath());
|
||||
} catch (ClassNotFoundException e) {
|
||||
infoBox("An Error occured. Please try again. (ClassNotFoundException)", "Furniture Planner");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
infoBox("An Error occured. Please try again. (IOException)", "Furniture Planner");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (returnVal == JFileChooser.CANCEL_OPTION) { // executed when the operation is cancelled
|
||||
return;
|
||||
} else if (returnVal == JFileChooser.ERROR_OPTION) { // executed on error
|
||||
infoBox("An Error occured. Please try again.", "Furniture Planner");
|
||||
}
|
||||
}
|
||||
|
||||
public void read(String filepath) throws IOException, ClassNotFoundException {
|
||||
if (confirmDialog("All unsaved data will be lost! Are you sure?", "Warning")) {
|
||||
boolean EOLreached = false;
|
||||
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(filepath));
|
||||
for (Furniture importedFurniture : allFurniture) {
|
||||
importedFurniture.hide();
|
||||
}
|
||||
Canvas.allFurniture.clear(); // clears the array
|
||||
Furniture importedFurniture = null;
|
||||
do { // is at least executed once
|
||||
try {
|
||||
importedFurniture = (Furniture) objIn.readObject();
|
||||
System.out.println("Imported " + importedFurniture); //// DEBUG
|
||||
} catch (EOFException e) {
|
||||
System.out.println("EOL reached"); //// DEBUG
|
||||
EOLreached = true;
|
||||
}
|
||||
if (EOLreached) {
|
||||
objIn.close(); // closes once the end is reached
|
||||
} else {
|
||||
Canvas.allFurniture.add(importedFurniture);
|
||||
importedFurniture.hide();
|
||||
importedFurniture.show();
|
||||
}
|
||||
} while (!EOLreached); // keeps repeating until EOF is reached
|
||||
} else {
|
||||
System.out.println("Operation denied!");
|
||||
return;
|
||||
}
|
||||
drawingSurface.repaint(); // repaint is triggered
|
||||
System.out.println("Repaint triggered "); //// DEBUG
|
||||
}
|
||||
|
||||
public static void infoBox(String infoMessage, String titleBar) { // method for creating an infobox
|
||||
JOptionPane.showMessageDialog(null, infoMessage, titleBar, JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
|
||||
public static boolean confirmDialog(String Message, String titleBar) {
|
||||
int reply = JOptionPane.showConfirmDialog(null, Message, titleBar, JOptionPane.YES_NO_OPTION);
|
||||
if (reply == JOptionPane.YES_OPTION) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void createmenubar() {
|
||||
|
||||
JMenuBar menubar = new JMenuBar(); // create menubar
|
||||
|
||||
JMenu file = new JMenu("File"); // creates the file tab
|
||||
menubar.add(file);
|
||||
file.setIcon(imageScaler("icons/icons8-file-48.png"));
|
||||
|
||||
JMenuItem open = new JMenuItem("Open Project", imageScaler("icons/icons8-opened-folder-48.png"));
|
||||
file.add(open);
|
||||
|
||||
JMenuItem save = new JMenuItem("Save Project", imageScaler("icons/icons8-save-48.png"));
|
||||
file.add(save);
|
||||
|
||||
JMenuItem exit = new JMenuItem("Exit", imageScaler("icons/icons8-exit-48.png")); // creates the exit entry
|
||||
file.add(exit); // adds the exit entry to the file tab
|
||||
|
||||
JMenu edit = new JMenu("Edit"); // creates the edit tab
|
||||
menubar.add(edit);
|
||||
edit.setIcon(imageScaler("icons/icons8-edit-48.png"));
|
||||
|
||||
JMenuItem changeColor = new JMenuItem("Change Color", imageScaler("icons/icons8-paint-palette-48.png"));
|
||||
edit.add(changeColor);
|
||||
|
||||
JMenuItem resetColor = new JMenuItem("Reset Color", imageScaler("icons/icons8-invert-colors-48.png"));
|
||||
edit.add(resetColor);
|
||||
|
||||
JMenuItem resetSize = new JMenuItem("Reset Size", imageScaler("icons/icons8-original-size-48.png"));
|
||||
edit.add(resetSize);
|
||||
|
||||
JMenuItem resetRotation = new JMenuItem("Reset Rotation", imageScaler("icons/icons8-rotate-48.png"));
|
||||
edit.add(resetRotation);
|
||||
|
||||
JMenuItem delete = new JMenuItem("Delete", imageScaler("icons/icons8-trash-48.png"));
|
||||
edit.add(delete);
|
||||
|
||||
JMenu addbar = new JMenu("Add");
|
||||
menubar.add(addbar);
|
||||
addbar.setIcon(imageScaler("icons/icons8-add-48.png"));
|
||||
|
||||
JMenuItem table = new JMenuItem("Table", imageScaler("icons/icons8-table-48.png"));
|
||||
addbar.add(table);
|
||||
|
||||
JMenuItem chair = new JMenuItem("Chair", imageScaler("icons/icons8-chair-48.png"));
|
||||
addbar.add(chair);
|
||||
|
||||
JMenuItem recliner = new JMenuItem("Armchair", imageScaler("icons/icons8-armchair-48.png"));
|
||||
addbar.add(recliner);
|
||||
|
||||
JMenuItem closet = new JMenuItem("Closet", imageScaler("icons/icons8-closet-48.png"));
|
||||
addbar.add(closet);
|
||||
|
||||
JMenuItem bed = new JMenuItem("Bed", imageScaler("icons/icons8-bed-48.png"));
|
||||
addbar.add(bed);
|
||||
|
||||
JMenu help = new JMenu("Help"); // creates the help tab
|
||||
menubar.add(help); // adds the help tab to the menubar
|
||||
help.setIcon(imageScaler("icons/icons8-help-48.png"));
|
||||
|
||||
JMenuItem about = new JMenuItem("About", imageScaler("icons/icons8-info-48.png"));
|
||||
help.add(about); // adds the about entry to the help tab
|
||||
|
||||
JMenuItem mouseMovement = new JMenuItem("Mouse Movement Demo", imageScaler("icons/icons8-drag-48.png"));
|
||||
help.add(mouseMovement);
|
||||
|
||||
exit.addActionListener(new exitaction()); // add actionListener to the exit entry
|
||||
table.addActionListener(new tableaction());
|
||||
delete.addActionListener(new deleteaction());
|
||||
open.addActionListener(new openaction());
|
||||
save.addActionListener(new saveaction());
|
||||
about.addActionListener(new aboutaction());
|
||||
chair.addActionListener(new chairaction());
|
||||
recliner.addActionListener(new reclineraction());
|
||||
closet.addActionListener(new closetaction());
|
||||
bed.addActionListener(new bedaction());
|
||||
resetRotation.addActionListener(new resetrotationaction());
|
||||
changeColor.addActionListener(new changecoloraction());
|
||||
resetColor.addActionListener(new resetcoloraction());
|
||||
resetSize.addActionListener(new resetsizeaction());
|
||||
mouseMovement.addActionListener(new mousemovementdemoaction());
|
||||
|
||||
window.setJMenuBar(menubar); // assigns the menubar to the Jframe
|
||||
}
|
||||
|
||||
public ImageIcon imageScaler(String icon) {
|
||||
ImageIcon i = new ImageIcon((getClass().getResource(icon)));
|
||||
Image image = i.getImage(); // transform it
|
||||
Image newimg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way to 20x20
|
||||
// pixels
|
||||
i = new ImageIcon(newimg); // transform it back
|
||||
return i;
|
||||
}
|
||||
|
||||
// Main Function is executed with class
|
||||
public static void main(String[] args) {
|
||||
getCanvas();
|
||||
|
||||
}
|
||||
|
||||
// Action classes that implement the ActionListener that execute once clicked
|
||||
class exitaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
class tableaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Furniture table = new Table(10, 10, Color.red, 0, 100, 60);
|
||||
table.show();
|
||||
Canvas.allFurniture.add(table);
|
||||
}
|
||||
}
|
||||
|
||||
class chairaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Furniture chair = new Chair(10, 10, Color.blue, 0);
|
||||
chair.show();
|
||||
Canvas.allFurniture.add(chair);
|
||||
}
|
||||
}
|
||||
|
||||
class reclineraction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Furniture recliner = new Recliner(10, 10, Color.green, 0);
|
||||
recliner.show();
|
||||
Canvas.allFurniture.add(recliner);
|
||||
}
|
||||
}
|
||||
|
||||
class closetaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Furniture closet = new Closet(10, 10, Color.orange, 0, 60, 37);
|
||||
closet.show();
|
||||
Canvas.allFurniture.add(closet);
|
||||
}
|
||||
}
|
||||
|
||||
class bedaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Furniture bed = new Bed(10, 10, Color.magenta, 0, 60, 60);
|
||||
bed.show();
|
||||
Canvas.allFurniture.add(bed);
|
||||
}
|
||||
}
|
||||
|
||||
class openaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
openProject();
|
||||
}
|
||||
}
|
||||
|
||||
class saveaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
saveProject();
|
||||
}
|
||||
}
|
||||
|
||||
class mousemovementdemoaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
MOUSEMovementTest.MouseMovementTester();
|
||||
}
|
||||
}
|
||||
|
||||
class resetsizeaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
selectedFurniture.resetObjectSize();
|
||||
} else {
|
||||
System.out.println("Nothing Selected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class deleteaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
Furniture x = selectedFurniture;
|
||||
selectedFurniture.hide();
|
||||
selectedFurniture.resetCanvas();
|
||||
removeFigure(x); // removes from canvas
|
||||
allFurniture.remove(x); // removes from array
|
||||
System.out.println("REMOVED: " + x); //// DEBUG
|
||||
System.out.println("New array: " + allFurniture); //// DEBUG
|
||||
} else {
|
||||
System.out.println("Nothing Selected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class changecoloraction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
Color color = selectedFurniture.getColor();
|
||||
color = JColorChooser.showDialog(null, "Choose new color", color);
|
||||
selectedFurniture.changeGeneralColor(color); // changes the general color of the object
|
||||
selectedFurniture.changeCurrentColor(color); // changes the current/changing-with-selection color of the object
|
||||
} else {
|
||||
System.out.println("Nothing Selected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class resetcoloraction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
Color color = selectedFurniture.getDefaultColor();
|
||||
selectedFurniture.changeGeneralColor(color);
|
||||
selectedFurniture.changeCurrentColor(color);
|
||||
} else {
|
||||
System.out.println("Nothing Selected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class resetrotationaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (allFurniture.size() != 0 && selectedFurniture != null) { // only executes once the array is not empty
|
||||
selectedFurniture.resetRotation();
|
||||
}
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class aboutaction implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Canvas.infoBox(
|
||||
"Furniture Planner v3.0\nby Kevin Dorner\n\nInformatics 2020\n\nIcons from ICONS8\n\nContributors:\nBruce Quig\nMichael Koelling\nAxel Schmolitzky\nUwe Debacher",
|
||||
"About");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.GeneralPath;
|
||||
|
||||
/**
|
||||
* Ein Stuhl, der manipuliert werden kann und sich selbst auf einer Leinwand
|
||||
* zeichnet.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.3 (sept 07)
|
||||
*/
|
||||
public class Chair extends Furniture {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Stuhl mit einer Standardfarbe und Standardgroesse an
|
||||
* einer Standardposition. (Standardkonstruktor)
|
||||
*/
|
||||
public Chair() {
|
||||
xPosition = 60;
|
||||
yPosition = 80;
|
||||
orientation = 270;
|
||||
color = Color.blue;
|
||||
isVisible = false;
|
||||
width = 40;
|
||||
depth = 40;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Stuhl mit einer Standardgroesse.
|
||||
*/
|
||||
public Chair(int xPosition, int yPosition, Color color, int orientation) {
|
||||
this.xPosition = xPosition;
|
||||
this.yPosition = yPosition;
|
||||
this.color = color;
|
||||
this.currentColor = color; // the default color is set to the initial color
|
||||
this.defaultColor = color; // the default color is set to the initial color
|
||||
this.orientation = orientation;
|
||||
isVisible = false;
|
||||
width = 40;
|
||||
depth = 40;
|
||||
this.defaultWidth = width; // the default width is set to the initial width
|
||||
this.defaultDepth = depth; // the default width is set to the initial depth
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das zu zeichnende Shape anhand der gegebenen Daten [ Zum Anzeigen
|
||||
* der Attributwerte ueber Inspect muss hier die Sichtbarkeit auf public gesetzt
|
||||
* werden. ]
|
||||
*/
|
||||
protected Shape getCurrentFigure() {
|
||||
// einen GeneralPath definieren
|
||||
GeneralPath chair = new GeneralPath();
|
||||
chair.moveTo(0, 0);
|
||||
chair.lineTo(width, 0);
|
||||
chair.lineTo(width + (width / 20 + 1), depth);
|
||||
chair.lineTo(-(width / 20 + 1), depth);
|
||||
chair.lineTo(0, 0);
|
||||
// Das ist die Umrandung. Das Stuhl bekommt noch eine Lehne:
|
||||
chair.moveTo(0, (width / 10 + 1));
|
||||
chair.lineTo(width, (width / 10 + 1));
|
||||
return transform(chair);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.geom.Line2D;
|
||||
|
||||
/**
|
||||
* Ein Schrank, der manipuliert werden kann und sich selbst auf einer Leinwand
|
||||
* zeichnet.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.3 (sept 07)
|
||||
*/
|
||||
public class Closet extends Furniture {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Schrank mit einer Standardfarbe und Standardgroesse an
|
||||
* einer Standardposition. (Standardkonstruktor)
|
||||
*/
|
||||
public Closet() {
|
||||
xPosition = 10;
|
||||
yPosition = 10;
|
||||
color = Color.yellow;
|
||||
orientation = 0;
|
||||
isVisible = false;
|
||||
width = 60;
|
||||
depth = 37;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Schrank.
|
||||
*/
|
||||
public Closet(int xPosition, int yPosition, Color color, int orientation, int width, int depth) {
|
||||
this.xPosition = xPosition;
|
||||
this.yPosition = yPosition;
|
||||
this.color = color;
|
||||
this.currentColor = color; // the default color is set to the initial color
|
||||
this.defaultColor = color; // the default color is set to the initial color
|
||||
this.defaultWidth = width; // the default width is set to the initial width
|
||||
this.defaultDepth = depth; // the default width is set to the initial depth
|
||||
this.orientation = orientation;
|
||||
isVisible = false;
|
||||
this.width = width;
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das zu zeichnende Shape anhand der gegebenen Daten [ Zum Anzeigen
|
||||
* der Attributwerte ueber Inspect muss hier die Sichtbarkeit auf public gesetzt
|
||||
* werden. ]
|
||||
*/
|
||||
protected Shape getCurrentFigure() {
|
||||
// einen GeneralPath definieren
|
||||
GeneralPath schrank = new GeneralPath();
|
||||
Rectangle2D rahmen = new Rectangle2D.Double(0, 0, width, depth);
|
||||
schrank.append(rahmen, false);
|
||||
Line2D diagonale1 = new Line2D.Double(0, 0, width, depth);
|
||||
schrank.append(diagonale1, false);
|
||||
Line2D diagonale2 = new Line2D.Double(0, depth, width, 0);
|
||||
schrank.append(diagonale2, false);
|
||||
return transform(schrank);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,212 @@
|
|||
import java.awt.Shape;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.Color;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Die Klasse Moebel definiert die gemeinsamen Eigenschaften aller
|
||||
* Moebelklassen.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.2 (aug 07)
|
||||
*/
|
||||
public abstract class Furniture implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L; // default serialID
|
||||
protected int xPosition;
|
||||
protected int yPosition;
|
||||
protected int orientation;
|
||||
protected Color color;
|
||||
protected Color currentColor; // this is copied from color once created
|
||||
protected Color defaultColor; // this is copied from color once created
|
||||
protected boolean isVisible;
|
||||
protected int width;
|
||||
protected int depth;
|
||||
protected int defaultWidth;
|
||||
protected int defaultDepth;
|
||||
|
||||
// Kein Konstruktor !!!!
|
||||
|
||||
/**
|
||||
* Diese Methode ist der Kern der Abstraktion.
|
||||
*/
|
||||
protected abstract Shape getCurrentFigure();
|
||||
|
||||
/**
|
||||
* Mache dieses Objekt sichtbar. Wenn es bereits sichtbar ist, tue nichts.
|
||||
*/
|
||||
public void show() {
|
||||
if (!isVisible) {
|
||||
isVisible = true;
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* transformiert das aktuelle Shape.
|
||||
*/
|
||||
protected Shape transform(Shape shape) {
|
||||
AffineTransform t = new AffineTransform();
|
||||
t.translate(xPosition, yPosition);
|
||||
Rectangle2D umriss = shape.getBounds2D();
|
||||
t.rotate(Math.toRadians(orientation), umriss.getX() + umriss.getWidth() / 2,
|
||||
umriss.getY() + umriss.getHeight() / 2);
|
||||
return t.createTransformedShape(shape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mache dieses Objekt unsichtbar. Wenn es bereits unsichtbar ist, tue nichts.
|
||||
*/
|
||||
public void hide() {
|
||||
delete(); // "tue nichts" wird in delete() abgefangen.
|
||||
isVisible = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drehe auf den angegebenen Winkel
|
||||
*/
|
||||
public void rotateTo(int newAngle) {
|
||||
delete();
|
||||
orientation = newAngle;
|
||||
draw();
|
||||
}
|
||||
|
||||
public void resetCanvas() {
|
||||
delete();
|
||||
draw();
|
||||
}
|
||||
|
||||
public void rotateAdd(int angleDifference) {
|
||||
delete();
|
||||
orientation += angleDifference;
|
||||
draw();
|
||||
}
|
||||
|
||||
public void resetRotation() {
|
||||
delete();
|
||||
orientation = 0;
|
||||
draw();
|
||||
}
|
||||
|
||||
public void changeObjectSize(int scrollDirection) { // scrollDirection is 1 or -1 depending of scrolled direction
|
||||
delete();
|
||||
if (scrollDirection == 1) {
|
||||
width += width / 15;
|
||||
depth += depth / 15;
|
||||
} else if (scrollDirection == -1 && width > 30 && depth > 30) { // depth or width shall not be under 30 to set a
|
||||
// minimum size
|
||||
width -= width / 15;
|
||||
depth -= depth / 15;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
|
||||
public void resetObjectSize() {
|
||||
delete();
|
||||
width = defaultWidth;
|
||||
depth = defaultDepth;
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bewege dieses Objekt horizontal um 'entfernung' Bildschirmpunkte.
|
||||
*/
|
||||
public void moveHorizontal(int distance) {
|
||||
delete();
|
||||
xPosition += distance;
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bewege dieses objekt vertikal um 'entfernung' Bildschirmpunkte.
|
||||
*/
|
||||
public void moveVertical(int distance) {
|
||||
delete();
|
||||
yPosition += distance;
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets new x and y positions.
|
||||
*/
|
||||
public void setPos(int xPos, int yPos) {
|
||||
delete();
|
||||
xPosition = xPos;
|
||||
yPosition = yPos;
|
||||
draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets x position.
|
||||
*/
|
||||
public int getXPos() {
|
||||
return xPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets y position.
|
||||
*/
|
||||
public int getYPos() {
|
||||
return yPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aendere die Farbe dieses Objektes in 'neueFarbe'. Gueltige Angaben sind
|
||||
* "rot", "gelb", "blau", "gruen", "lila" und "schwarz".
|
||||
*/
|
||||
public void changeCurrentColor(Color newColor) {
|
||||
delete();
|
||||
currentColor = newColor;
|
||||
draw();
|
||||
}
|
||||
|
||||
public void changeGeneralColor(Color newColor) {
|
||||
color = newColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the color assigned to an object.
|
||||
*/
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current color of an object.
|
||||
*/
|
||||
public Color getCurrentColor() {
|
||||
return currentColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default color of an object.
|
||||
*/
|
||||
public Color getDefaultColor() {
|
||||
return defaultColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeichne dieses Objekt mit seinen aktuellen Werten auf den Bildschirm.
|
||||
*/
|
||||
private void draw() {
|
||||
if (isVisible) {
|
||||
Shape figur = getCurrentFigure();
|
||||
Canvas canvas = Canvas.getCanvas();
|
||||
canvas.drawFigure(this, // leinwand kennt das Objekt
|
||||
currentColor, // defines the current color
|
||||
figur); // definiert seinen grafischen Aspekt
|
||||
canvas.wait(10);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loesche dieses Objekt vom Bildschirm.
|
||||
*/
|
||||
private void delete() {
|
||||
if (isVisible) {
|
||||
Canvas leinwand = Canvas.getCanvas();
|
||||
leinwand.removeFigure(this);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class MOUSEMovementTest extends JButton {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private volatile int draggedAtX, draggedAtY;
|
||||
|
||||
public MOUSEMovementTest(String text) {
|
||||
super(text);
|
||||
setDoubleBuffered(false);
|
||||
setMargin(new Insets(0, 0, 0, 0));
|
||||
setSize(125, 25);
|
||||
setPreferredSize(new Dimension(25, 25));
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
draggedAtX = e.getX();
|
||||
draggedAtY = e.getY();
|
||||
}
|
||||
});
|
||||
|
||||
addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
setLocation(e.getX() - draggedAtX + getLocation().x, e.getY() - draggedAtY + getLocation().y);
|
||||
System.out.println(e.getX() + " " + draggedAtX + " " + getLocation().x);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void MouseMovementTester() {
|
||||
JFrame frame = new JFrame("DragButton");
|
||||
frame.setLayout(null);
|
||||
frame.getContentPane().add(new MOUSEMovementTest("Thanks"));
|
||||
frame.getContentPane().add(new MOUSEMovementTest("for"));
|
||||
frame.getContentPane().add(new MOUSEMovementTest("using!"));
|
||||
frame.setSize(300, 300);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
/**
|
||||
* Ein Sessel, der manipuliert werden kann und sich selbst auf einer Leinwand
|
||||
* zeichnet.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.2 (aug 07)
|
||||
*/
|
||||
public class Recliner extends Furniture {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Sessel mit einer Standardfarbe und Standardgroesse an
|
||||
* einer Standardposition. (Standardkonstruktor)
|
||||
*/
|
||||
public Recliner() {
|
||||
xPosition = 250;
|
||||
yPosition = 70;
|
||||
color = Color.green;
|
||||
orientation = 90;
|
||||
isVisible = false;
|
||||
width = 60;
|
||||
depth = 60;
|
||||
}
|
||||
|
||||
public Recliner(int xPosition, int yPosition, Color color, int orientation) {
|
||||
this.xPosition = xPosition;
|
||||
this.yPosition = yPosition;
|
||||
this.color = color;
|
||||
this.currentColor = color; // the default color is set to the initial color
|
||||
this.defaultColor = color; // the default color is set to the initial color
|
||||
this.orientation = orientation;
|
||||
isVisible = false;
|
||||
width = 60;
|
||||
depth = 60;
|
||||
this.defaultWidth = width; // the default width is set to the initial width
|
||||
this.defaultDepth = depth; // the default width is set to the initial depth
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das zu zeichnende Shape anhand der gegebenen Daten
|
||||
*/
|
||||
protected Shape getCurrentFigure() {
|
||||
// einen GeneralPath definieren
|
||||
GeneralPath recliner = new GeneralPath();
|
||||
Rectangle2D leftArmRest = new Rectangle2D.Double(0, 0, width / 6, depth);
|
||||
Rectangle2D rightArmRest = new Rectangle2D.Double(5 * width / 6, 0, width / 6, depth);
|
||||
Rectangle2D sittingArea = new Rectangle2D.Double(width / 6, depth / 6, 2 * width / 3, depth - depth / 6);
|
||||
Rectangle2D back = new Rectangle2D.Double(width / 6, 0, 2 * width / 3, depth / 6);
|
||||
recliner.append(leftArmRest, false);
|
||||
recliner.append(rightArmRest, false);
|
||||
recliner.append(sittingArea, false);
|
||||
recliner.append(back, false);
|
||||
return transform(recliner);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
import java.awt.Color;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
|
||||
/**
|
||||
* Ein Tisch, der manipuliert werden kann und sich selbst auf einer Leinwand
|
||||
* zeichnet.
|
||||
*
|
||||
* @author Claus Albowski
|
||||
* @version 2.3 (sept 07)
|
||||
*/
|
||||
public class Table extends Furniture {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Tisch mit einer Standardfarbe und Standardgroesse an
|
||||
* einer Standardposition. (Standardkonstruktor)
|
||||
*/
|
||||
public Table() {
|
||||
xPosition = 120;
|
||||
yPosition = 50;
|
||||
orientation = 0;
|
||||
color = Color.red;
|
||||
isVisible = false;
|
||||
width = 120;
|
||||
depth = 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeuge einen neuen Tisch.
|
||||
*/
|
||||
public Table(int xPosition, int yPosition, Color color, int orientation, int width, int depth) {
|
||||
this.xPosition = xPosition;
|
||||
this.yPosition = yPosition;
|
||||
this.color = color;
|
||||
this.currentColor = color; // the default color is set to the initial color
|
||||
this.defaultColor = color; // the default color is set to the initial color
|
||||
this.orientation = orientation;
|
||||
isVisible = false;
|
||||
this.width = width;
|
||||
this.depth = depth;
|
||||
this.defaultWidth = width; // the default width is set to the initial width
|
||||
this.defaultDepth = depth; // the default width is set to the initial depth
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das zu zeichnende Shape anhand der gegebenen Daten [ Zum Anzeigen
|
||||
* der Attributwerte ueber Inspect muss hier die Sichtbarkeit auf public gesetzt
|
||||
* werden. ]
|
||||
*/
|
||||
protected Shape getCurrentFigure() {
|
||||
Shape tisch = new Ellipse2D.Double(0, 0, width, depth);
|
||||
return transform(tisch);
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 999 B |
After Width: | Height: | Size: 488 B |
After Width: | Height: | Size: 881 B |
After Width: | Height: | Size: 793 B |
After Width: | Height: | Size: 325 B |
After Width: | Height: | Size: 445 B |
After Width: | Height: | Size: 741 B |
After Width: | Height: | Size: 650 B |
After Width: | Height: | Size: 256 B |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 578 B |
After Width: | Height: | Size: 411 B |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 423 B |
After Width: | Height: | Size: 210 B |
After Width: | Height: | Size: 456 B |