Advertisement

[java] Creating instances of Image vars

Started by February 03, 2000 12:03 PM
5 comments, last by Smoo 24 years, 7 months ago
Well, I''m stuck again with another graphics related question (obviously still related to my game). When I''m frolicking around on map and I wish to talk to someone, I basically need a "window" (manually made FF series looking window type of deal) so I create another class and create the window in there because I''m also going to have to interact with the window. Now here''s my problem. I still want to see the background of the map that I''m walking around on so basically what I do is snapshot of the background, send it to the class handling my dialog window and it draws a window with the appropriate text on top of it. Well that''s all fine and dandy *except* when I take a snapshot of my background (composed of the tiles, then it draws the hero on top) well my hero dissapears and the image flickers for a sec before going to the TextDialog class. Phew! Now that my ranting and raving is over, the solutions I''m looking for is this: I need to create a copy of the map (this includes the little guy running around) but obviously I run into problems since everything is a pointer in here. So that''s problem 1. Problem 2 is the flickering. Well I know it''s going to flicker since I''m basically going from the public void paint(Graphics g) of one class to another but is there some way to still not make it flicker? Smoo
I''m assuming you''re double buffering. (If not, go get Java in a Nutshell, it''ll change your life. ) Instead of blasting the background from your main window to the TextDialog class, why not just draw on the back buffer graphics context directly from the TextDialog class? That should minimize flicker as well as eliminate the need to re-create the image.
Advertisement
Sounds like an easier way to do it would have the dialogs background be transparent or semi-transpartent. I''m not sure you can to it with a java.awt.Window .Frame or .Dialog, but i know you can do it with java.awt.Image and I''m almost sure you can do it with java.awt.Component. So i would make my own CharacterDialog subclass of Component and give its constructor the appropriate methods for setting up a border and background.
Are you trying to make a frame for the dialog or are you trying to "tint" the graphics behind the window? Either way, you could manually overwrite public void paint() with your own method, but that''s a massive pain in the neck. [You''d be on it for weeks.] Basically all you really need to is suppress the repaint() method to eliminate the flickering. Dang! I wish I could remember how to do this completely...
Okay, well first things first, yes I am using the doublebuffering method, I''d have to be nuts otherwise


Anyhoo, getting down to business. Using a transparent background.... hrm... that sounds very interesting... There''s only one slight problem with that, I have no clue how! I''m only starting off in Java (basically had a class in it and continued on from there) so alot of this stuff I''m really learning for the first time but hey, you gotta start somewhere! If you could give me a snippet of code on how to do that it would be greatly appreciated (or if you wish to point me to an URL that would be also appreciated) but the only question I have to your solution is: won''t it still flicker?.... hrm... perhaps not since the background is still transparent. *ponder* *ponder* *think* *think* *think* ... perhaps not after all. Well, it''ll definitely be something to try.

Oh and SiCrane, thanks for the input and since I didn''t explain myself well enough, that''s basically what I had done (if I understood you correctly). Image snapShot = backgroundBuffer and dump the snapShot to textDialog class but this did cause a flicker because it had to redraw the background. I needed the snapShot image and not the backgroundBuffer because it didn''t have the hero drawn at the time. it''s basically like
public void paint()
{
switch()
{
case MOVE_DOWN:
drawBackgroundBuffer();
drawHeroPic();

case MOVE_UP:
drawBackgroundBuffer();
drawHeroPic();
...
}

drawBackgroundBuffer();
}

obviously this is just an example and not accurate but I need to have the drawBackgroundBuffer below my case statement, that''s why I wanted to create a copy of the backgroundbuffer after I drew the heropic and not want it to be a pointer.

hopefully that''ll have explained a bit more the situation.

Smoo
http://java.sun.com/products/jdk/1.2/docs/api/

go there to explore all the methods an fields of any class about which you have questions. I'm not exactly sure why you are getting or what kind of flicker is going on so i will just ask if you have overrided repaint. If in your code of the class that gets painted on, a canvas perhaps, you have the method:

void repaint() {
...
}
then you overrode it. Inside you should just have a call to paint(); so that repaint won't do it's default action of clearing the screen then painting it.

as for the clearbackgrounds, the API says that instances of the java.awt.Component class are not opaque by default, I'm guessing that means transparent. So you would do something like this.
class CharacterDialog extends Component {  int Sizex, Sizey;  Image BGimage, Borderimage;  String Text;    public CharacterDialog(String s) {       setBGimage(something);       setBorder(something);       this.Text = s;    }    paint(graphics g) {       g.drawimage(BGimage);       g.drawimage(Borderimage);       g.drawString(Text);    // maybe this calls a function that scrolls or animates the text    }}

That's at least how I would go about it. Make the class in steps, first just see if you can get it to display a semitransparent image over your bg, then set a border for it, then have it display the text, then see about scrolling the text on to it.

Edited by - Jim_Ross on 2/3/00 10:50:48 PM
Advertisement
I''ve actually used this kind of thing under Java 2 once and the key thing to remember is that you''ve got to add() the text component to your game window, otherwise it won''t show up. It has something to do witht the internal mechanism of handling components and their drawing...

Once you have added the text component you can say to the text component setLocation(int,int) that places it where you want it to show up. And setText() to set the text you want. If you are using Java 2''s Swing components e.g. JTextField you can say setBorder(someBorder) and setOpaque(false)...

After you''ve displayed the text as long as you want you can simply remove() the text component from the game window. Or if you want to recycle (= use it to show another spoken text) the once created and added text component you can just say setVisible(false) and it''ll hide itself.
-Pasi Keranen

This topic is closed to new replies.

Advertisement