Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Created March 8, 2024 08:55
Show Gist options
  • Save altbdoor/83f6a6b7af9463ff3ff24fc24b16f8b1 to your computer and use it in GitHub Desktop.
Save altbdoor/83f6a6b7af9463ff3ff24fc24b16f8b1 to your computer and use it in GitHub Desktop.
Drawing Czechia flag with Java AWT
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class CzechiaFlag {
static class CzechiaFlagComponent extends Component {
public void paint(Graphics g) {
// Get the width and height of the frame
int frameWidth = getWidth();
int frameHeight = getHeight();
int middleHeight = frameHeight / 2;
g.setColor(Color.white);
g.fillRect(0, 0, frameWidth, middleHeight);
g.setColor(Color.red);
g.fillRect(0, middleHeight, frameWidth, middleHeight);
// get radius (a^2 + b^2 = c^2)
int trianglePoint = frameWidth / 3;
double radius = Math.sqrt(Math.pow(trianglePoint, 2) + Math.pow(middleHeight, 2));
int radiusInt = (int)radius;
// get angle
double angle = Math.atan2(middleHeight, trianglePoint);
double angleDegrees = Math.toDegrees(angle);
int angleInt = (int)angleDegrees;
int circleSize = radiusInt * 2;
int xOffset = trianglePoint - radiusInt;
int yOffset = middleHeight - radiusInt;
// draw arc
g.setColor(Color.blue);
g.fillArc(xOffset, yOffset, circleSize, circleSize, 180 - angleInt, angleInt * 2);
}
}
public static void main(String[] args) {
Frame innerFrame = new Frame("czechia flag");
innerFrame.setSize(400, 300);
innerFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
innerFrame.add(new CzechiaFlagComponent());
innerFrame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment