[java] Map isn't drawing properly.
My map is not drawing properly. You can add yuor own pics to it because I don''t have web page where I could put it up =( So I have to copy all here...
Moving doesn''t work and it''s not needed
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class rpg
{
public static void main(String args[])
{
smallwindow editor = new smallwindow();
editor.requestFocus();
editor.addWindowListener(new close());
}
}
class smallwindow extends Frame
{
Engine engine= new Engine();
public smallwindow()
{
super("Pieni Editori");
setSize(800, 600);
addKeyListener(new KeyEventHandler());
add(engine);
show();
}
class KeyEventHandler extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
switch(ke.getKeyCode())
{
case KeyEvent.VK_NUMPAD1: PC.moveSouthWest();
break;
case KeyEvent.VK_NUMPAD2: PC.moveSouth();
break;
case KeyEvent.VK_NUMPAD3: PC.moveSouthEast();
break;
case KeyEvent.VK_NUMPAD4: PC.moveWest();
break;
case KeyEvent.VK_NUMPAD5: PC.moveHold();
break;
case KeyEvent.VK_NUMPAD6: PC.moveEast();
break;
case KeyEvent.VK_NUMPAD7: PC.moveNorthWest();
break;
case KeyEvent.VK_NUMPAD8: PC.moveNorth();
break;
case KeyEvent.VK_NUMPAD9: PC.moveNorthEast();
break;
case KeyEvent.VK_ESCAPE: System.exit(0);
break;
}
// check bounds
if(PC.focusX < 0) PC.focusX = 0;
if(PC.focusY < 0) PC.focusY = 0;
if(PC.collisionbox().intersects(engine.westEdge)) PC.focusX = PC.focusX + 1;
if(PC.collisionbox().intersects(engine.northEdge)) PC.focusY = PC.focusY + 1;
if(PC.collisionbox().intersects(engine.eastEdge)) PC.focusX = PC.focusX - 1;
if(PC.collisionbox().intersects(engine.southEdge)) PC.focusY = PC.focusY -1;
engine.repaint(0,0,608,544);
}
}
} // smallwindow ends
class close extends WindowAdapter
{
public void windowClosing(WindowEvent event) {
System.exit( 0 );
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Engine extends Canvas
{
public int viewX, viewY;
//ImageManager
ImageManager imgMgr;
//Declaration of images
Image grassImages;
Image grassTiles[] = new Image[40];
Image PCImages;
Image PCTiles[]=new Image[8];
//Declaration of offscreen buffer
Graphics offScreen;
Image buffer;
//Test map
MapData map[][] = new MapData[32][32];
//Scroll borders
public final static Rectangle eastEdge = new Rectangle(512,0,94,544);
public final static Rectangle southEdge = new Rectangle(0,448,608,94);
public final static Rectangle westEdge = new Rectangle(0,0,94,544);
public final static Rectangle northEdge = new Rectangle(0,0,608,94);
//Map border
protected final static Rectangle mapBorder = new Rectangle(96,96,1076,916);
//Personal Character
PC pc;
//Loaded
private boolean loaded;
//Movement area
Dimension Area = new Dimension(13,11);
public Engine()
{
System.out.println("GoDEngine intialized");
viewX = 0;
viewY =0;
}
protected void loadMap()
{
int i,j;
int transform_map[][] = {
//Map {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0}};
//Transform the map into map structure
for(i=0;i<32;i++)
{
for(j=0;j<32;j++)
{
map[j] = new MapData();
switch(transform_map[j])
{
case 0: map[j].num = 1;
//map[j].tile[0] = 0;
break;
case 1: map[j].num = 1;
map[j].tile[0] = 1;
break;
case 2: map[j].num = 1;
map[j].tile[0] = 2;
break;
case 3: map[j].num = 3;
map[j].tile[0] = 3;
break;
case 4: map[j].num = 2;
map[j].tile[0] = 3;
break;
case 5: map[j].num = 2;
map[j].tile[0] = 4;
break;
case 6: map[j].num = 1;
map[j].tile[0] = 5;
break;
case 7: map[j].num = 4;
map[j].tile[0] = 5;
break;
case 8: map[j].num = 3;
map[j].tile[0] = 4;
break;
}
}
}
}
public void init()
{
imgMgr = new ImageManager(this);
grassImages = getToolkit().getImage(getClass().getResource("images/tiles.gif"));
PCImages = getToolkit().getImage(getClass().getResource("images/pc.gif"));
//Loading graphics
loadImages();
//Intialization of Personal Character
pc = new PC(PCTiles, offScreen,this);
//Map
loadMap();
//Intialization of double buffering
buffer= createImage(608,544);
offScreen = buffer.getGraphics();
loaded = true;
}
public void update(Graphics g)
{
if (loaded == false)
{
init();
}
drawMap(viewX, viewY);
pc.paint(offScreen);
g.drawImage(buffer,0,0,this);
}
final public void paint(Graphics g)
{
update(g);
}
protected void loadImages()
{
int num=0;
for(int i=0; i<4; i++)
{
for(int j=0;j<10;j++)
{
grassTiles[num++] = imgMgr.getCroppedImage(grassImages, j*32,i*32,32,32);
}
}
num=0;
for(int i=0; i<4; i++)
{
for(int j=0;j<2;j++)
{
PCTiles[num++] = imgMgr.getCroppedImage(PCImages, j*32,i*32,32,32);
}
}
for(int i= 0; i <39; i++)
{
imgMgr.trackImage(grassTiles, this);
}
for(int i= 0; i <7; i++)
{
imgMgr.trackImage(PCTiles, this);
}
System.out.println("Images are loaded");
}
protected void drawMap(int x, int y)
{
int i,j,k;
int mapx,mapy,mx,my,tx,ty,xo,yo,xa,ya,screenx,screeny;
int length;
int tile_to_draw,height_to_draw;
int sprite_mx,sprite_my,spritex,spritey,xx,yy;
mapx = x;
xo = x;
mapy = y;
yo = y;
while(true)
{
mx = mapx;
my = mapy;
screeny = 32;
//for each row
for(i=0;i<38;i++)
{
tx = mx;
ty = my;
screenx = 32;
length = 19;
//Check to see if this is an odd row
if((i & 0) == 0)
{
screenx-=32;
}
if((i & 1) == 0)
{
screeny-=32;
}
//For each tile in row
for(j=0;j<length;j++)
{
//for each tile in cell
for(k=0;k<map[ty][tx].num;k++)
{
tile_to_draw = map[ty][tx].tile[k];
offScreen.drawImage(grassTiles[tile_to_draw], screenx, screeny, this);
}//for each tile
screenx+=32;
// map update and wrap around
if((tx+=1) > 31)
tx = 0;
if((ty-=1) < 0)
ty = 31;
}//for each tile in row
screeny+=32;
if((i & 1) == 1)
{
if((mx+=1) > 31)
mx = 32;
} else
{
if((my+=1) > 31)
my = 32;
}
}//for each row
break;
}//while
repaint();
}//drawMap
}
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
/*****************************************************************
Handles opening / retrieving of images from disk
*****************************************************************/
public class ImageManager
{
Canvas canvas;
public ImageManager(Canvas c)
{
this.canvas = c;
}
public Image getCroppedImage(Image img, int x, int y, int width, int height)
{
ImageProducer cropped = new FilteredImageSource(img.getSource(),
new CropImageFilter(x,y,width,height));
return canvas.createImage(cropped);
}
public void trackImage(Image img, Component cmp)
{
MediaTracker tracker = new MediaTracker(cmp);
tracker.addImage(img, 0);
try
{
tracker.waitForID(0);
if(tracker.isErrorAny())
{
System.out.println("Error loading image");
}
}
catch (Exception ex) { ex.printStackTrace(); }
}
}
public class MapData
{
public int num;
public int tile[];
public int height[];
public int layer[];
public MapData(){
tile = new int[10];
height = new int[10];
layer = new int[10];
}
}
import java.awt.*;
import java.awt.Image.*;
public class PC
{
static Image faceUP, faceDOWN, faceLEFT, faceRIGHT, faceUP2, faceDOWN2, faceLEFT2, faceRIGHT2;
static Image PC;
Canvas canvas;
public static int focusX = 9; // which tile gets the hilite
public static int focusY = 8;
public PC(Image PCImages[], Graphics offScrn, Canvas c)
{
faceUP = PCImages[0];
faceUP2 = PCImages[1];
faceDOWN = PCImages[4];
faceDOWN2 = PCImages[5];
faceLEFT = PCImages[6];
faceLEFT2 = PCImages[7];
faceRIGHT = PCImages[2];
faceRIGHT2 = PCImages[3];
PC=faceDOWN;
canvas = c;
System.out.println("GoDPC intialized");
}
final public static void moveNorthWest()
{
focusX–;
focusY–;
if(PC==faceUP){
PC=faceUP2;
}
else{
PC=faceUP;
}
}
final public static void moveNorth()
{
focusY–;
if(PC==faceUP){
PC=faceUP2;
}
else{
PC=faceUP;
}
}
final public static void moveNorthEast()
{
focusX++;
focusY–;
if(PC==faceUP){
PC=faceUP2;
}
else{
PC=faceUP;
}
}
final public static void moveEast()
{
focusX++;
if(PC==faceRIGHT){
PC=faceRIGHT2;
}
else{
PC=faceRIGHT;
}
}
final public static void moveSouthEast()
{
focusY++;
focusX++;
if(PC==faceDOWN){
PC=faceDOWN2;
}
else{
PC=faceDOWN;
}
}
final public static void moveSouth()
{
focusY++;
if(PC==faceDOWN){
PC=faceDOWN2;
}
else{
PC=faceDOWN;
}
}
final public static void moveSouthWest()
{
focusY++;
focusX–;
if(PC==faceDOWN){
PC=faceDOWN2;
}
else{
PC=faceDOWN;
}
}
final public static void moveWest()
{
focusX–;
if(PC==faceLEFT){
PC=faceLEFT2;
}
else{
PC=faceLEFT;
}
}
final public static void moveHold()
{
PC=faceDOWN;
}
public void update(Graphics offScreen)
{
offScreen.setClip(0, 0, 608, 544);
offScreen.drawImage(PC ,focusX * 32, focusY * 32,
canvas);
}
public final void paint(Graphics offScreen)
{
update(offScreen);
}
public final static Rectangle collisionbox()
{
return new Rectangle((int)(focusX * 32 ),
(int)(focusY * 32 ),
32, 32 );
}
}
</code>
It should draw in line but now it draws - 45 degrees to wrong way =(…
</i>
That''s way too much code to look at on a web page. Howsabout you get a free web acount, put up a screenshot, and just the code that you feel is problematic...?
Yeah, maybe, but I don''t know where are these free web accounts?
Time comes, time goes and I only am.
Time comes, time goes and I only am.
April 06, 2000 01:23 AM
xoom.com, 50megs.com, homestead.com, yahoo.com the list could go on forever
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement