Skip to content

Commit 6a40991

Browse files
committed
Preliminary solutions for Exercise 5
1 parent c1808ca commit 6a40991

File tree

10 files changed

+757
-243
lines changed

10 files changed

+757
-243
lines changed

.idea/misc.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+498-243
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ex5/question1/MyFrame.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ex5.question1;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* This class IS a JFrame, since it extends it. What this means in
7+
* practice is that this class will have access to all non-private
8+
* methods of JFrame.
9+
*/
10+
class MyFrame extends JFrame {
11+
12+
public MyFrame() {
13+
this.setTitle("Programmering");
14+
this.setSize(300, 300);
15+
this.setVisible(true);
16+
}
17+
}

src/ex5/question1/MyFrame2.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ex5.question1;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* Created by christopher on 1/30/14.
7+
* <p/>
8+
* This class HAS a JFrame, and thus uses the Composition pattern,
9+
* rather than Inheritance. In practice, this means that we can use
10+
* the JFrame according to its public API only - we cannot access
11+
* inner members for the frame, as we could in the Inheritance example.
12+
*/
13+
public class MyFrame2 {
14+
15+
public MyFrame2() {
16+
JFrame frame = new JFrame();
17+
frame.setTitle("Programmering");
18+
frame.setSize(300, 300);
19+
frame.setVisible(true);
20+
}
21+
22+
public static void main(String[] args) {
23+
24+
}
25+
}

src/ex5/question1/question1.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ex5.question1;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* Created by christopher on 1/29/14.
7+
*/
8+
public class question1 {
9+
10+
public static void main(String[] args) {
11+
JFrame frame = new JFrame();
12+
frame.setTitle("Programmering");
13+
frame.setSize(300, 300);
14+
frame.setVisible(true);
15+
}
16+
}
17+

src/ex5/question2/MyMultiFrame.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ex5.question2;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* This custom JFrame places each new instance of itself
7+
* on a new area of the screen.
8+
*/
9+
class MyMultiFrame extends JFrame {
10+
11+
/*
12+
* We make use of these static (i.e. JVM-global) variables
13+
* in order to indicate what position the next frame should take.
14+
*
15+
* Note that we do not pay any special respect to upper bounds here -
16+
* if we create enough frames during a single run, we will eventually
17+
* end up placing them beyond the physical constraints of the users
18+
* monitor, effectively rendering them invisible after creation.
19+
*/
20+
private static int X_POSITION = 100;
21+
private static int Y_POSITION = 100;
22+
23+
public MyMultiFrame() {
24+
this.setTitle("Programmering");
25+
this.setSize(300, 300);
26+
27+
// Set the location, and then increase the coordinates.
28+
this.setLocation(X_POSITION, Y_POSITION);
29+
X_POSITION += 50;
30+
Y_POSITION += 50;
31+
32+
this.setVisible(true);
33+
}
34+
35+
public static void main(String[] args) {
36+
for (int i = 0; i < 10; i++) {
37+
new MyMultiFrame();
38+
}
39+
}
40+
}

src/ex5/question2/MyMultiFrame2.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ex5.question2;
2+
3+
import javax.swing.*;
4+
5+
public class MyMultiFrame2 {
6+
private static int X_POSITION = 100;
7+
private static int Y_POSITION = 100;
8+
9+
public MyMultiFrame2() {
10+
JFrame frame = new JFrame();
11+
frame.setTitle("Programmering");
12+
frame.setSize(300, 300);
13+
14+
frame.setLocation(X_POSITION, Y_POSITION);
15+
X_POSITION += 50;
16+
Y_POSITION += 50;
17+
18+
frame.setVisible(true);
19+
}
20+
21+
public static void main(String[] args) {
22+
for (int x = 0; x < 10; x++) {
23+
new MyMultiFrame2();
24+
}
25+
}
26+
}

