Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / Java 2D / Painting to JPanel on: 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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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....  
 

2  Java Game APIs & Engines / Java 2D / Painting to JPanel? on: 2003-01-16 04:58:23
Hi all,

Sorry for the newbi question but i am stuck.

I am creating a RPG and i am using tiles. Any way i paint the tiles to a Graphics Object which is created by bi.createGraphics().
I also have a buffered image (bi).

This is for the Double Buffering

When i call paint component in my class which extends JPanel it just pains black even though on my out put screen it says it has paint all the tile to the offscreen and then called paint. And then says painted.
Whats happening.

Any help would be great.
3  Java Game APIs & Engines / Java 2D / Scrolling Background - Tiles? on: 2003-01-10 06:34:33
Hi all,

I have been programming java for about a year now and i have a fairly good understanding. But when it comes to RPG games i am so lost.

Really the main question i need to ask is how do i make the "background move". Like in all the other RPG games, like i am walking and the back ground scrolls. Do i need to first create a massive image. And what gui would i use. I am so lost any help would be great.

I cant wait for ur reply.

Even a tutorial would be really helpful.
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
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!