Hey there,


My problem is probably simple, but I just can't seem to find a solution to it. I'm currently working on some sort of small RPG. Everything's running smoothly, but since I implemented the possibility to move your character, drawn Images tend to disappear randomly or not appear at all.


The prog is supposed to work like this:
-floor tiles (read: Indexes of the Image Array) are read from a txt file
-another class starts the Canvas.paint() function with the parameter "init", which proceeds to draw all floor tiles (indexed by previously read values stored in an Int matrix)
-once the character moves, the tile he previously stood on gets repainted (Canvas.paint() with the parameter "redraw") and the character gets painted onto the next tile (Canvas.paint() with the parameter "drawchar")


Things that actually DO work:
-drawing the interface (img igen[0]) and initiating the floor graphics as long as the character DOES NOT get drawn atop.
see http://img340.imageshack.us/img340/2866/uo0146ej0.jpg
-after the floor/interface is lost (read below) the tiles I'd stood on before stay (as they are supposed to be)
see http://img340.imageshack.us/img340/4461/uo0157bx9.jpg


Things that aren't quite supposed to be the way they are:
-if I draw the interface and floor, then draw the character, the canvas is white and only the character is shown
see http://img340.imageshack.us/img340/1474/uo0152ic2.jpg
-within the first 20 steps after this, anything but the character itself gets reset to white randomly 2-3 times


All I want is my Canvas to not reset things I've previously drawn onto it. I'm pretty much used to direct graphics memory access (?), as the only thing close to this I've ever done was programming cheap games in Mode13h (C++/for DOS)
Hope you guys can help me, because this is actually meant as a school project. Meh, I guess I should've chosen something like Tic Tac Toe, just like the rest of my class, haha.

Thanks in Advance,
F.

Btw. I added some of my Canvas code

import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.lang.Object;
import java.io.*;                   //tons of random imports, just to be sure
public class MyCanvas extends Canvas
{
  Game t;              //main class
  Generic gen;       //class with generic functions
  String para;         // parameter to determine the actual function of  paint()
  int ipara,xpos,ypos; // same as above
  int i,j;              //predeclaration of loop variables
  Toolkit kit = Toolkit.getDefaultToolkit();
  MediaTracker mt = new MediaTracker(this);
 
  Image igen[]   = new Image[10];
  Image ifloor[] = new Image[10]; 
  Image ichar[]  = new Image[5];
 
  public void setParameter(String parameter,int iparameter, int xpara, int ypara)
  {
    para = parameter;
    ipara = iparameter;
    xpos = xpara;
    ypos = ypara;
  }
 
 
  public MyCanvas(Game game, Generic G)
  {
    super();
    this.t = game;
    this.gen = G;
    {
      igen[0] = kit.getImage("img\\generic\\interf.png");
  
      ifloor[0] = kit.getImage("img\\floor\\grass.png");
      ifloor[1] = kit.getImage("img\\floor\\cobble.png");
      ifloor[2] = kit.getImage("img\\floor\\dirt.png");
      ifloor[3] = kit.getImage("img\\floor\\stone.png");
      ifloor[4] = kit.getImage("img\\floor\\wood.png");
   
      ichar[0]  = kit.getImage("img\\char\\c00.png");
      ichar[1]  = kit.getImage("img\\char\\c30.png");
      ichar[2]  = kit.getImage("img\\char\\c60.png");
      ichar[3]  = kit.getImage("img\\char\\c90.png");
    }
    {
    mt.addImage(igen[0],0);
    mt.addImage(ifloor[0],1);
    mt.addImage(ifloor[1],2);
    mt.addImage(ifloor[2],3);
    mt.addImage(ifloor[3],4);
    mt.addImage(ifloor[4],5);
    mt.addImage(ichar[0],6);
    mt.addImage(ichar[0],7);
    mt.addImage(ichar[0],8);
    mt.addImage(ichar[0],9);
    }  
    try {mt.waitForAll();}
    catch (InterruptedException e) {}
  }
    
    
    
  public void paint(Graphics g)
  // called by diff subclass using: 
  // t.canvasobject.paint(t.canvasobject.getGraphics());
  // t being the main window

  {
    if (para=="init")
    {
      for(i=1;i<18;i++)
        for(j=1;j<16;j++)
          g.drawImage(ifloor[t.afloor[i][j]],gen.getx(i),gen.gety(j),this);
          //draw a floor image array according to the floor type saved in the afloor maxtrix, repeat until entire game area is drawn
      g.drawImage(igen[0],0,0,this);
      //draw the interface (with a transparent middle part) around the actual game area 
    }
 

    if(para=="redraw")
    {
      g.drawImage(ifloor[t.afloor[xpos][ypos]],gen.getx(xpos),gen.gety(ypos),this);
    // redraws a certain tile of the floor grid (eg. the one the character just left)
    }
 

    if (para=="drawchar")
    {
      g.drawImage(ichar[(t.you.facing/3)],gen.getx(xpos),gen.gety(ypos),this);
    // draws the character on top of the previously drawn floor tile
    // (eg. the one the character just moved onto)
    }
  }  //end of paint function
    
} // end of class declaration
Member Avatar for iamthwee

Wouldn't the same logic be applied to tic tac toe though?

I mean you would just be repainting the canvas for 'x' and 'o' in the same way you would be repainting the guy as he moves around in your rpg.

Unless the problem arises when you have to repaint over a square that is already painted?

If you use a transparent .gif as the man does that make any difference?

Why yes, I do use images with transparent backgrounds. png though, not gif.
I greatly appreciate your quick reply and all those links, but even after 2h of reading I don't think I've gotten any further...
My main concept was based on drawing right away via paint() -completely disregarding repaint() and update()- 'cause it seemed way easier my way.

Goin' to try to backup everything and rewrite it according to those tutorials.

By the way, to carify, the problem is the following:

X/Y = opaque; O = transparent;
 
XXXX    YYOO
XXXX    OYYO
XXXX    OYYY
 
img1     img2

Loading img2 on top of img1, so I get:

YYXX
XYYX
XYYY

Which basically is not a problem but in some cases results in the canvas just being reset to white.

F.

Member Avatar for iamthwee

Goin' to try to backup everything and rewrite it according to those tutorials.

Yeah do that. I bet you it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.