3 Attachment(s)
Java Graphics: Drawing one object into another
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.
Attachment 700
Attachment 698
Attachment 699
threebythree.java
Code :
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
Code :
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
Code :
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);
}
}
Re: Java Graphics: Drawing one object into another
Well, how would you do this by hand? Get a piece of graph paper, and do the drawing, keeping track of the coordinates where you're drawing. Do you see a pattern?
Re: Java Graphics: Drawing one object into another
I can manually draw the rhombus into the 3x3 square grid, that is definitely one solution but what I want do is to call the drawing of rhombus into the 3x3 square grid that I have created in another class. I hope you get what I mean.
Re: Java Graphics: Drawing one object into another
Quote:
Originally Posted by
zaxahmed
I can manually draw the rhombus into the 3x3 square grid, that is definitely one solution but what I want do is to call the drawing of rhombus into the 3x3 square grid that I have created in another class. I hope you get what I mean.
Hmm, I'm not sure. What I'd do if I were you is create a method that draws a single square/rhombus at a specified coordinate, something like:
Code java:
public void drawSquare(int x, int y, Graphics){
//draw a single square with a rhombus inside it
}
Then just call that with whatever x and y value you want, in your loop. You could also add parameters for the width and height of the square.
Re: Java Graphics: Drawing one object into another
Re: Java Graphics: Drawing one object into another
Quote:
Originally Posted by
zaxahmed
Thank you very much!
No problem. Did you get it sorted out then?