Question
Write the program in java to replace the triangles of program Triangles.java with squares and draw a great many of them, arranged in a chessboard,
Write the program in java to replace the triangles of program Triangles.java with squares and draw a great many of them, arranged in a chessboard, as shown in Fig. below. As usual, this chessboard consists of n * n normal squares (with horizontal and vertical edges), where n = 8. Each of these actually consists of k squares of different sizes, with k = 10. Finally, the value q = 0.2 (and p = 1 - q = 0.8)was used to divide each edge into two parts with ratio p : q , but the interesting pattern of Fig. below was obtained by reversing the roles of p and q in half of the n * n normal squares, which is similar to the black and whites squares of a normal chessboard. Your program should accept the values n, k and q as program arguments
Triangle.java import java.awt.*; import java.awt.event.*; public class Triangles extends Frame { public static void main(String[] args) {new Triangles();} Triangles() { super("Triangles: 50 triangles inside each other"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); setSize(600, 400); add("Center", new CvTriangles()); setVisible(true); } } class CvTriangles extends Canvas { int maxX, maxY, minMaxXY, xCenter, yCenter; void initgr() { Dimension d = getSize(); maxX = d.width - 1; maxY = d.height - 1; minMaxXY = Math.min(maxX, maxY); xCenter = maxX / 2; yCenter = maxY / 2; } int iX(float x) {return Math.round(x);} int iY(float y) {return maxY - Math.round(y);} public void paint(Graphics g) { initgr(); float side = 0.95F * minMaxXY, sideHalf = 0.5F * side, h = sideHalf * (float) Math.sqrt(3), xA, yA, xB, yB, xC, yC, xA1, yA1, xB1, yB1, xC1, yC1, p, q; q = 0.05F; p = 1 - q; xA = xCenter - sideHalf; yA = yCenter - 0.5F * h; xB = xCenter + sideHalf; yB = yA; xC = xCenter; yC = yCenter + 0.5F * h; for (int i = 0; i Fig, 1. A chessboard of squares Fig, 1. A chessboard of squares
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started