Show Posts
|
|
Pages: [1]
|
|
5
|
Game Development / Newbie & Debugging Questions / Re: repaint with swing - paintComponent method never gets called...
|
on: 2007-08-20 18:47:52
|
|
The MediaTracker is not essential, all it does is ensure the image is loaded before it attempts to draw it and provides you with a way of prioritising the order in which images are loaded.
My problem is that the paintComponent method is not even being called. I have a line in the loadImage method which displays a line of output to my log file when an image is loaded, also in my loadImage method you can see the image is drawn and then dispose is called so this is not the problem.
My GameWindow class uses the same methodology, eg, same loadImage function with paintComponent method and this works fine.
Regardless of where I put my output lines, the fact is when I debug in JBuilder and step into every function called the paintComponent method is never called and as you can see from the output, the value of bDealer is true.
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Re: repaint with swing - paintComponent method never gets called...
|
on: 2007-08-20 02:25:03
|
ok i modified the moveCard2 and paintComponenet methods to the following so some more output would be displayed: 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
| public void moveCard2(double x, double y) { this.x = (int)x; this.y = (int)y; log.debug("about to call repaint from moveCards2"); log.debug("value of bDealing is " + bDealing); repaint(); log.debug("repaint called from moveCards2"); }
protected void paintComponent(Graphics g){ log.debug("inside DealCards paintComponent method"); log.debug("value of bDealing is " + bDealing); if (bDealing) { try { Graphics2D g2 = (Graphics2D) g; log.debug("draw a card"); g2.drawImage(nextcard1, startRect.x, startRect.y, this); if (lastCard > -1) { g2.drawImage(lastcard1, playerRect.x, playerRect.y, this); g2.drawImage(lastcard1, compRect.x, compRect.y, this); } if (currentCard > -1) g2.drawImage(currentcard1, x, y, this); } catch (Exception ex) { log.error("Exception " + ex.toString()); new ErrorWindow(ex.toString(), getClass().toString()); } } } |
so now in my log i just see the following: 2007-08-20 01:13:15,406 [GameWindow.java] DEBUG - about to call repaint from moveCards2 (Thread-27) 2007-08-20 01:13:15,406 [GameWindow.java] DEBUG - value of bDealing is true (Thread-27) 2007-08-20 01:13:15,406 [GameWindow.java] DEBUG - repaint called from moveCards2 (Thread-27) and nothing more. As for your point about a visivle container, well this could be where I have made the mistake. My assumption (probably wrong) is that GameWindow extends JPanel and therefore by DealCards extending GameWindow it would therefore be extending JPanel too since the paintComponent method works fine in the GameWindow class. 
|
|
|
|
|
7
|
Game Development / Newbie & Debugging Questions / repaint with swing - paintComponent method never gets called...
|
on: 2007-08-19 23:14:17
|
Hi all, New to this forum so this is my first post! I'm fairly new to Java game programming so I thought I would start myself off with a nice 2D game based around awt/swing components. I'm having problems calling the paintComponent method in one of my classes. I make a call to repaint() which makes a call to paint which then invokes paintComponent, if my understanding is correct. I therefore placed my paint code inside the overriden method paintComponent. However, when repaint is called, no call to paintComponent is made in the subclass, or parent class for that matter. I inserted break points into my code at the line where repaint is called and it the first line of my paintComponent method and then debugged the project in JBuilder which also confirmed the paintComponent method was not being called. Here is a copy of the class in question. As you can see it extends a class called GameWindow (which extends JPanel) which has its own paintComponent method. 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| class DealCards extends GameWindow{ private static final long serialVersionUID = 1L; private static Logger log = Logger.getLogger(GameWindow.class.getName()); private Rectangle startRect; private Rectangle compRect; private Rectangle playerRect; private int x; private int y; private final double SPEED = 19.0; private final long DELAY = 5; private final int MAX_CARDS = 9; private int lastCard; private int currentCard; private int nextCard; private int startx = 200; private int starty = 175; private int comprx = 180; private int compry = 175; private int playerrx = 20; private int playerry = 367; private int count = 0; private boolean bPlayerMove = true; private boolean bCompMove = true; private boolean bDealing = false; private boolean bShufflingIsDone = false; final private static String images_prefix = "C:/Documents and Settings/Paulo/jbproject/Canasta_Game/src/images/";
BufferedImage nextcard1 = loadImage2(images_prefix + "card0.gif"); BufferedImage currentcard1 = loadImage2(images_prefix + "card1.gif"); BufferedImage lastcard1 = loadImage2(images_prefix + "card2.gif");
public BufferedImage loadImage2(String name2){ try { log.debug("ImageIO string is " + ImageIO.read(new File(name2))); BufferedImage im = ImageIO.read(new File(name2)); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); int transparency = im.getColorModel().getTransparency(); BufferedImage copy = gc.createCompatibleImage(im.getWidth(), im.getHeight(), transparency ); Graphics2D g2d = copy.createGraphics(); g2d.drawImage(im, 0, 0, null); g2d.dispose(); return copy; } catch (IOException ex) { log.error("Load image error: " + ex); new ErrorWindow(ex.toString(), getClass().toString()); return null; } }
public void shuffleWait2() { log.debug("SHUFFLEWAIT2 CALLED"); Thread shuffleThread = new Thread(shuffler2); shuffleThread.setPriority(Thread.NORM_PRIORITY); shuffleThread.start(); }
private void initRects() { int cardw = 70; int cardh = 100; startRect = new Rectangle(startx, starty, cardw, cardh); compRect = new Rectangle(comprx, compry, cardw, cardh); playerRect = new Rectangle(playerrx, playerry, cardw, cardh); this.x = startRect.x; this.y = startRect.y; }
public void dealCard2() { start(); }
public void dealCards2() { log.debug("DEALCARDS2 CALLED"); if(!bDealing) { new SoundApplet2("Deal.wav"); Thread dealThread = new Thread(dealer); dealThread.setPriority(Thread.NORM_PRIORITY); dealThread.start(); } }
public void run() { while(bDealing) { try { Thread.sleep(DELAY); } catch (InterruptedException ex) { log.error("Dealing thread interrupted"); new ErrorWindow(ex.toString(), getClass().toString()); stop(); } boolean finished = advanceCard2(); if(finished) { stop(); } } }
public void start() { if(!bDealing) { next2(); count = 0; bDealing = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } }
private void stop() { bDealing = false; if(nextCard == MAX_CARDS) { bShufflingIsDone = true; } if(thread != null) thread.interrupt(); thread = null; }
private boolean advanceCard2() { Point p1 = startRect.getLocation(); Point p2 = playerRect.getLocation(); Point p3 = compRect.getLocation(); if(bPlayerMove) { double distance = p1.distance(p2); double d = ++count * SPEED; double x = p1.x + d; if (d > distance) x -= d - distance; moveCard2(x, p1.y); bCompMove = true; bPlayerMove = false; return d > distance; } if(bCompMove) { double distance = p1.distance(p3); double d = ++count * SPEED; double x = p1.x + d; if (d > distance) x -= d - distance; moveCard2(x, p1.y); bPlayerMove = true; bCompMove = false; return d > distance; } return true; }
public void moveCard2(double x, double y) { this.x = (int)x; this.y = (int)y; repaint(); }
public void next2() { if(nextCard+1 > 9) resetCards2(); lastCard = currentCard; currentCard = nextCard; nextCard++; if(startRect == null) initRects(); GameWindow.card_count--; x = startRect.x; y = startRect.y; log.debug("card_count is " + GameWindow.card_count); }
public void resetCards2() { lastCard = -2; currentCard = -1; nextCard = 0; repaint(); }
Runnable shuffler2 = new Runnable() { public void run() { log.debug("SHUFFLER2 RUNNER CALLED"); int count = 0; for(; count < 1;){ try { log.debug("Sleep for 500ms"); Thread.sleep(500); dealCards2(); count++; } catch (InterruptedException ex) { log.error("runner thread error " + ex); new ErrorWindow(ex.toString(), getClass().toString()); } } } };
Runnable dealer = new Runnable() { public void run() { int count = 0; final int WAIT_FOR_END_OF_DEAL = 50; while(count++ < MAX_CARDS) { dealCard2(); while(bDealing) { try { Thread.sleep(WAIT_FOR_END_OF_DEAL); } catch(InterruptedException ex) { log.error("runner thread error " + ex); new ErrorWindow(ex.toString(), getClass().toString()); stop(); } } } } };
protected void paintComponent(Graphics g){ if (bDealing) { try { Graphics2D g2 = (Graphics2D) g; log.debug("draw a card"); g2.drawImage(nextcard1, startRect.x, startRect.y, this); if (lastCard > -1) { g2.drawImage(lastcard1, playerRect.x, playerRect.y, this); g2.drawImage(lastcard1, compRect.x, compRect.y, this); } if (currentCard > -1) g2.drawImage(currentcard1, x, y, this); } catch (Exception ex) { log.error("Exception " + ex.toString()); new ErrorWindow(ex.toString(), getClass().toString()); } } } } |
I'm guessing this is probably a fairly straight forward question. I read the following article http://java.sun.com/products/jfc/tsc/articles/painting/index.html and I seem to be following everything that they say on here so I'm not sure what I'm doing wrong. The only way I can get it to work is if I put all this code into the GameWindow class but it's getting quite big and so is the paintComponent method in it so I would like to break the classes down into parts that all perform seperate actions of the game to make it easier to understand. Thanks.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|