Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (307)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Painting to JPanel  (Read 1023 times)
0 Members and 1 Guest are viewing this topic.
Offline nick_coder

Junior Newbie




Java games rock!


« Posted 2003-01-16 23:58:06 »

Hi all,

Why does the following code only paint black to my JPanel. I am making a RPG and this is where i am up to, only the tile map.


//Engine for game

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class Engine extends JPanel{
 
FRW connecton;
Fight gui;
Tile[] tiles;
Image grass;
Image water;
BufferedImage screenImage;
Graphics screenGC;
int curX;
int curY;
int cutX;
int cutY;
boolean first = true;
int[][] mapdata = {
 
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0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,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0}};
 
 
 
public Engine( Fight gui ){
 screenImage = null;
 setSize(400,400);
 System.out.println("Starting Tracker");
 MediaTracker tracker = new MediaTracker(this);
 tracker.addImage(water, 0);
 tracker.addImage(grass, 1);
 try{
  tracker.waitForAll();
 }
 catch(Exception trackerx){ System.out.println("Tracker Failed"); }
 System.out.println("Tracker Done");
 this.gui = gui;  
 tiles = new Tile[2];
 System.out.println("Tiles GUI setup");
 curX = 0;
 curY = 0;
 cutY = 0;
 cutX = 0;
 
 
 System.out.println("Creating Image");
 grass = Toolkit.getDefaultToolkit().createImage("Grass.bmp");
 water = Toolkit.getDefaultToolkit().createImage("Water.bmp");
 
 System.out.println("Setting up KeyListener");
 
 this.addKeyListener( new KeyAdapter(){
   
  public void keyPressed( KeyEvent e){
   
   switch(e.getKeyCode()){
     
   case KeyEvent.VK_UP:
    scroll(1);
    break;
 
   case KeyEvent.VK_DOWN:
    scroll(3);
    break;
     
   case KeyEvent.VK_RIGHT:
    scroll(2);
    break;
     
   case KeyEvent.VK_LEFT:
    scroll(4);
    break;
   }
 repaint();
   
}
}
);
 
 tiles[0] = new Tile(grass, 40, 40);
 tiles[1] = new Tile(water, 40, 40);
 System.out.println("About to printMap");
 System.out.println("In paint setting up Graphics");  
 screenImage = new BufferedImage(480, 480, BufferedImage.TYPE_INT_RGB);
 System.out.println("Double Buffering");
 System.out.println("Setting up graphics");  
 screenGC = screenImage.createGraphics();
 first = false;
 drawMap();
 
 }  
   
public void drawMap(){
 
 for(int y = 0; y < 10; y++){
  for(int x = 0; x < 10; x++){
   tiles[mapdata[cutY + y][cutX + x]].paint(screenGC, curX, curY);
   curX += 40;
}
  curY += 40;
  curX = 0;
}
 System.out.println("About to repaint map");
 repaint();
}
 public void scroll(int dir){
 
}

public void paintComponent(Graphics g){

 super.paintComponent(g);

 System.out.println("In Paint");
 g.drawImage(screenImage, 0, 0, this);
 System.out.println("Painted");

}
 
public void distroy(){
 
 screenGC.dispose();
 
}

}


//Tile Class

import java.awt.*;
import java.awt.image.*;

public class Tile{
 
Image tileImage; //Hold tile image
int tileHieght; //Tile hieght
int tileWidth; //Tile width
 
public Tile(Image tile, int width, int hieght){
 
 tileImage = tile;
 tileHieght = hieght;
 tileWidth = width;
 
}
 
public void paint(Graphics g, int x, int y){
 System.out.println("In Tile Paint");
 Graphics g2 = g.create(x, y, 40, 40);
 g2.drawImage(tileImage, 0, 0,null);
 g2.dispose();
 System.out.println("Painted Image in Tile");
 
}
}
 
 
\\JFrame class - main

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;


public class Fight extends JFrame{
 
Engine bgmap;
 
public Fight(){
 
 super("MiniWars");
 
 System.out.println("In Constructor");
 bgmap = new Engine(this);
 System.out.println("Created Engine");
 Container hold;
 hold = getContentPane();
 hold.add(bgmap);
 
 setSize(400, 400);
 show();
 
 
}
 
public static void main( String args[] ){
 
 Fight app = new Fight();
 app.addWindowListener(
        new WindowAdapter() {
         public void windowClosing( WindowEvent e ){
          System.exit(0);
         }
       }    
      );
 
}
}



Any help would really be great....  
 

Offline markuskidd

Junior Member


Medals: 1



« Reply #1 - Posted 2003-01-17 00:30:48 »

tip #1: use the [ code ] tags to format code posts
tip #2: I don't see the setColor(Color c) method of a graphics object being invoked anywhere. I just did a quick scan, but that would probably be part of it.
Offline zparticle

Senior Member




Thick As A Brick


« Reply #2 - Posted 2003-01-17 02:05:55 »

The problem appears to be the way you are loading the grass and water images. I use png but I suppose bmp should work. At any rate I got it to draw something by changing the toolkit.creatImage calls to the following.
1  
2  
3  
4  
5  
6  
7  
    URL url = Engine.class.getResource("Grass.png");
    ImageIcon xi = new ImageIcon(url);  
    grass = xi.getImage();

    url = Engine.class.getResource("Water.png");
    xi = new ImageIcon(url);  
    grass = xi.getImage();

BTW please don't cross post in these forums.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (17 views)
2013-06-19 14:55:23

BrassApparatus (27 views)
2013-06-19 08:52:37

NegativeZero (30 views)
2013-06-19 03:31:52

NegativeZero (33 views)
2013-06-19 03:24:09

Jesse_Attard (36 views)
2013-06-18 22:03:02

HeroesGraveDev (73 views)
2013-06-15 23:35:23

Vermeer (74 views)
2013-06-14 20:08:06

davedes (75 views)
2013-06-14 16:03:55

alaslipknot (66 views)
2013-06-13 07:56:31

Roquen (90 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!