Skip to content

Commit a3510ae

Browse files
committed
complete interface examples with applet
1 parent fa84433 commit a3510ae

8 files changed

+130
-1
lines changed

src/section13/Colored.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package section13;
2+
3+
import java.awt.*;
4+
5+
/* Trivial interface that describes a Color getter */
6+
public interface Colored {
7+
Color getColor(); // method is implicitly public
8+
}

src/section13/ColoredDrawable.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package section13;
2+
3+
/* Trivial subinterface that extends both Colored and Drawable */
4+
public interface ColoredDrawable extends Colored, Drawable {
5+
// Nothing
6+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package section13;
2+
3+
import java.awt.*;
4+
5+
/* Enhance a ColoredPoint as drawable; implements ColoredDrawable since it also includes draw method */
6+
public class ColoredDrawablePoint extends ColoredPoint implements ColoredDrawable {
7+
8+
Color c;
9+
10+
public ColoredDrawablePoint(int x, int y, Color c) {
11+
super(x, y, c);
12+
}
13+
14+
public void draw(Graphics g) {
15+
System.out.println("Drawing ColoredDrawablePoint...");
16+
g.fillRect(x, y, 100, 100);
17+
}
18+
19+
}

src/section13/ColoredPoint.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package section13;
2+
3+
import section9.BasicPoint;
4+
5+
import java.awt.*;
6+
7+
/* Enhance a BasicPoint object with Color; implements Colored interface by defining getColor method */
8+
public class ColoredPoint extends BasicPoint implements Colored {
9+
10+
Color c;
11+
12+
/* Constructor for ColorPoint has to exist because otherwise it would call the default constructor, super().
13+
* However, super(), or BasicPoint() does not exist.
14+
* See: https://stackoverflow.com/q/7187799
15+
*/
16+
public ColoredPoint(int x, int y, Color c) {
17+
super(x, y);
18+
this.c = c;
19+
}
20+
21+
public Color getColor() { // must be public since it's part of Colored interface
22+
return c;
23+
}
24+
25+
}

src/section13/ColoredRectangle.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package section13;
2+
3+
import java.awt.*;
4+
5+
/* Implements a Rectangle class based on ColoredDrawable interface */
6+
public class ColoredRectangle implements ColoredDrawable {
7+
8+
Color c;
9+
int x1, x2, y1, y2;
10+
11+
public ColoredRectangle(int x1, int x2, int y1, int y2, Color c) {
12+
this.x1 = x1;
13+
this.x2 = x2;
14+
this.y1 = y1;
15+
this.y2 = y2;
16+
this.c = c;
17+
}
18+
19+
public Color getColor() {
20+
return c;
21+
}
22+
23+
public void draw(Graphics g) {
24+
System.out.println("Drawing colored rectangle...");
25+
g.fillRect(x1, y1, x2-x1, y2-y1);
26+
}
27+
28+
}

src/section13/Drawable.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package section13;
2+
3+
import java.awt.*;
4+
5+
/* Trivial interface that describes a draw method */
6+
public interface Drawable {
7+
void draw(Graphics g); // method is implicitly public
8+
}

src/section13/ShapeDrawingApp.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package section13;
2+
3+
import javax.swing.*;
4+
import java.applet.Applet;
5+
import java.awt.*;
6+
7+
/* This class uses the Colored* classes defined in the package */
8+
public class ShapeDrawingApp extends Applet {
9+
10+
/* Given an array of Colored, print out their colors */
11+
static void printColors(Colored[] cs) {
12+
for (Colored elem : cs) {
13+
System.out.println(elem.getColor().toString());
14+
}
15+
}
16+
17+
/* Draws the ColoredDrawable by using its interface methods */
18+
static void draw(Graphics g, ColoredDrawable[] cds) {
19+
for (ColoredDrawable cd : cds) {
20+
g.setColor(cd.getColor()); // get the color of Drawable with getColor()
21+
cd.draw(g); // draw the shape with draw(Graphics)
22+
}
23+
}
24+
25+
/* This method will be invoked by the Applet to draw shapes */
26+
@Override
27+
public void paint(Graphics g) {
28+
ColoredDrawablePoint p1 = new ColoredDrawablePoint(12, 12, new Color(12, 12, 12));
29+
ColoredRectangle r1 = new ColoredRectangle(105, 305, 205, 355, new Color(33, 75, 152));
30+
ColoredDrawable[] shapes = new ColoredDrawable[] { p1, r1 };
31+
32+
printColors(shapes);
33+
draw(g, shapes);
34+
}
35+
}

src/section9/BasicPoint.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class BasicPoint {
99
// Two non-static fields
10-
int x, y;
10+
public int x, y;
1111

1212
// One constructor
1313
public BasicPoint(int x, int y) {

0 commit comments

Comments
 (0)