Introduction
Steps
Step 1
Make sure you have this template:
public class ParameterisedShapes{
public void setupGUI() {
UI.initialise();
UI.addButton("Clear", UI::clearPanes);
UI.addButton("China Flag", this::doChinaFlag);
UI.addButton("Quit", UI::quit);
UI.setDivider(0.3);
}
public static void main(String[] args){
ParameterisedShapes ps = new ParameterisedShapes();
ps.setupGUI();
}
}
### Step 2
First we will ask the user for the x and y position of the flag, as well as the width of the flag.
## Full Code
```java
import ecs100.*;
import java.awt.Color;
public class ParameterisedShapes{
public void doChinaFlag() {
double left = UI.askDouble("Left of flag: ");
double top = UI.askDouble("Top of flag: ");
double width = UI.askDouble("Width of flag: ");
double height = width / 3 * 2;
drawChinaFlag(left, top, width, height);
}
public void drawChinaFlag(double left, double top, double width, double height) {
UI.clearGraphics();
/*# YOUR CODE HERE */
UI.setColor(new Color(238, 28, 37));
UI.fillRect(left, top, width, height);
UI.setColor(new Color(255, 255, 0));
double square = width / 30;
drawStar(left + square * 5, top + square * 5, square * 3, 270);
drawStar(left + square * 10, top + square * 2, square * 1, 270 + 23.04);
drawStar(left + square * 12, top + square * 4, square * 1, 270 + 46.87);
drawStar(left + square * 12, top + square * 7, square * 1, 270);
drawStar(left + square * 10, top + square * 9, square * 1, 270 + 97.13);
}
public void drawStar(double midx, double midy, double rad, double angle) {
double outrad = rad;
double innrad = rad * 0.382;
int nPoints = 10;
double xPoints[] = new double[nPoints];
double yPoints[] = new double[nPoints];
for (int i = 0; i < nPoints; i++) {
if (i % 2 == 0) {
xPoints[i] = Math.cos(Math.toRadians(i * 36 + angle)) * outrad + midx;
yPoints[i] = Math.sin(Math.toRadians(i * 36 + angle)) * outrad + midy;
} else {
xPoints[i] = Math.cos(Math.toRadians(i * 36 + angle)) * innrad + midx;
yPoints[i] = Math.sin(Math.toRadians(i * 36 + angle)) * innrad + midy;
}
}
UI.fillPolygon(xPoints, yPoints, nPoints);
}
public void setupGUI() {
UI.initialise();
UI.addButton("Clear", UI::clearPanes);
UI.addButton("China Flag", this::doChinaFlag);
UI.addButton("Quit", UI::quit);
UI.setDivider(0.3);
}
public static void main(String[] args){
ParameterisedShapes ps = new ParameterisedShapes();
ps.setupGUI();
}
}