This exercise requires the SaxionApp library. Please download this library using the link provided at relevant links.
Write a program that allows you to draw a rectangle, circle and square at any position and in any colour. To do this, write a super class Shape and its subclasses Rectangle, Circle and Square.
With all Shape instances you should be able to do the following:
To make life easier, feel free to use our run()
-method:
public void run() {
SaxionApp.turnBorderOff();
Rectangle[] rectangles = {
// Rectangle(x, y, width, height, color)
new Rectangle(50, 50, 200, 100, Color.RED),
new Rectangle(50, 250, 300, 150, Color.GREEN),
new Rectangle(50, 450, 400, 50, Color.YELLOW)
};
Circle[] circles = {
// Circle(x, y, radius, color)
new Circle(100, 100, 50, Color.RED),
new Circle(100, 300, 75, Color.GREEN),
new Circle(100, 600, 100, Color.YELLOW)
};
Square[] squares = {
// Square(x, y, size, color)
new Square(50, 50, 50, Color.RED),
new Square(50, 250, 250, Color.GREEN),
new Square(50, 600, 100, Color.YELLOW)
};
// Print all rectangles
for(Rectangle rectangle : rectangles) {
rectangle.draw();
int textX = 700;
int textY = rectangle.getY();
SaxionApp.drawBorderedText("Circumference: "+ rectangle.getCircumference() + " (pixels)", textX, textY, 20);
SaxionApp.drawBorderedText("Area: "+ rectangle.getArea() + " (pixels)", textX, textY + 20, 20);
}
SaxionApp.pause();
SaxionApp.clear();
// Print all circles
for(Circle circle : circles) {
circle.draw();
int textX = 300;
int textY = circle.getY() - (int) (0.5 * circle.getRadius());
SaxionApp.drawBorderedText("Circumference: "+ circle.getCircumference() + " (pixels)", textX, textY, 20);
SaxionApp.drawBorderedText("Area: "+ circle.getArea() + " (pixels)", textX, textY + 20, 20);
}
SaxionApp.pause();
SaxionApp.clear();
// Print all squares
for(Square square : squares) {
square.draw();
int textX = 500;
int textY = square.getY();
SaxionApp.drawBorderedText("Circumference: "+ square.getCircumference() + " (pixels)", textX, textY, 20);
SaxionApp.drawBorderedText("Area: "+ square.getArea() + " (pixels)", textX, textY + 20, 20);
}
}