Jump to content
  • Advertisement
Sign in to follow this  
Ueneth Echil

JAVA: Passing Graphics as a parameter or alternatives

This topic is 4235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hullo, I'm working on an End-of-Year programming assignment for my Java I class. It's suppose to be simply from what we've learned this year, which excludes passing Graphics and other alternatives than an applet and console window. What I'm currently trying to do, is create a file and class that constructs an image like, g.drawRect();, and g.fillOval();. Then I have another file with the paint method, constructing an object of the class. I've tried every way of passing and not passing and naming of Graphics. I read that you cannot pass Graphics, because it's passing an complex object, or it's being instantiated, or blah blah blah, it just cannot be done. So, just curious if there were any alternatives, such as using Swing/JFC/JFrame to setup the window, but still being able to use my usual code for creating graphics. (g.drawRect(). g.setColor()) It makes me mad, because how are we to create a sophisticated program when we cannot even using graphics in a proper OOP context. Unfortunately for this project we have to use graphics, so...

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what problem you are having with it, but you can certainly pass the graphics context as a parameter to whatever rendering function you like, i.e.
public void paint(Graphics g){
myRenderer.drawScene(g);
}

Share this post


Link to post
Share on other sites
Your class (the one that needs to render itself) should inherit from JComponent, and override paintComponent method.

The reason you cannot instantiate Graphics object for rendering is justified.

But there's never a need to do so. Do it as it was meant to be done.


class MyComponent extends JComponent
{
public void paintComponent( Graphics g )
{
drawBorder( g );
drawContent( g );
drawText( g );
}

private void drawBorder( Graphics g ) { .... }
private void drawContent( Graphics g ) { .... }
private void drawText( Graphics g ) { .... }

}




But keep in mind the following: You *MUST NOT* store this graphics object, and simply draw on it when you please. The rendering pipeline will provide you with some instance of Graphics when it deems it necessary. The Graphics will then contain clip bounds that need to be repainted, the offset, and all other parameters, including repainted background or anything else.

You *CANNOT* draw whenever you wish or in any part you like. Drawing is passive, and performed from Swing's repaint thread. Only, and only, render when your paintComponent gets called. Violating this will give you many problems, especially when mixing threads, let alone if things change, such as native Graphics implementation getting re-allocated, where you'd end up drawing on some non-existant context.

If you need to force component to render itself, use one of the appropriate methods, such as invalidate or repaint.

Drawing anything directly, especially in response to user input will quickly bring you into dead-lock when done from multiple threads.

Share this post


Link to post
Share on other sites
Here's my error:


PlanetEdge.java:15: cannot find symbol
symbol : method Team(int,java.awt.Graphics)
location: class PlanetEdge
Team(1, g);









Here's my code:



PlanetEdge.java

/**
* PlanetEdge
* Paul Adamski
*
* PlanetEdge.java
*/


import java.awt.*;
import java.applet.*;

public class PlanetEdge extends Applet
{
public void paint(Graphics g)
{
Team(1, g);
}
}




Team.java

/**
* PlanetEdge
* Paul Adamski
*
* Team.java
*/


import java.awt.*;
import java.applet.*;

public class Team
{
public Team(int teamChoice, Graphics g)
{
g.drawRect(10,10,50,50);
}
}



Share this post


Link to post
Share on other sites
This wouldn't happen to be gcj compiler, would it?

If so, then the AWT/Swing support might not be complete, or possibly even incompatible with Sun's implementation.

I know when I was last working with Java a few years back, gcj was essentially useless.

You probably need to include AWT/Swing jars into classpath. You might even need to download them separately.

If you do have a choice however, download Sun's JDK. Unless I'm mistaken, and this isn't gcj.

Share this post


Link to post
Share on other sites
Also unless you'll have something to do with the java.applet.* in Team later, this cast is useless.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!