Results 1 to 6 of 6
  1. #1
    jj007 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Display shape drawn from another class

    Hi I draw a shape in one class but i need it do actually appear when i play another class. The class I wish to play from extends from a gui class which will draw the frame. So all that is required is for the shape to draw.

    Rect class:

    Java Code:
    package work;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics2D;
    
    
    class Rect extends Shapes{
    
    	
    	public Rect(int x, int y, int w, int h) {
    		super(x,y,w,h);
    		
    	}
    
    	public void draw(Graphics2D g) {
    		g.setColor(Color.black);
    		g.fillRect(x, y, w, h);
    	}
    	
    	public static void main(String[] args) {
    	}
    
    }
    Test class which draws shape using gui:
    Java Code:
    package work;
    
    public class TestDraw extends Gui implements ShapeListener {
    
    	public TestDraw(){
    		Rect rect = new Rect(20, 20, 200, 100);
    		rect.addListener(this);
    		
    	}
    	
    	public static void main(String[] args) {
    		new TestDraw();
    		
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Display shape drawn from another class

    What is a Gui class? Are you supposed to add your Rect object to something? You create it, but don't do anything with it (that I can tell). What is the contract for the ShapeListener interface?

    Also, it's usually good practice to answer the replies in previous threads before starting a new one.

  3. #3
    jj007 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Display shape drawn from another class

    the gui class has the coding for jframe and jpanels etc. I can play draw the rect in the same class by saying:

    Java Code:
     Gui rect = new Rect(20,20,100,100);
    //Gui has a go method which repaints.
     rect.go();
    but how can i do something similar but actually call it in the other class?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Display shape drawn from another class

    There are too many unknowns here for me, seeing as how I have absolutely no knowledge of what's going on in your other classes.

  5. #5
    jj007 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Display shape drawn from another class

    i have tested something and it seems like theres a problem with the class constructor for rect...aparantly its not inputting the values into the draw method in order to draw the shape. Can you help?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,777
    Rep Power
    25

    Default Re: Display shape drawn from another class

    Do you get any error messages? If so please post.

    aparantly its not inputting the values into the draw method i
    Can you explain what happens and why you think this is happening?

    Can you post a small program that compiles, executes and shows the problem?

Similar Threads

  1. Strange extra shape drawn
    By eyebrow in forum New To Java
    Replies: 2
    Last Post: 10-26-2011, 12:18 AM
  2. A Shape Class
    By GoodApollo in forum AWT / Swing
    Replies: 7
    Last Post: 06-20-2011, 02:13 PM
  3. How can I display these objects from another class?
    By xx__rose in forum New To Java
    Replies: 5
    Last Post: 05-07-2010, 09:01 PM
  4. Replies: 1
    Last Post: 04-02-2010, 01:48 PM
  5. SWT Display class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •