Results 1 to 9 of 9
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default [solved] using paint methode in another class

    Im making a very simple tower defence game. This game consists of a map made out of squares. I use one class to display the map and would like to use the square class to display the square, because the square class holds all the information of the square needed to display it. My question is, what is the best way to do this?
    Last edited by imorio; 08-12-2010 at 12:56 AM.

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Just discovered that when I make a window and then call a paint methode in another class, it just paints in the current window.

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

    Default

    What context is the Graphics object referring to? The current window or the other one?

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What context is the Graphics object referring to? The current window or the other one?
    Well, I made a paint methode in class 1 and then send the graphics object into the paint methode of the second class. Then the paint methode of the second class painted in the window made by the first class.

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

    Default

    Many here would prefer to see the code or a small compilable representation of the code before deciding if they agree with your approach or what the best approach should be.

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Ok, this is the paint methode of class "editor", this class is in charge of the window:

    Java Code:
    public void paint(Graphics g)
    	{
    		super.paint(g);
    		for(int i=0; i<sizeOfField;i++)
    		{
    			for(int j=0;j<sizeOfField;j++)
    			{
    				playingField[i][j].paint(g);
    			}
    		}
    	}
    sizeOfField is a constant, the field consists of sizeOfField*sizeOfField squares.
    playingField is the 2d array which contains all squares.

    This is the paint methode of the square class:
    Java Code:
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		int[] xValues=new int[4];
    		xWaarden[0]=horizontalLocation*sizeSquare+4;
    		xWaarden[1]=horizontalLocation*sizeSquare+4+sizeSquare-1;
    		xWaarden[2]=horizontalLocation*sizeSquare+4+sizeSquare-1;
    		xWaarden[3]=horizontalLocation*sizeSquare+4;
    		int[] yValues=new int [4];
    		yWaarden[0]=verticalLocation*sizeSquare+30;
    		yWaarden[1]=verticalLocation*sizeSquare+30;
    		yWaarden[2]=verticalLocation*sizeSquare+30+sizeSquare-1;
    		yWaarden[3]=verticalLocation*sizeSquare+30+sizeSquare-1;
    		if(editMode)
    		{
    			g.setColor(Color.black);
    		}
    		else
    		{
    			g.setColor(curentColor);
    		}
    		p= new Polygon(xWaarden,yWaarden,4);
    		g.drawPolygon(p);
    		g.setColor(huidigeKleur);
    		g.fillPolygon(p);
    	}
    Vertical and horizontalLocation is the position of the square in the grid (starting at 0). The reason I add 4 and 30 is because the upper left corner isn't 0,0 in windows. editMode is a boolean that is for now always true.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by imorio View Post
    Ok, this is the paint methode of class "editor", this class is in charge of the window:

    Java Code:
    public void paint(Graphics g)
    	{
    		super.paint(g);
    		for(int i=0; i<sizeOfField;i++)
    		{
    			for(int j=0;j<sizeOfField;j++)
    			{
    				playingField[i][j].paint(g);
    			}
    		}
    	}
    sizeOfField is a constant, the field consists of sizeOfField*sizeOfField squares.
    playingField is the 2d array which contains all squares.

    This is the paint methode of the square class:
    Java Code:
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		int[] xValues=new int[4];
    		xWaarden[0]=horizontalLocation*sizeSquare+4;
    		xWaarden[1]=horizontalLocation*sizeSquare+4+sizeSquare-1;
    		xWaarden[2]=horizontalLocation*sizeSquare+4+sizeSquare-1;
    		xWaarden[3]=horizontalLocation*sizeSquare+4;
    		int[] yValues=new int [4];
    		yWaarden[0]=verticalLocation*sizeSquare+30;
    		yWaarden[1]=verticalLocation*sizeSquare+30;
    		yWaarden[2]=verticalLocation*sizeSquare+30+sizeSquare-1;
    		yWaarden[3]=verticalLocation*sizeSquare+30+sizeSquare-1;
    		if(editMode)
    		{
    			g.setColor(Color.black);
    		}
    		else
    		{
    			g.setColor(curentColor);
    		}
    		p= new Polygon(xWaarden,yWaarden,4);
    		g.drawPolygon(p);
    		g.setColor(huidigeKleur);
    		g.fillPolygon(p);
    	}
    Vertical and horizontalLocation is the position of the square in the grid (starting at 0). The reason I add 4 and 30 is because the upper left corner isn't 0,0 in windows. editMode is a boolean that is for now always true.
    A few minor nitpicks: if those components are Swing components better override the paintComponent( ... ) method instead of the paint( ... ) method.

    Note that a Graphics object is very capable of drawing (and filling) rectangles; there is no need to build a Polygon for that purpose.

    kind regards,

    Jos

  8. #8
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    A few minor nitpicks: if those components are Swing components better override the paintComponent( ... ) method instead of the paint( ... ) method.

    Note that a Graphics object is very capable of drawing (and filling) rectangles; there is no need to build a Polygon for that purpose.

    kind regards,

    Jos
    Thanks for the tip. Could you tell me which methode of graphics makes a rectangle? I'm looking in the database of oracle, but somehow, finding the right classes in there is pretty hard.

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

Similar Threads

  1. calling methode with array
    By koddy in forum New To Java
    Replies: 0
    Last Post: 06-30-2010, 01:53 PM
  2. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 04:24 PM
  3. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  4. Get "Frame"(?) as a methode
    By Mattedatten in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 12:31 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

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