Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Graphics: Drawing one object into another

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
     
     
     
     
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    zaxahmed (August 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Graphics: Drawing one object into another

    Quote Originally Posted by zaxahmed View Post
    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:

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Graphics: Drawing one object into another

    Thank you very much!

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Graphics: Drawing one object into another

    Quote Originally Posted by zaxahmed View Post
    Thank you very much!
    No problem. Did you get it sorted out then?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 07:58 AM
  2. java grid and colors/graphics
    By ajmukon in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 25th, 2011, 03:38 PM
  3. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  4. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM
  5. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 05:16 PM