Show Posts
|
|
Pages: [1]
|
|
3
|
Java Game APIs & Engines / Java 2D / Help Me: I don't know whats wrong with my code
|
on: 2003-02-02 18:22:01
|
This is the first time I am using threads. In my code below, i've tried to use threads with double buffering, and although the code compiles, I see nothing on the screen, can you please help me. Thanx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import java.awt.*; import javax.swing.*; import java.awt.event.*;
class Game extends JPanel implements ActionListener, Runnable { int X = 40,Y = 40; private ImageIcon images[]; private static final int TOTAL_IMAGES=7; private int currentImage = 0; boolean running = true; public static Image offscreen; public Game() { setVisible(true); setupAnimation(); setIgnoreRepaint(true); } public void setupAnimation() { this.setSize(this.getPreferredSize()); this.images = new ImageIcon[TOTAL_IMAGES]; for (int i = 0; i < images.length; i++) { images[i] = new ImageIcon("pac"+i+".gif"); } } public void run() { offscreen= createImage(400,400); Graphics buff = offscreen.getGraphics(); buff.setColor(Color.black); buff.fillRect(0,0,400,400); try { while (running) { buff.drawImage(images[currentImage].getImage(), X, Y, this); images[currentImage].paintIcon(this, buff, X, Y); currentImage = (++currentImage) % TOTAL_IMAGES; paint(buff); Thread.sleep(30); } } catch (Exception e) {} } public void paint(Graphics g) { if (offscreen != null) { g.drawImage(offscreen, 0, 0, this); } } public void actionPerformed(ActionEvent e){ repaint(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getPreferredSize (){return new Dimension(400,400);} public static void main(String agrs[]) { Game ji = new Game(); JFrame main = new JFrame("PacMan"); main.getContentPane().add(ji, BorderLayout.CENTER); main.pack(); main.show(); Thread t = new Thread(ji); t.start(); } } |
|
|
|
|
|
5
|
Java Game APIs & Engines / Java 2D / Re: Need advice on Active Rendering. Please help
|
on: 2003-01-16 17:43:26
|
I have spent the past week trying to convert my code but without success. To be honest, I don't have that much experience in java, and this thing is giving me a headache. I am totally confused. below is a simple animation code in passive rendering (similar to the code that I use in my game). Can you please help me to convert it to active rendering. If you have the time to help me with this, then I am sure I will be able to do it for the rest of my game. I hope I'm not asking for too much. Thanx The below code uses 7 GIF images for pacman animation. ----------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class PacmanM extends JPanel implements ActionListener { private ImageIcon images[]; private static final int TOTAL_IMAGES=7, ANIM_DELAY=50; private int currentImage = 0; private Timer animationTimer; private int delay = 0;
public PacmanM(int delay) { this.delay = delay; setupAnimation(); } public PacmanM() { setupAnimation(); } public void setupAnimation() { this.setSize(this.getPreferredSize()); this.images = new ImageIcon[TOTAL_IMAGES];
for (int i = 0; i < images.length; i++) { images[i] = new ImageIcon("pac"+i+".gif"); } startAnimation(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.fillRect(0,0,this.getWidth(), this.getHeight()); if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE) { images[currentImage].paintIcon(this, g, 0, 0); currentImage = (++currentImage) % TOTAL_IMAGES; } } public void startAnimation() { if (animationTimer == null) { currentImage = 0; animationTimer = new Timer( ANIM_DELAY + delay, this); animationTimer.start(); } else { if (!animationTimer.isRunning()) animationTimer.restart(); } } public void stopAnimation (){ animationTimer.stop(); } public void actionPerformed(ActionEvent e){ repaint(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getPreferredSize (){return new Dimension(400,400);} public static void main(String[] args ) { PacmanM PacMan = new PacmanM(); JFrame main = new JFrame("PacMan"); main.getContentPane().add(PacMan, BorderLayout.CENTER); main.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); main.pack(); main.show(); } } |
|
|
|
|
|
6
|
Java Game APIs & Engines / Java 2D / Re: Need advice on Active Rendering. Please help
|
on: 2003-01-16 12:37:37
|
|
Well the problem is that in my game, almost all the logic is performed in the paint/paintComponent method. From what I read in Sun's website, this is passive rendering and is not really good idea to use for a game.
But the problem is I have not been able to find much information and tutorials about active rendering. So I don't exactly know how to use it for my game.
>Pacman should easily perform with 500fps on a modern box, even with Java.
500 fps, wow!!!! and I thought getting 33 fps is ideal.
|
|
|
|
|
7
|
Java Game APIs & Engines / Java 2D / Need advice on Active Rendering. Please help
|
on: 2003-01-16 09:34:22
|
|
I dont have that much experience in Java, and only a few days ago I realised that I need to use Active rendering instead of passive rendering for my pacman game. The game runs fine when I run it on my own PC (P4 2Ghz 512 ram) and I get around 33 fps. But if I run it on computers with lower specs (below 1Ghz) the game runs really slow (about 17 fps). I know this could be because of passive rendering since pacman is not really a processor hungry application to use all of the system's recources.
The problem is I can't really find much information on active rendering (except the article on Sun's website). Could you point my to some tutorials or examples on active rendering. (or if you could give me some guidelines)
Thanx
|
|
|
|
|
9
|
Java Game APIs & Engines / Java 2D / Problem with repainting of the screen: Please Help
|
on: 2003-01-08 21:24:42
|
Ok I have three files, the first is my Pacman game (File is called Game.java), that deals with displaying the main interface of the game (pacman animation, etc). I am trying to keep track of the number of food bits remaining in the maze. So I have the second file responsible for receiving the variable FOOD_COUNTER from Game.java, and then paint the value on the screen ( This file is called Score.java). See the codes below. Now the third file is a layout manager that uses GridBag layout, and is suppose to add the two above files to the main interface. The problem I have with running the files, is that when the value of the FOOD_COUNTER is updated in Game.java (ie when pacman eats a bit food in the maze), the new value is not painted to the screen in Score.java. HOWEVER, if I minimize the window and maximize it again, I see the new value of the FOOD_COUNTER painted on the screen. I can't figure out how to sort this problem. (if I try to add repaint() to the paintComponent() method in Score.java the program runs, but it slows down horribly). Can you please help me? Thanks Here are the codes This code is responsible for Pacman animation, adding the food to the maze etc. -------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Game extends JPanel implements ActionListener{
private int forwardBy = 5; private int upBy = 0; private int xANSWER = 0; private int yANSWER = 0; public int xCount = 315; private int yCount = 525; public static int FOOD_COUNTER = 140; private int CAPSULE_COUNTER = 4; private Timer animationTimer; static final int ANIM_DELAY=31; PacmanM pac = new PacmanM(); Maze theBoard = new Maze(); Capsule pill = new Capsule(); public void paintComponent(Graphics g) { super.paintComponent(g); startAnimation(); theBoard.xbound = 0; theBoard.ybound = 0; for(theBoard.count1=0; theBoard.count1 < theBoard.ARRAY_BOUND; theBoard.count1++) { for(theBoard.count2=0; theBoard.count2 < theBoard.ARRAY_BOUND; theBoard.count2++) { switch (theBoard.board[theBoard.count1][theBoard.count2]) { case 1: { g.drawImage(theBoard.block.getImage(), theBoard.xbound, theBoard.ybound, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,this); } break; case 2: { g.setColor(theBoard.rgb); g.fillRect(theBoard.xbound,theBoard.ybound,theBoard.BLOCK_SIZE,theBoard.BLOCK_SIZE); g.setColor(Color.white); g.fillOval(theBoard.xbound+15,theBoard.ybound+15,5,5); } break; case 3: { g.drawImage(pill.images[pill.currentImage].getImage(), theBoard.xbound , theBoard.ybound, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,this); pill.currentImage = ( pill.currentImage +1 ) % pill.TOTAL_IMAGES; } break; case 4: { g.setColor(theBoard.rgb); g.fillRect(theBoard.xbound,theBoard.ybound,theBoard.BLOCK_SIZE,theBoard.BLOCK_SIZE); } break; } theBoard.xbound += theBoard.BLOCK_SIZE; } theBoard.ybound += theBoard.BLOCK_SIZE; theBoard.xbound = 0; }
if (pac.images[pac.currentImage].getImageLoadStatus() == MediaTracker.COMPLETE) { g.drawImage(pac.images[pac.currentImage].getImage(), xCount , yCount, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,pac); xANSWER = xCount / theBoard.BLOCK_SIZE; yANSWER = yCount / theBoard.BLOCK_SIZE; if ( (xANSWER == 18) && (yANSWER == 9) && (currentDirection == RIGHT) ) { xCount = 0; yCount = 315;} if ( (xANSWER == 0) && (yANSWER == 9) && (xCount == 0) && (currentDirection == LEFT) ) { xCount = 630; yCount = 315;} if ( ((forwardBy == 5) || (upBy == 5))) { if ( xANSWER != 18 ) if ((theBoard.board[yANSWER][xANSWER+1] == 1)) { forwardBy = 0; }
if (theBoard.board[yANSWER+1][xANSWER] == 1) { upBy = 0; } if ( (xANSWER == 9) && (yANSWER == 7) ) upBy = 0; }
if ( (forwardBy == -5) || (upBy == -5) ) { if ( xANSWER != 0 ) if ((theBoard.board[yANSWER][xANSWER-1] == 1)) { if ( (xCount) == ((xANSWER) * theBoard.BLOCK_SIZE) ) forwardBy = 0;} if (theBoard.board[yANSWER-1][xANSWER] == 1) { if ( (yCount) == ((yANSWER) * theBoard.BLOCK_SIZE) ) upBy = 0;} } } xCount += forwardBy; yCount += upBy; if (++pac.currentImage > pac.endImage) pac.currentImage = pac.startImage; if ( theBoard.board[yANSWER][xANSWER] == theBoard.f ) {theBoard.board[yANSWER][xANSWER] = theBoard.n; FOOD_COUNTER--;} if ( theBoard.board[yANSWER][xANSWER] == theBoard.c ) {theBoard.board[yANSWER][xANSWER] = theBoard.n; CAPSULE_COUNTER--;} if ( (FOOD_COUNTER == 0) && (CAPSULE_COUNTER == 0) ) { g.setColor (Color.red); g.setFont( new Font( "Serif", Font.BOLD, 20) ); g.drawString(" LEVEL COMPLETED ", 227, 407); } } public void startAnimation() { if (animationTimer == null) { pac.currentImage = 0; animationTimer = new Timer( ANIM_DELAY, this); animationTimer.start(); } else { if (!animationTimer.isRunning()) animationTimer.restart(); } } public void stopAnimation (){ animationTimer.stop(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getPreferredSize (){return new Dimension(665,665);} public void actionPerformed(ActionEvent e){ repaint(); } public static void main(String[] args ) { Game app = new Game(); JFrame main = new JFrame("PACMAN"); main.getContentPane().add(app, BorderLayout.CENTER); main.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); main.pack(); main.show(); } } |
This code is responsible for keeping track of the Food bits, and painting the results to the screen. --------------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Score extends JPanel { Game ga = new Game();
public void paintComponent (Graphics g) { g.setColor (Color.gray); g.fillRect(0,0,150,70); g.setColor (Color.white); g.drawLine( 5,0,145,0 ); g.setColor (Color.white); g.setFont( new Font( "Serif", Font.BOLD, 14) ); g.drawString("Score:",20,25); g.setColor (Color.yellow); g.setFont( new Font( "Serif", Font.BOLD, 20) ); g.drawString(ga.FOOD_COUNTER+"",55,50); }
public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getPreferredSize (){return new Dimension(150,70);} } |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|