Created
October 23, 2011 22:28
-
-
Save devlaers/1307974 to your computer and use it in GitHub Desktop.
This program creates a GUI with 7 rectangels randomly spaced in the window. If a rectangel is outlined in red and you click on it a random shape will appear on that rectangle. If the shape is clicked again it will disapper. If you click on a reactangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
import java.awt.Graphics; | |
public class Circle extends Shape{ | |
/** constructor for the circle class | |
* @param a x-value that is being clicked by the user | |
* @param b y-value that is being clicked by the user | |
*/ | |
public Circle(int a, int b) | |
{ | |
super(a, b); | |
} | |
/** draw method for the Circle class | |
* @param g graphics object that is being passed in | |
*/ | |
public void draw(Graphics g) | |
{ | |
g.fillOval(x, y, LW, LW); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
public class Shape { | |
protected int x; | |
protected int y; | |
protected final int LW = 20; | |
/** constructor for the Shape class | |
* @param a x-value that is being clicked by the user | |
* @param b y-value that is being clicked by the user | |
*/ | |
public Shape(int a, int b) | |
{ | |
x = a; | |
y = b; | |
} | |
/** isInside method for the Shape class | |
* @param a x-value that is being clicked | |
* @param b y-value that is being clicked | |
* @return if where is user clicked is inside a shape in the window | |
*/ | |
public boolean isInside(int a, int b) | |
{ | |
if(x <= a && a <= x + LW && y <= b && b <= y + LW) | |
return true; | |
else | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics; | |
/** | |
* This interface should be implemented by the class that | |
* is responsible for managing the windows. The SimpleWindowsGUI | |
* will then call these methods when needed. | |
*/ | |
public interface SimpleWindowManager { | |
/** | |
* This method will be called when the windows need to | |
* be drawn. This method should draw all of the windows | |
* and their contents in the proper order using the | |
* provided Graphics object. | |
* | |
* @param g the Graphics object used for the drawing. | |
*/ | |
public void draw( Graphics g ); | |
/** | |
* This method will be called when the user clicks anywhere | |
* within the main area of the GUI. This method should update | |
* the appropriate window lists based on the location of the | |
* click. The draw method will be called automatically, | |
* shortly after this method returns. | |
* | |
* @param x the x location of the mouse click. | |
* @param y the y location of the mouse click. | |
*/ | |
public void handleClick( int x, int y ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
/** | |
* This class implements a simple GUI with a blank canvas. | |
* An instance of a class that implements SimpleWindowManager | |
* is responsible for all drawing within the canvas, and is | |
* responsible for updating the window information based on | |
* the location of the mouse clicks. | |
*/ | |
public class SimpleWindowsGUI extends JFrame implements MouseListener { | |
/** The canvas where the windows are drawn. */ | |
private RectanglesCanvas canvas; | |
/** The object that manages the window data. */ | |
private SimpleWindowManager model; | |
/** The default width of the canvas */ | |
public static final int CANVAS_WIDTH = 800; | |
/** The default height of the canvas */ | |
public static final int CANVAS_HEIGHT = 600; | |
/** | |
* Constructs the GUI and makes the window visible. | |
* | |
* @param mod the window manager to be used by this GUI. | |
*/ | |
public SimpleWindowsGUI(SimpleWindowManager mod) { | |
this.model = mod; | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
this.setTitle("Rectangles!"); | |
this.add(canvas = new RectanglesCanvas(), BorderLayout.CENTER); | |
canvas.addMouseListener(this); | |
this.pack(); | |
this.setVisible(true); | |
} | |
/** | |
* This class implements the canvas where the windows | |
* are drawn. | |
*/ | |
private class RectanglesCanvas extends JPanel { | |
/** | |
* Constructs the canvas with a white background. | |
*/ | |
public RectanglesCanvas() { | |
this.setPreferredSize( new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); | |
this.setBackground(Color.white); | |
} | |
/** | |
* Draws this component by calling the draw method | |
* of the window manager. | |
*/ | |
public void paintComponent( Graphics g ) { | |
super.paintComponent(g); | |
model.draw(g); | |
} | |
} | |
/** | |
* When mouse is clicked, the handleClick method in the | |
* window manager is called. | |
* @param e the mouse event | |
*/ | |
public void mouseClicked(MouseEvent e) { | |
model.handleClick(e.getX(), e.getY()); | |
canvas.repaint(); | |
} | |
public void mouseEntered(MouseEvent e) { } | |
public void mouseExited(MouseEvent e) { } | |
public void mousePressed(MouseEvent e) { } | |
public void mouseReleased(MouseEvent e) { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
import java.awt.Graphics; | |
public class SmileyFace extends Shape{ | |
/** constructor for the SmileyFace class | |
* @param a x-value that is being clicked by the user | |
* @param b y-value that is being clicked by the user | |
*/ | |
public SmileyFace(int a, int b) | |
{ | |
super(a, b); | |
} | |
/** draw method for the SmileyFace class | |
* @param g graphics object that is being passed in | |
*/ | |
public void draw(Graphics g) | |
{ | |
g.drawOval(x, y, LW, LW); | |
int x1 = x + LW / 4; | |
int y1 = y + LW / 2; | |
int length1 = LW / 4; | |
int width1 = LW / 2; | |
g.drawArc(x1, y1, width1, length1, 180, 180); | |
int y2 = y + LW / 4; | |
int length2 = LW / 10; | |
int width2 = LW / 5; | |
int x2 = x + 3 * LW / 4 - length2; | |
g.drawOval(x1, y2, length2, width2); | |
g.drawOval(x2, y2, length2, width2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
import java.awt.Graphics; | |
public class Square extends Shape{ | |
/** constructor for the square class | |
* @param a x-value that is being clicked by the user | |
* @param b y-value that is being clicked by the user | |
*/ | |
public Square(int a, int b) | |
{ | |
super(a, b); | |
} | |
/** draw method for the Square class | |
* @param g graphics object that is being passed in | |
*/ | |
public void draw(Graphics g) | |
{ | |
g.fillRect(x, y, LW, LW); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.util.LinkedList; | |
import java.util.ListIterator; | |
public class Window { | |
private LinkedList<Shape> shapes; | |
private int x; | |
private int y; | |
private final int WIDTH = 250; | |
private final int HEIGHT = 150; | |
private Color shapeColor; | |
private int shapeNum; | |
/** constructor for the Window class | |
* @param a x-value of where the user clicked | |
* @param b y-value of where the user clicked | |
* @param color color that the shape will be | |
*/ | |
public Window(int a, int b, Color color, int num) | |
{ | |
x = a; | |
y = b; | |
shapeColor = color; | |
shapeNum = num; | |
shapes = new LinkedList<Shape>(); | |
} | |
/** draw method for the Window class | |
* @param g graphics object being passed in | |
* @param isFront showing if the window is the one in front | |
*/ | |
public void draw(Graphics g, boolean isFront) | |
{ | |
//draw the window | |
g.setColor(Color.lightGray); | |
g.fillRect(x, y , WIDTH, HEIGHT); | |
if (isFront == true) | |
g.setColor(Color.red); | |
else | |
g.setColor(Color.black); | |
g.drawRect(x, y , WIDTH, HEIGHT); | |
//go through the linked list of shapes and make is so they can be drawn | |
ListIterator<Shape> iter = shapes.listIterator(); | |
g.setColor(shapeColor); | |
while(iter.hasNext()) | |
{ | |
if(shapeNum == 0) | |
{ | |
Square s = (Square) iter.next(); | |
s.draw(g); | |
} | |
else if(shapeNum == 1) | |
{ | |
SmileyFace s = (SmileyFace) iter.next(); | |
s.draw(g); | |
} | |
else | |
{ | |
Circle c = (Circle) iter.next(); | |
c.draw(g); | |
} | |
} | |
} | |
/** isInside method for the Window class | |
* @param a x-value of where the user clicked | |
* @param b y-value of where the user clicked | |
* @return if the user clicked inside the window | |
*/ | |
public boolean isInside(int a, int b) | |
{ | |
if(x <= a && a <= x + WIDTH && y <= b && b <= y + HEIGHT) | |
return true; | |
else | |
return false; | |
} | |
/** isToClose method for the Window class | |
* @param a x-value of where the user clicked | |
* @param b y-value of where the user clicked | |
* @return if the user clicked to close to the side of the window | |
*/ | |
public boolean isToClose(int a, int b) | |
{ | |
if((x <= a && a <= x + 15) || (a <= x + WIDTH && x + WIDTH - 15 <= a) || | |
(y <= b && b <= y + 15) || (b <= y + HEIGHT && y + HEIGHT - 15 <= b)) | |
return true; | |
else | |
return false; | |
} | |
/** the handleClick method for the Window class | |
* @param a x-value of where the user clicked | |
* @param b y-value of where the user clicked | |
*/ | |
public void handleClick(int a, int b) | |
{ | |
ListIterator<Shape> iter = shapes.listIterator(); | |
boolean hasRemoved = false; | |
//remove all shapes that have been clicked | |
while(iter.hasNext()) | |
{ | |
Shape s = iter.next(); | |
if(s.isInside(a, b) == true) | |
{ | |
iter.remove(); | |
hasRemoved = true; | |
} | |
} | |
//add a shape as long as it is not to close to the edge and nothing has | |
//been removed | |
if(isToClose(a, b) == false && hasRemoved == false) | |
{ | |
int x = a; | |
int y = b; | |
if(shapeNum == 0) | |
{ | |
Square square1 = new Square(x-10, y-10); | |
shapes.add(square1); | |
} | |
else if(shapeNum ==1) | |
{ | |
SmileyFace smileyface1 = new SmileyFace(x-10, y-10); | |
shapes.add(smileyface1); | |
} | |
else | |
{ | |
Circle circle1 = new Circle(x-10, y-10); | |
shapes.add(circle1); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
public class WindowManagerMain { | |
public static void main(String[] args) { | |
WindowManager manager = new WindowManager(); | |
SimpleWindowsGUI gui = new SimpleWindowsGUI(manager); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Rachael Devlaeminck | |
// Assignment: Lab 06 | |
// Title: Linked Lists in a Graphical Window Manager | |
// Course: CSCE 270 | |
// Lab Section: Section 1 | |
// Semester: Fall 2010 | |
// Instructor: David Wolff | |
// Date: 10/20/10 | |
// Sources consulted: Professor Wolff | |
// Program description: creates windows and lets you add random shapes to the | |
// front window which is outlined in red | |
// Known Bugs: none | |
// Creativity: created a shape class that will allow random shapes to be made | |
// in the windows | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.util.LinkedList; | |
import java.util.ListIterator; | |
import java.util.Random; | |
public class WindowManager implements SimpleWindowManager{ | |
protected LinkedList<Window> windows = new LinkedList<Window>(); | |
Random rand = new Random(); | |
/** constructor for WindowManager class | |
* creates 7 window objects and assigns them a color | |
*/ | |
public WindowManager() | |
{ | |
//get random x and y values | |
int x = rand.nextInt(401); | |
int y = rand.nextInt(401); | |
//create window object and add it to the linked list | |
for (int i = 0; i < 7; i++) | |
{ | |
//create a random color for the shapes in the window | |
int R = rand.nextInt(256); | |
int G = rand.nextInt(256); | |
int B = rand.nextInt(256); | |
Color rgb = new Color(R, G, B); | |
int num = rand.nextInt(3); | |
Window window1 = new Window(x, y, rgb, num); | |
windows.add(window1); | |
//get new random x and y values | |
x = rand.nextInt(401); | |
y = rand.nextInt(401); | |
} | |
} | |
/** draw method for WindowManager class | |
* @param g passes in the graphic object | |
*/ | |
@Override | |
public void draw(Graphics g) | |
{ | |
ListIterator<Window> iter = windows.listIterator(); | |
//go through the linked list and make them able to be drawn in the | |
//Window class | |
while(iter.hasNext()) | |
{ | |
Window w = iter.next(); | |
//tell is the widow is in the front or not | |
if(iter.hasNext()) | |
w.draw(g, false); | |
else | |
w.draw(g, true); | |
} | |
} | |
/** handleClick method for WindowManager class | |
* @param x value that is being clicked by the user | |
* @param y value that is being clicked by the user | |
*/ | |
@Override | |
public void handleClick(int x, int y) { | |
ListIterator<Window> iter = windows.listIterator(7); | |
boolean hasRemoved = false; | |
//go through the list as long as nothing has been removed | |
while(iter.hasPrevious() && hasRemoved == false) | |
{ | |
Window w = iter.previous(); | |
if(w.isInside(x, y) == true) | |
{ | |
//if the window is already at the front call the window | |
//handle click method to draw shapes | |
if(iter.nextIndex() == windows.size()-1) | |
{ | |
w.handleClick(x, y); | |
} | |
//move the window to the front of the screen | |
else | |
{ | |
iter.remove(); | |
windows.add(w); | |
} | |
hasRemoved = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment