I'm a beginner in java programming and I would appreciate if someone can guide me as to how I've to proceed to obtain the wanted solution I am looking for. Ive attached screenshots of the outputs that I obtain from running the code. the threebythree.java code creates a 3x3 square grid and the rhombus.java creates a rhombus. What I'm trying to attain is to take the whole rhombus drawing and paste it into each square present in the grid. I have attached an image showing the required rough output that Im looking for. I will be really greatful if someone could guide me in the right direction.
result expected.JPG
threebythree.JPG
rhombus.JPG
threebythree.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class threebythree extends JPanel { int s = 150; public threebythree(){ setPreferredSize(new Dimension(450, 450)); setBackground(Color.WHITE); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; g2d.translate(60, 50); this.setBackground(Color.WHITE); for(int x=0; x<3; x++ ){ for(int y=0; y<3; y++){ g2d.drawRect(x*s, y*s, s, s); } } } }
rhombus.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class rhombus extends JPanel { public rhombus(){ JPanel jp2 = new JPanel(); setPreferredSize(new Dimension(450, 450)); setBackground(Color.WHITE); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d=(Graphics2D)g; int s = 100; this.setBackground(Color.WHITE); g2d.translate(275, 75); g2d.rotate(Math.toRadians(45)); for(int x=0; x<3; x++ ){ for(int y=0; y<3; y++){ g2d.drawRect(x*s, y*s, s, s); } } } }
testing.java
import java.awt.*; import javax.swing.*; public class zaax { public static void main(String[] args){ JFrame f = new JFrame("Testing"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new rhombus()); f.setSize(600, 600); f.setVisible(true); } }