Question
in the Wikipedia page Truchet Tiles , tiling the two-dimensional grid using four rotationally symmetric black-and-white tiles can produce surprising op-art geometric
in the Wikipedia page "Truchet Tiles", tiling the two-dimensional grid using four rotationally symmetric black-and-white tiles can produce surprising "op-art" geometric patterns. The four types of Truchet tiles are identiYied in this lab by two truth values parity and diag. The parameter diag determines whether the dividing line between black and white parts cuts through the main diagonal or the anti-diagonal, and the parameter parity determines which side is black and which side is white. In your labs project, create a new class
public class Truchet extends JPanel
to make your class to be a full-Yledged Swing component out of the box. Each instance of this class should then display the Truchet tiling deYined by its Yive constructor parameters.
public Truchet(int s, int w, int h, BiPredicate parity, BiPredicate diag)
Parameters w and h determine the width and height of the grid in tiles, and the parameter s determines the size of each tile in pixels. For example, an image made of values w=100, h=60 and s=5 would be 500 pixels wide and 300 pixels tall. Parameters parity and diag, both of the same type java.util.function.BiPredicate to encapsulate a function that takes two Integer parameters and returns a boolean result, provide your renderer the parity and diagonal of each tile based on its coordinates (x,y). This constructor should store its arguments into object Yields, and set the preferred size of this component to new Dimension(s*w,s*h).
public void paintComponent(Graphics g)
Renders the individual Truchet tiles on the surface of this component, the shape of each tile as determined by the strategy objects parity and diag to make these decisions. There are four possible ways to assign the these colours based on the parity and diagonal direction of the tile. However, to allow both the student and the grader to agree at a glance that this lab has been implemented correctly, your code should make these decisions in the exact manner that makes running the automated test class TruchetMain produce the result shown in the screenshot below.
(Unlike with JUnit test that do their work even if we close our eyes and pretend that we are no longer in the room, in this problem our eyeballs will have to make the Yinal determination whether the pattern is as expected. But at least our visual system offers a massive level of parallel processing right on the wetware that JUnit or any other computer program could not even dream of...)
Especially important is the Truchet instance at the top left corner of this 2-by-3 component grid, the simple black lozenge against the white background (or is it four white corner triangles against the black background, similar to the vase illusion, as told in the voice of Alan Watts), since that one
conYirms that your code uses the same scheme for colouring the tiles as in the instructor's private model solution. You might want to check out the modular arithmetic formulas used to produce the other instances where a pattern emerges from the local decisions that depend on the numerical properties of the tile coordinates. Even randomness is more pleasant after adding nonrandom things to balance it out so that the human visual system has something to latch on to interpret. (The TruchetMain.java class also has a method that uses Truchet tiling to showcase the use of basic punctuation characters of War and Peace, as inspired by the post "Punctuation in Novels".)
Once you get this component to work, you can also try out more artistic variations such as Smith tiles that use quarter circles instead of triangles to create a curved shape. Or create a labyrinth by using diag to draw a black diagonal line to separate the two white halves ignoring parity completely, in spirit of the classic Commodore 64 Basic one-liner. You could even subdivide some tiles into smaller tiles for a fractal subdivision effect, or explore the properties of random Truchet
ing of your art, since in this course, the only e acknowledge and allow bind our hands is the Law of Awesome!
TEST CODE: import javax.swing.BorderFactory; import javax.swing.JFrame; import java.awt.GridLayout; import java.util.Random; public class TruchetMain { public static void main(String[] args) { JFrame f = new JFrame("Truchet demo"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLayout(new GridLayout(2, 3)); Random rng = new Random(1234); boolean[][] p1 = new boolean[40][40]; boolean[][] d1 = new boolean[40][40]; for(int x = 0; x < 40; x++) { for(int y = 0; y < 40; y++) { p1[x][y] = rng.nextBoolean(); d1[x][y] = rng.nextBoolean(); } } Truchet[] truchets = { new Truchet(200, 2, 2, (x, y) -> y % 2 == 1, (x, y) -> (x+y) % 2 == 1), new Truchet(20, 20, 20, (x, y) -> (2*x+y) % 2 == 1, (x, y) -> (x+2*y) % 2 == 0), new Truchet(20, 20, 20, (x, y) -> ((x ^ y) & 4) != 0, (x, y) -> (x + y*y) % 2 == 0), new Truchet(20, 20, 20, (x, y) -> (x + y) % 2 == 0, (x, y) -> (3*x + 2*y) % 3 == 1), new Truchet(20, 20, 20, (x, y) -> (2*x + 7*y) % 3 == 0, (x, y) -> (4*x + 3*y) % 4 == 1), new Truchet(10, 40, 40, (x, y) -> p1[x][y], (x, y) -> d1[x][y]) }; for(Truchet truchet: truchets) { truchet.setBorder(BorderFactory.createRaisedBevelBorder()); f.add(truchet); } f.pack(); f.setVisible(true); } }
Step by Step Solution
3.56 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
Here is the Java code for the Truchet class import javaawt import javaawtgeom import javautilfunctionBiPredicate public class Truchet extends JPanel private int s private int w private int h private B...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