Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 1198 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,0,0},
 {0,0,0,0,0,0,0,0,0,0,0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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.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!