Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions maman12-Q2/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions maman12-Q2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions maman12-Q2/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>maman12-Q2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions maman12-Q2/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
9 changes: 9 additions & 0 deletions maman12-Q2/src/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class MainClass {

public static void main(String[] args) {

Program aProgram = new Program();
}

}
19 changes: 19 additions & 0 deletions maman12-Q2/src/MyBoundedShape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.awt.Color;

public abstract class MyBoundedShape extends MyShape {

private boolean isFull;

protected MyBoundedShape(int x1, int x2, int y1, int y2, Color color, boolean isFull) {
super(x1, x2, y1, y2, color);
this.isFull = isFull;
}

public boolean getIsFull() {
return isFull;
}

public void setFull(boolean isFull) {
this.isFull = isFull;
}
}
15 changes: 15 additions & 0 deletions maman12-Q2/src/MyJFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import javax.swing.JFrame;

public class MyJFrame extends JFrame {

private static final int WIDTH = 400;
private static final int HEIGHT = 400;

public MyJFrame() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setVisible(true);
}

}
45 changes: 45 additions & 0 deletions maman12-Q2/src/MyLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.awt.Color;
import java.awt.Graphics;

public class MyLine extends MyShape {

private static final int POW_FOR_DISTANCE = 2;

public MyLine(int x1, int x2, int y1, int y2, Color color) {
super(x1, x2, y1, y2, color);

}

@Override
public void paint(Graphics g) {
g.setColor(this.getColor());
g.drawLine(this.getX1(), this.getX2(), this.getY1(), this.getY1());

}

// To calculate line's distance, this function is using the distance between 2
// points formula
public double distance() {
return Math.sqrt(Math.pow(this.getX1() - this.getX2(), POW_FOR_DISTANCE)
+ Math.pow(this.getY1() - this.getY2(), POW_FOR_DISTANCE));
}

@Override
public boolean equals(Object object) {

if (object instanceof MyLine) {
MyLine aLine = (MyLine) object;
return (this.distance() == aLine.distance());

}
return false;
}

@Override
public Object clone() {
MyLine aLine = new MyLine(this.getX1(), this.getX2(), this.getY1(), this.getY2(), this.getColor());
return aLine;

}

}
39 changes: 39 additions & 0 deletions maman12-Q2/src/MyOval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.awt.Color;
import java.awt.Graphics;

public class MyOval extends MyBoundedShape {

protected MyOval(int x1, int x2, int y1, int y2, Color color, boolean isFull) {
super(x1, x2, y1, y2, color, isFull);
}

@Override
public void paint(Graphics g) {
g.setColor(this.getColor());
g.drawOval(this.getX1(), this.getX2(), this.getY1(), this.getY1());
if (this.getIsFull()) {
g.fillOval(this.getX1(), this.getX2(), this.getY1(), this.getY1());
}

}

@Override
public boolean equals(Object object) {
if (object instanceof MyOval) {
MyOval aOval = (MyOval) object;

return (this.getX2() == aOval.getX2() && this.getY2() == aOval.getY2());

}
return false;
}

@Override
public Object clone() {
MyOval anOval = new MyOval(this.getX1(), this.getX2(), this.getY1(), this.getY2(), this.getColor(),
this.getIsFull());
return anOval;

}

}
41 changes: 41 additions & 0 deletions maman12-Q2/src/MyRectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.awt.Color;
import java.awt.Graphics;