src/ex5/question6/DrawFunction.java

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ex5.question6;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
6+
/**
7+
* Created by christopher on 1/30/14.
8+
* <p/>
9+
* Notice that our class IS a JFrame. We are creating a custom frame tailored
10+
* for function plotting, rather than reusing a basic JFrame.
11+
*/
12+
public class DrawFunction extends JFrame {
13+
14+
public DrawFunction() throws HeadlessException {
15+
16+
/*
17+
* We use a standard border layout, since we will only need
18+
* to consider a single component.
19+
*/
20+
setLayout(new BorderLayout());
21+
22+
/*
23+
* Here, we create and attach the actual "drawing surface to
24+
* the frame. The second parameter, BorderLayout.CENTER, tells
25+
* the system that the component should be placed at the center
26+
* of the frames layout.
27+
*/
28+
add(new DrawingBoard(), BorderLayout.CENTER);
29+
30+
/*
31+
* This defines what action to take when we close the frame (by
32+
* clicking the "X" in the top-right corner, if we are on a
33+
* Windows machine, for example). In our case, we shut down the
34+
* frame when it is closes.
35+
*/
36+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37+
38+
/*
39+
* Set the title to appear in the top-bar of the frame.
40+
*/
41+
setTitle("Draw");
42+
}
43+
44+
/**
45+
* This will be our "drawing board", where we do the actual plotting.
46+
* Our board is a JPanel, which is a multi-purpose component which
47+
* can be used to hold text, graphics, or other components.
48+
*/
49+
class DrawingBoard extends JPanel {
50+
51+
/**
52+
* The function which we will be plotting. Could easily be replaced by an
53+
* interface like we did in previous exercises.
54+
*
55+
* @param x
56+
* @return
57+
*/
58+
private double f(double x) {
59+
return x * x;
60+
}
61+
62+
@Override
63+
protected void paintComponent(final Graphics g) {
64+
super.paintComponent(g);
65+
66+
/*
67+
* First, we draw the arrows on our chart (to give it that nice, math-ish feeling).
68+
*/
69+
g.drawLine(390, 200, 370, 190);
70+
g.drawLine(390, 200, 370, 210);
71+
g.drawLine(200, 30, 190, 50);
72+
g.drawLine(200, 30, 210, 50);
73+
74+
/*
75+
* Draw the X and Y axes
76+
*/
77+
g.drawLine(10, 200, 390, 200);
78+
g.drawLine(200, 30, 200, 390);
79+
80+
/*
81+
* Draw the labels for the axes
82+
*/
83+
g.drawString("X", 390, 170);
84+
g.drawString("Y", 220, 40);
85+
86+
/*
87+
* To do the actual drawing, we use a Polygon.
88+
*/
89+
Polygon polygon = new Polygon();
90+
91+
for (int x = -40; x <= 40; x++) {
92+
polygon.addPoint(x + 200, 200 - (int) ((0.1) * f(x)));
93+
}
94+
95+
g.drawPolyline(polygon.xpoints, polygon.ypoints, polygon.npoints);
96+
}
97+
}
98+
99+
public static void main(String[] args) {
100+
final int FRAME_WIDTH = 400;
101+
final int FRAME_HEIGHT = 400;
102+
DrawFunction frame = new DrawFunction();
103+
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
104+
frame.setVisible(true);
105+
}
106+
}

src/ex5/question6/Test.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ex5.question6;
2+
3+
/**
4+
* Created by christopher on 1/30/14.
5+
*/
6+
public class Test {
7+
8+
public static void main(String[] args) {
9+
DrawFunction drawFunction = new DrawFunction();
10+
drawFunction.setVisible(true);
11+
}
12+
}

src/ex5/question7/NameFrame.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ex5.question7;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* Created by christopher on 1/30/14.
7+
*/
8+
public class NameFrame {
9+
10+
private final JFrame frame = new JFrame();
11+
12+
public NameFrame() {
13+
14+
}
15+
}

0 commit comments

Comments
 (0)