public class MyRectangle extends MyBoundedShape {

protected MyRectangle(int x1, int x2, int y1, int y2, Color color, boolean isFull) {
super(x1, x2, y1, y2, color, isFull);

}

@Override
public void paint(Graphics g) {
g.setColor(this.getColor());
g.drawRect(this.getX1(), this.getX2(), this.getY1(), this.getY1());

if (this.getIsFull()) {
g.fillRect(this.getX1(), this.getX2(), this.getY1(), this.getY1());
}

}

@Override
public boolean equals(Object object) {
if (object instanceof MyRectangle) {
MyRectangle aRect = (MyRectangle) object;

return (this.getX2() == aRect.getX2() && this.getY2() == aRect.getY2());

}
return false;
}

@Override
public Object clone() {
MyRectangle aRect = new MyRectangle(this.getX1(), this.getX2(), this.getY1(), this.getY2(), this.getColor(),
this.getIsFull());
return aRect;

}

}
70 changes: 70 additions & 0 deletions maman12-Q2/src/MyShape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public abstract class MyShape implements Cloneable {

private int x1;
private int x2;
private int y1;
private int y2;
private Color color;

protected MyShape(int x1, int x2, int y1, int y2, Color color) {
super();
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.color = color;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public int getX1() {
return x1;
}

public void setX1(int x1) {
this.x1 = x1;
}

public int getX2() {
return x2;
}

public void setX2(int x2) {
this.x2 = x2;
}

public int getY1() {
return y1;
}

public void setY1(int y1) {
this.y1 = y1;
}

public int getY2() {
return y2;
}

public void setY2(int y2) {
this.y2 = y2;
}

public abstract void paint(Graphics g);

@Override
public abstract boolean equals(Object object);

@Override
public abstract Object clone();
}
13 changes: 13 additions & 0 deletions maman12-Q2/src/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//This class exists for the soul purpose of hiding any logic from the main function
public class Program {

public Program()
{
MyJFrame frame = new MyJFrame();
ShapesPanel aPanel = new ShapesPanel();
frame.add(aPanel);
frame.setVisible(true);

}

}
90 changes: 90 additions & 0 deletions maman12-Q2/src/ShapesPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.swing.JPanel;

//Contains most of the logic of the main program.
public class ShapesPanel extends JPanel {

private static final SecureRandom randomNumbers = new SecureRandom();
private static final int RANGE_OF_NUMBERS = 200;
private static final int NUMBER_TO_LOWER = 10;
private static final boolean FULL = true;
private static final boolean NOT_FULL = false;

private ArrayList<MyShape> shapes;
private ArrayList<MyShape> copiedShapes;

public ShapesPanel() {
this.shapes = new ArrayList<MyShape>();
this.copiedShapes = new ArrayList<MyShape>();

this.generateShapes(this.shapes);
this.copyShapes(this.shapes, this.copiedShapes);
this.makeChangesToShapes(this.copiedShapes);
}

private void copyShapes(ArrayList<MyShape> sourceArray, ArrayList<MyShape> copyArray) {
if (sourceArray != null && copyArray != null) {
for (int i = 0; i < sourceArray.size(); i++) {
copyArray.add((MyShape) sourceArray.get(i).clone());
}
}
}

private void generateShapes(ArrayList<MyShape> anArray) {
MyOval oval1 = new MyOval(randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.RED, FULL);
MyOval oval2 = new MyOval(randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.RED, NOT_FULL);
MyLine line1 = new MyLine(randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.MAGENTA);
MyLine line2 = new MyLine(randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.MAGENTA);
MyRectangle rect1 = new MyRectangle(randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.BLUE, NOT_FULL);
MyRectangle rect2 = new MyRectangle(randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), randomNumbers.nextInt(RANGE_OF_NUMBERS),
randomNumbers.nextInt(RANGE_OF_NUMBERS), Color.BLUE, NOT_FULL);

anArray.add(oval1);
anArray.add(oval2);
anArray.add(line1);
anArray.add(line2);
anArray.add(rect1);
anArray.add(rect2);
}

private void paintArray(ArrayList<MyShape> anArray, Graphics g) {
for (int i = 0; i < anArray.size(); i++) {
anArray.get(i).paint(g);
}
}

private void makeChangesToShapes(ArrayList<MyShape> anArray) {
// Lower down and right
for (int i = 0; i < anArray.size(); i++) {
MyShape temp = anArray.get(i);

// X1&X2 are the coordinates of the top left corner of the shape (And not x1,y1
// as described on the exercise)
temp.setX1(temp.getX1() + NUMBER_TO_LOWER);
temp.setX2(temp.getX2() + NUMBER_TO_LOWER);

// Change to opposite of fill status
if (temp instanceof MyBoundedShape) {
MyBoundedShape x = (MyBoundedShape) temp;
x.setFull(!x.getIsFull());
}
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.paintArray(shapes, g);
this.paintArray(copiedShapes, g);
}
}
6 changes: 6 additions & 0 deletions maman12/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions maman12/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
Loading