My paint method won't work

I'm trying to draw a connect four board and pieces on the screen. The board shows up but none of the pieces do. I've used tons of print statements all over to verify that the Pieces are actually instantiated and they are, so I have no idea why they won't show up on the screen.

publicvoid paint(Graphics g)

{

g.drawImage(board.display(), 0, 0,null);

for(int i=0;i<board.getPieces().size();i++)

{

Piece p = (Piece)((board.getPieces()).get(i));

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

g.drawImage(p.display(),0, 0,null);//I don't care where they show up I'm just trying to verify that they do show up

}

if(!(board.gameOver().equals("nobody")))

{

g.drawString(message,0,0);

//Wait a certain amount of time

init();//Reset game

}

}

>

[1406 byte] By [JFactor2004a] at [2007-11-27 8:00:49]
# 1
well are you sure p.display() returns a valid Image object and not something which is filled with nothing but 0 pixels?
gimbal2a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 2
We probably need a bit more code then what you've provided.1) Where does this paint method reside?2) It looks to be that much of a logic used to paint the Piece object as an Image as in a display() method in the Piece object. The problem might be there as well.b.
bryanoa at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 3

> I'm trying to draw a connect four board and pieces on

> the screen. The board shows up but none of the

> pieces do. I've used tons of print statements all

> over to verify that the Pieces are actually

> instantiated and they are, so I have no idea why they

> won't show up on the screen.

>

> public void paint(Graphics g)

>{

>g.drawImage(board.display(), 0, 0, null);

>for(int i=0;i<board.getPieces().size();i++)

>{

>Piece p = (Piece)((board.getPieces()).get(i));

> //g.drawImage(p.display(), p.getXloc(),

> p.getYloc(),null);

> g.drawImage(p.display(),0, 0,null);//I

> don't care where they show up I'm just trying to

> verify that they do show up

>}

> if(!(board.gameOver().equals("nobody")))

>{

>g.drawString(message,0,0);

>//Wait a certain amount of time

>init(); //Reset game

>}

> }

One thing wrong I see is that you are trying to perform some program logic within the paint procedure, and this I wouldn't do. What I mean is the init() method. You have limited control over when the paint method is called. it could be called once, twice or 1000 times, whenever the OS wants to redraw your applet. So, all program logic needs to be out of this routine.

Second, I'll bet 100 shekels that you're trying to call this thing directly. In other words, you have somewhere in your code a call to paint(g), don't you? If I'm wrong, sue me, but it doesn't work that way.

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 4
> I'll bet 100 shekelsI think you would have lost your shekels:> The board shows up
tom_jansena at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 5

> > I'll bet 100 shekels

>

> I think you would have lost your shekels:

>

> > The board shows up

(pssst, I cheated)

I never make a bet I can't win. In the OP's previous post, he directly calls paint in this very program. Peek here:

http://forum.java.sun.com/thread.jspa?threadID=5184735&messageID=9719486#9719486

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 6

haha haha :)

@JFactor:

so let it be clear that paint is an abstract method; you need to implement it but you should not call it. It is called by the system when it's needed.

Why don't you put a breakpoint at>g.drawImage(p.display(),0, 0,null);//I don't care where they show up

and then do some stepping?

tom_jansena at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 7

> haha haha :)

>

> @JFactor:

> so let it be clear that paint is an abstract method;

I beg to differ. Java has an explicit definition of abstract and paint doesn't come near this. In fact paint is very concrete. I'd call it a callback method, one that you override so that java can call it if and when it wants to.

> you need to implement it but you should not call it.

> It is called by the system when it's needed.

Agree. Better to say "override it".

Also, if we are talking swing, then don't use paint but rather use paintComponent.

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 8

> Agree. Better to say "override it".

> Also, if we are talking swing, then don't use paint

> but rather use paintComponent.

If he's still working on the code from the other thread you linked, it's awt. He still should be calling super.paint(g) at the beginning of the method though.

hunter9000a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 9
> he's still working on the code from the other> thread you linked, it's awt. He still should be> calling super.paint(g) at the beginning of the method> though.Ahhh thanks. I missed that.
petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 10

I called paint via the update method because there are times when they state of the applet could change and I need to do it again. Also, here's my Piece class...its very simple...I'm not trying to do ridiculous logic in my paint method, I just need to iterate through a list so i know how many objects to paint....If I'm missing something please explain what I'm doing wrong. p.display() returns an Image, pic

public void update(Graphics g)

{

paint(g);

}

public void paint(Graphics g)

{

g.drawImage(board.display(), 0, 0, null);

for(int i=0;i<board.getPieces().size();i++)

{

Piece p = (Piece)((board.getPieces()).get(i));

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

g.drawImage(p.display(),0, 0,null);

}

if(!(board.gameOver().equals("nobody")))

{

g.drawString(message,0,0);

//Wait a certain amount of time

init(); //Reset game

}

}

}

import java.awt.*;

public class Piece

{

int myRow,myCol,myXloc,myYloc;

String myColor,fileName,otherFileName;

Image pic;

Toolkit t;

public Piece(String c, int x,int y)

{

myRow = x;

myCol = y;

myColor = c;

fileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\redpiece";

otherFileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\blackpiece";

t = Toolkit.getDefaultToolkit();

if(c.equals("red"))

pic = t.getImage(fileName);

else if(c.equals("blue"))

pic = t.getImage(otherFileName);

convert();

}

public void convert()

{

//if(myRow ==0)

myXloc = 25; //etc

myYloc = 25;

}

public int getXloc()//this has to figure out where to draw the piece on the screen

{

return myXloc;

}

public int getYloc()

{

return myYloc;

}

public int row()

{

return myRow;

}

public int col()

{

return myCol;

}

public String getColor()

{

return myColor;

}

public Image display()

{

return pic;

}

}

Message was edited by:

JFactor2004>

JFactor2004a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 11

> I called paint via the update method because there

> are times when they state of the applet could change

> and I need to do it again. Also, here's my Piece

> class...its very simple...I'm not trying to do

> ridiculous logic in my paint method, I just need to

> iterate through a list so i know how many objects to

> paint....If I'm missing something please explain what

> I'm doing wrong. p.display() returns an Image, pic

>

You need to read through the tutorial on painting and awt. You're doing many things wrong.

You never call paint directly. You call other things like revalidate or repaint and then let java call paint. You do not have init in your paint method.

Please read the tutorials. You cannot learn java by trial and error.

(also, don't you want to call super(g) on first line of the paint procedure?)

Message was edited by:

petes1234

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 12

I used paint(g) in the update method because repaint calls update, which usually clears the screen i thought. Since I didn't want to clear the screen, I just had it call paint again. I saw that in a tutorial. This time, I got rid of the overridden update method. I've never used update when I learned applets so idk why I decided to now. Super(g) I've NEVER used before. What else am I doing wrong? Where are these tutorials

JFactor2004a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 13

> I used paint(g) in the update method because repaint

> calls update, which usually clears the screen i

> thought. Since I didn't want to clear the screen, I

> just had it call paint again. I saw that in a

> tutorial. This time, I got rid of the overridden

> update method. I've never used update when I learned

> applets so idk why I decided to now. Super(g) I've

> NEVER used before. What else am I doing wrong?

> Where are these tutorials

Google the tutorials. Again, your core beliefs are wrong. And if you have a tutorial showing a direct call to the paint method (which I highly doubt), I'd like to see it.

addendum: first hit of my google search:

http://java.sun.com/products/jfc/tsc/articles/painting/

Message was edited by:

petes1234

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 14

From my link:

AWT Painting Guidelines

The AWT provides a simple callback API for painting components. When you use it, the following guidelines apply:

For most programs, all client paint code should be placed within the scope of the component's paint() method.

Programs may trigger a future call to paint() by invoking repaint(), but shouldn't call paint() directly.

On components with complex output, repaint() should be invoked with arguments which define only the rectangle that needs updating, rather than the no-arg version, which causes the entire component to be repainted.

Since a call to repaint() results first in a call to update(), which is forwarded to paint() by default, heavyweight components may override update() to do incremental drawing if desired (lightweights do not support incremental drawing)

Extensions of java.awt.Container which override paint() should always invoke super.paint() to ensure children are painted.

Components which render complex output should make smart use of the clip rectangle to narrow the drawing operations to those which intersects with the clip area.

petes1234a at 2007-7-12 19:42:52 > top of Java-index,Java Essentials,New To Java...
# 15
http://www.cs.clemson.edu/~cs428/resources/java/tutorial/JTGraphEx.htmlIn the second program, update is overwritten and calls paint. Also super is never called
JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 16

I agree there's no super called. it's a simple purple rectangle so I gather that it doesn't need to call super.

But I don't see "paint" called anywhere (note repaint is not the same thing,... not even close).

ah, I see, it is calling paint in the second iteration of the program....

let me see..;.

Message was edited by:

petes1234

petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 17
my apologies, you are right. you are not calling paint directly since you are putting it in update which is being called indirectly through repaint. 1000 pardons.
petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 18
haha its cool, still doesn't work though
JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 19
:(how big is the whole code? or at least the shortest fully compilable (self-contained) code that can reproduce your problem?If within reasonable limits, post it? (though it has to be a compilable entity).
petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 20

Not too long...I'll only post the important parts needed for compliling

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class GameDisplay extends Applet implements MouseListener

{

Board board;

String turn,message;

public void init()

{

board = new Board();

turn = "red";

message = "";

addMouseListener(this);

}

public void mouseClicked (MouseEvent me)

{

Location loc = new Location(me.getX(),me.getY());

if(loc.isValid(board) && board.isEmpty(loc))

{

board.add(new Piece(turn,loc.row(),loc.col()));

if(turn.equals("red"))

turn = "blue";

else turn = "red";

}

repaint();

}

public void mouseEntered (MouseEvent me) {}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}

public void paint(Graphics g)

{

g.drawImage(board.display(), 0, 0, null);

for(int i=0;i<board.getPieces().size();i++)

{

Piece p = (Piece)((board.getPieces()).get(i));

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

g.drawImage(p.display(),0, 0,null);

}

}

}

public class Location

{

int myX,myY,myRow,myCol;

public Location(int x,int y)

{

myX = x;

myY = y;

convert();

}

public boolean isValid(Board b)

{

return myRow>=0&&myCol>=0&&myRow<b.getWidth()&&myCol><b.getLength()&&(myRow==0||!b.isEmpty(myRow-1,myCol));

}

public void convert()

{

if(myY>=23&&myY<=94)

myRow = 5;

else if(myY>=120&&myY<=186)

myRow = 4;

else if(myY>=212&&myY<=289)

myRow = 3;

else if(myY>=308&&myY<=387)

myRow = 2;

else if(myY>=403&&myY<=484)

myRow = 1;

else if(myY>=499&&myY<=577)

myRow = 0;

if(myX>=687&&myX<=774)

myCol = 6;

else if(myX>=573&&myX<=662)

myCol = 5;

else if(myX>=465&&myX<=549)

myCol = 4;

else if(myX>=354&&myX<=436)

myCol = 3;

else if(myX>=237&&myX<=326)

myCol = 2;

else if(myX>=130&&myX<=212)

myCol = 1;

else if(myX>=18&&myX<=101)

myCol = 0;

}

public int row()

{

return myRow;

}

public int col()

{

return myCol;

}

}

import java.util.*;

import java.awt.*;

public class Board

{

Image pic;

Piece[][] board = new Piece[6][7]; //something

Toolkit t;

ArrayList pieces;

String winner,fileName;

boolean over;

int ct,myLength,myWidth;

public Board()

{

fileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\ConnectFourBoard.gif";

t = Toolkit.getDefaultToolkit();

pic = t.getImage(fileName);

pieces = new ArrayList();

winner = "nobody";

over = false;

ct=1;

myLength = 7;

myWidth = 6;

}

public boolean isEmpty(Location l)

{

return board[l.row()][l.col()]==null;

}

public boolean isEmpty(int r, int c)

{

return board[r][c]==null;

}

public void add(Piece p)

{

board[p.row()][p.col()]=p;

pieces.add(p);

}

public Image display()

{

return pic;

}

public ArrayList getPieces()

{

return pieces;

}

public Piece[][] getBoard()

{

return board;

}

public int getLength()

{

return myLength;

}

public int getWidth()

{

return myWidth;

}

}

import java.awt.*;

public class Piece

{

int myRow,myCol,myXloc,myYloc;

String myColor,fileName,otherFileName;

Image pic;

Toolkit t;

public Piece(String c, int x,int y)

{

myRow = x;

myCol = y;

myColor = c;

fileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\redpiece";

otherFileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\blackpiece";

t = Toolkit.getDefaultToolkit();

if(c.equals("red"))

pic = t.getImage(fileName);

else if(c.equals("blue"))

pic = t.getImage(otherFileName);

}

public int getXloc()//this has to figure out where to draw the piece on the screen

{

return myXloc;

}

public int getYloc()

{

return myYloc;

}

public int row()

{

return myRow;

}

public int col()

{

return myCol;

}

public String getColor()

{

return myColor;

}

public Image display()

{

return pic;

}

}

JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 21

There is an error in the forum software that miscopies greater and less than signs especially when they are crowded in code without spaces on either side. It has affected your code here:

public boolean isValid(Board b)

{

return myRow>=0&&myCol>=0&&myRow<b.getWidth()&&myCol><b.getLength()&&(myRow==0||!b.isEmpty(myRow-1,myCol));

}

What should be there in place of the "><" symbols?

petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 22
just a <
JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 23

next, are your image file paths valid and are the image files themselves valid (I don't see a jpg, bmp, or png extension)?

The reason I ask is that if I have a valid image file address loaded into the piece fileName field, and I click in the upper left of the applet, I do see the image in the applet.

Message was edited by:

petes1234

petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 24
o wow...i dont have an extension on my piece images, how stupid of me...Ok so now I have pieces appearing but not on the first click...I have it tell me when a piece is created so I know there should be a piece but it doesn't show up until I click again.
JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 25

> o wow...i dont have an extension on my piece images,

> how stupid of me...Ok so now I have pieces appearing

> but not on the first click...I have it tell me when a

> piece is created so I know there should be a piece

> but it doesn't show up until I click again.

try clicking way up in the left upper corner. I get an image on first click, but only there.

Can you post your whole prog, but have comments on the methods explaining what they are supposed to do? (like the cryptic convert method in "Location").

petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 26

Surely

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class GameDisplay extends Applet implements MouseListener

{

Board board;

String turn,message;

public void init()

{

board = new Board();

turn = "red";

message = "";

addMouseListener(this);

}

public void mouseClicked (MouseEvent me)

{

Location loc = new Location(me.getX(),me.getY());

if(loc.isValid(board) && board.isEmpty(loc))

{

board.add(new Piece(turn,loc.row(),loc.col()));

repaint();

if(turn.equals("red"))

turn = "blue";

else turn = "red";

}

if(!(board.gameOver().equals("nobody")))

{

message = "Game Over! The winner is " + board.gameOver();

repaint();

}

}

public void mouseEntered (MouseEvent me) {}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}

public void update(Graphics g)

{

paint(g);

}

public void paint(Graphics g)

{

g.drawImage(board.display(), 0, 0, null);

System.out.println("There are "+board.getPieces().size()+ " pieces.");

for(int i=0;i<board.getPieces().size();i++)//prints all pieces on board in their proper locations

{

Piece p = (Piece)((board.getPieces()).get(i));

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

g.drawImage(p.display(),0, 0,null);

}

if(!(board.gameOver().equals("nobody")))

{

g.drawString(message,0,0);

//Wait a certain amount of time

//init(); //Reset game

}

}

}

public class Location

{

int myX,myY,myRow,myCol;

public Location(int x,int y)

{

myX = x;

myY = y;

convert();

}

public boolean isValid(Board b)// Checks to see whether this is a valid location to put a piece

{

return myRow>=0&&myCol>=0&&myRow<b.getWidth()&&myCol><b.getLength()&&(myRow==0||!b.isEmpty(myRow-1,myCol));

}

public void convert()//Converts a region on the screen to a cell in a matrix

{

if(myY>=23&&myY<=94)

myRow = 5;

else if(myY>=120&&myY<=186)

myRow = 4;

else if(myY>=212&&myY<=289)

myRow = 3;

else if(myY>=308&&myY<=387)

myRow = 2;

else if(myY>=403&&myY<=484)

myRow = 1;

else if(myY>=499&&myY<=577)

myRow = 0;

if(myX>=687&&myX<=774)

myCol = 6;

else if(myX>=573&&myX<=662)

myCol = 5;

else if(myX>=465&&myX<=549)

myCol = 4;

else if(myX>=354&&myX<=436)

myCol = 3;

else if(myX>=237&&myX<=326)

myCol = 2;

else if(myX>=130&&myX<=212)

myCol = 1;

else if(myX>=18&&myX<=101)

myCol = 0;

}

public int row()

{

return myRow;

}

public int col()

{

return myCol;

}

}

import java.util.*;

import java.awt.*;

public class Board

{

Image pic;

Piece[][] board = new Piece[6][7]; //something

Toolkit t;

ArrayList pieces;

String winner,fileName;

boolean over;

int ct,myLength,myWidth;

public Board()

{

fileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\ConnectFourBoard.gif";

t = Toolkit.getDefaultToolkit();

pic = t.getImage(fileName);

pieces = new ArrayList();

winner = "nobody";

over = false;

ct=1;

myLength = 7;

myWidth = 6;

}

public boolean isEmpty(Location l)

{

return board[l.row()][l.col()]==null;

}

public boolean isEmpty(int r, int c)

{

return board[r][c]==null;

}

public void add(Piece p)

{

board[p.row()][p.col()]=p;

pieces.add(p);

}

public Image display()

{

return pic;

}

public ArrayList getPieces()

{

return pieces;

}

public Piece[][] getBoard()

{

return board;

}

public int getLength()

{

return myLength;

}

public int getWidth()

{

return myWidth;

}

public String gameOver()//find out if game is over...i may have to change plus and minus

{

for (int r=0;r<pieces.size();r++)//did red win

{

Piece p = (Piece)(pieces.get(r));

int row = p.row();

int col = p.col();

while(row-1>=0 && !isEmpty(row-1,col) && board[row-1][col].getColor().equals(board[row][col].getColor()))

{

ct++;

row--;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(row+1<board.length && !isEmpty(row+1,col) && board[row+1][col].getColor().equals(board[row][col].getColor()))

{

ct++;

row++;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(col-1>=0 && !isEmpty(row,col-1) && board[row][col-1].getColor().equals(board[row][col].getColor()))

{

ct++;

col--;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(col+1<board.length && !isEmpty(row,col+1) && board[row][col+1].getColor().equals(board[row][col].getColor()))

{

ct++;

col++;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(row+1><board.length&&col+1><board.length && !isEmpty(row+1,col+1) && board[row+1][col+1].getColor().equals(board[row][col].getColor()))

{

ct++;

col++;

row++;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(row+1><board.length&&col-1>=0 && !isEmpty(row+1,col-1) && board[row+1][col-1].getColor().equals(board[row][col].getColor()))

{

ct++;

col--;

row++;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(row-1>=0&&col-1>=0 && !isEmpty(row-1,col-1) && board[row-1][col-1].getColor().equals(board[row][col].getColor()))

{

ct++;

col--;

row--;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

while(row-1>=0&&col+1<board.length && !isEmpty(row-1,col+1) && board[row-1][col+1].getColor().equals(board[row][col].getColor()))

{

ct++;

col++;

row--;

if(ct==4 && board[row][col].getColor().equals("red"))

winner="red";

else if(ct==4 && board[row][col].getColor().equals("blue"))

winner="blue";

}

ct=1;

}

return winner;

}

}

import java.awt.*;

public class Piece

{

int myRow,myCol,myXloc,myYloc;

String myColor,fileName,otherFileName;

Image pic;

Toolkit t;

public Piece(String c, int x,int y)

{

myRow = x;

myCol = y;

myColor = c;

fileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\redpiece.gif";

otherFileName = "C:\\Users\\JP\\Projects\\Connect Four\\src\\blackpiece.gif";

t = Toolkit.getDefaultToolkit();

if(c.equals("red"))

pic = t.getImage(fileName);

else if(c.equals("blue"))

pic = t.getImage(otherFileName);

convert();

}

public void convert()//This (when it's done) will figure out where on the screen to draw a piece based on its row, col...right now I just want to draw them anywhere I can see them

{

//if(myRow ==0)

myXloc = 25; //etc

myYloc = 25;

}

public int getXloc()

{

return myXloc;

}

public int getYloc()

{

return myYloc;

}

public int row()

{

return myRow;

}

public int col()

{

return myCol;

}

public String getColor()

{

return myColor;

}

public Image display()

{

return pic;

}

}

>

JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 27
you shouldn't be able to add a piece by clicking in the upper left corner. Everytime for me its when you add the first piece it doesn't show up unless you add another one
JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 28

Things not printing until the 2nd time to me sounds like an OBO error but because of my paint code which prints the number of pieces before trying draw the actual piece, I can't be off by one. If I print that I have 1 piece, I have to have 1 piece, and so it should then print that piece...So I think it has something to do with me not understanding paint. Should I be calling repaint in paint?

public void paint(Graphics g)

{

g.drawImage(board.display(), 0, 0, null);

System.out.println("There are "+board.getPieces().size()+ " pieces.");

for(int i=0;i<board.getPieces().size();i++)//prints all pieces on board in their proper locations

{

Piece p = (Piece)((board.getPieces()).get(i));

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

g.drawImage(p.display(),0, 0,null);

}

if(!(board.gameOver().equals("nobody")))

{

g.drawString(message,0,0);

//Wait a certain amount of time

//init(); //Reset game

}

}

>

JFactor2004a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 29

> Things not printing until the 2nd time to me sounds

> like an OBO error but because of my paint code which

> prints the number of pieces before trying draw the

> actual piece, I can't be off by one. If I print that

> I have 1 piece, I have to have 1 piece, and so it

> should then print that piece...So I think it has

> something to do with me not understanding paint.

> Should I be calling repaint in paint?

I believe that calling repaint in paint is inviting a lot of badness. you have no control over when paint is being called and are possibly setting yourself up for infinite loops. In a word, don't do it.

petes1234a at 2007-7-21 22:26:24 > top of Java-index,Java Essentials,New To Java...
# 30
fair enough haha...my other thought was having it try and draw each image a couple of times but then I think things will just be laggy...it looks like it should print though on the first try right?
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 31

A couple things to consider is to

1) separate your program logic from your GUI to a greater degree than you have done. This game should be able to be implemented with a console window if necessary. This will give you much greater flexibility with what GUI's you can try, and would make your program much easier to debug.

2) Consider using labels in a grid that change color or a grid of buttons rather than painting. It's a whole lot easier to implement.

3) If you want to use painting on components, consider using Swing rather than AWT.

petes1234a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 32
The only real logic I have in my GUI is a for loop that tells how many pieces to paint on the screen. I wanted to start with an applet b/c thats what I've learned, and its easy. I'd love to move onto other things but I want to figure out why this doesn't work first
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 33

> The only real logic I have in my GUI is a for loop

> that tells how many pieces to paint on the screen.

I'm not so sure about this. You need a logical (non-visual) grid or 2-D array, you need to know what spot has been filled, you need to know which columns are completely filled, you need to know whose turn it is (which I would not follow with "red" and "yellow" Strings), you need to check if there is a win after each move, and this has to be checked 4 ways (up-down, side-to-side, left-lower to right upper diag, and the other diagonal), you need to know if a maximum number of turns have been played (if your game has this),.....

> I wanted to start with an applet b/c thats what I've

> learned, and its easy. I'd love to move onto other

> things but I want to figure out why this doesn't work

> first

ok, your time.

petes1234a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 34
most of that logic done isn't done in paint, its all done in other classes...all the paint method does is put stuff on the screen...i don't see whats complicated about that.
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 35

I'm not talking about just the paint method. I'm talking about separation of logic, about having a grid that's smart enough to know if a piece can go someplace, to know if a player has won, THEN implementing a GUI that connects with this logic.

Another point. You don't call super.paint(g) I think because you don't want your display to be over-written. That's not an issue if you let paint know how to draw your display everytime.

Also, I don't know much about Applet, but with JApplet (I think -- since I don't know much about this), it is rare to draw right on the JApplet. I've overridden the paintComponent of a jpanel then added that jpanel to the japplet's contentPane (anyone who knows anything, PLEASE correct any mistakes):

import javax.swing.JApplet;

public class MyApplet extends JApplet

{

MyPane pane = new MyPane();

public void init()

{

getContentPane().add(pane);

}

}

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.List;

import javax.swing.*;

class MyPane extends JPanel implements MouseListener

{

private int myX, myY;

private final String redDot = ".\\petes\\brief3\\redDot.jpg";

private String yellowDot = ".\\petes\\brief3\\yellowDot.jpg";

private Image myImage;

private Toolkit tk;

private List<Point> points;

public MyPane()

{

super();

tk = Toolkit.getDefaultToolkit();

myImage = tk.getImage(redDot);

addMouseListener(this);

points = new ArrayList<Point>();

}

public void mouseClicked(MouseEvent me)

{

myX = me.getX();

myY = me.getY();

points.add(new Point(myX, myY));

revalidate();

repaint();

}

public void mouseEntered(MouseEvent arg0){}

public void mouseExited(MouseEvent arg0){}

public void mousePressed(MouseEvent arg0){}

public void mouseReleased(MouseEvent arg0){}

@Override

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if (points != null && points.size() > 0)

{

for (int i = 0; i < points.size(); i++)

{

g.drawImage(myImage, (int)points.get(i).getX(), (int)points.get(i).getY(), null);

}

}

}

}

petes1234a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 36

To the OP: I've been trying to isolate the display part by using a grid of labels. I have a functional display (without the logic) for a JFrame app but it fails when I try to do it as a JApplet. See this thread here if you want to have a peek.

http://forum.java.sun.com/thread.jspa?threadID=5186350&tstart=0

petes1234a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 37

Another way to put this together is to have Board extend Canvas or Panel, load the images

in it with Toolkit or ImageIO (j2se 1.4+), draw the images on it (grid and pieces), and

add it to the center section of your Applet in a BorderLayout. You may need a MediaTracker

to load the image data. All Piece coordinates for images turn out to be pretty much at the

origin of the Applet (0,0). Image paths outside the current directory will generate

SecurityAccessViolations unless you take special measures, eg, signing the applet. Images

loaded okay in current directory. It is more efficient to load all images once at

construction/startup rather than each time a new Piece is instantiated.

// <applet code="GameDisplay" width="600" height="600"></applet>

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class GameDisplay extends Applet implements MouseListener

{

Board board;

Image[] images;

int turn;

String message;

public void init()

{

images = loadImages();

board = new Board(images[0]);

turn = 1;

message = "";

addMouseListener(this);

}

public void mouseClicked (MouseEvent me)

{

Location loc = new Location(me.getX(),me.getY());

System.out.println("loc = [" + loc.row() + ", " + loc.col() + "]");

System.out.println("test = " + (loc.isValid(board) && board.isEmpty(loc)));

if(loc.isValid(board) && board.isEmpty(loc))

{

board.add(new Piece(images[turn], loc.row(), loc.col()));

if(turn == 1)

turn = 2;

else turn = 1;

}

repaint();

}

public void mouseEntered (MouseEvent me) {}

public void mousePressed (MouseEvent me) {}

public void mouseReleased (MouseEvent me) {}

public void mouseExited (MouseEvent me) {}

public void paint(Graphics g)

{

g.drawImage(board.display(), 0, 0, null);

for(int i=0;i<board.getPieces().size();i++)

{

Piece p = (Piece)((board.getPieces()).get(i));

System.out.println("Pieces["+i+"] row = " + p.row() +

" col = " + p.col());

//g.drawImage(p.display(), p.getXloc(), p.getYloc(),null);

// Looks like you need x, y coords instead of row, col data.

g.drawImage(p.display(), p.row(), p.col(), this);

}

}

private Image[] loadImages()

{

String[] paths =

{

"ConnectFourBoard.gif", "blackpiece.gif", "redpiece.gif"

// These test images loaded okay from current directory.

//"cougar.jpg", "geek--.gif", "geek-t.gif"

};

Image[] images = new Image[paths.length];

Toolkit t = Toolkit.getDefaultToolkit();

// Use a MediaTracker (see api) to load the image data.

// It requires an ImageObserver.

MediaTracker mt = new MediaTracker(this);

for(int j = 0; j >< images.length; j++)

{

images[j] = t.createImage(paths[j]);

mt.addImage(images[j], 0);

}

try

{

mt.waitForAll();

}

catch(InterruptedException e)

{

System.out.println("mt interrupted");

}

// You can query the MediaTracker for load errors here.

return images;

}

}

import java.util.*;

import java.awt.*;

public class Board

{

Image pic;

Piece[][] board = new Piece[6][7]; //something

ArrayList pieces;

String winner,fileName;

boolean over;

int ct,myLength,myWidth;

public Board(Image image)

{

pic = image;

pieces = new ArrayList();

winner = "nobody";

over = false;

ct=1;

myLength = 7;

myWidth = 6;

}

public boolean isEmpty(Location l)

{

return board[l.row()][l.col()]==null;

}

public boolean isEmpty(int r, int c)

{

return board[r][c]==null;

}

public void add(Piece p)

{

board[p.row()][p.col()]=p;

pieces.add(p);

}

public Image display() { return pic; }

public ArrayList getPieces() { return pieces; }

public Piece[][] getBoard() { return board;}

public int getLength() { return myLength; }

public int getWidth() { return myWidth; }

}

import java.awt.*;

public class Piece

{

int myRow,myCol,myXloc,myYloc;

String myColor,fileName,otherFileName;

Image pic;

public Piece(Image image, int x, int y)

{

myRow = x; // rows are usually y

myCol = y;

//myColor = c;

pic = image;

}

//this has to figure out where to draw the piece on the screen

public int getXloc() { return myXloc; }

public int getYloc() { return myYloc; }

public int row() { return myRow; }

public int col() { return myCol; }

public String getColor() { return myColor; }

public Image display() { return pic; }

}

crwooda at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 38
Thanks for your guys tips, I'll give everything a shot. I'm still a little confused as to why what I did doesn't work. Don't get me wrong, I appreciate all the opinions, but I'd like for someone to please explain why the way I did it doesn't work.
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 39
I even added super.paint(g) to the first line of paint and that didn't fix anything
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 40

I'd like for someone to please explain why the way I did it doesn't work.

I don't have access to your images so I'm unable to give the full lowdown on why what you

tried didn't work. One problem is that all your column and row assignments are hard-coded in

the Location class. I could work through them and re-create the grid and then would likely

draw it in the paint method to test the rest of your code. But I'm too lazy. From the work I

did to post reply 37 I will say I think you may have some problems with image loading. The

comments, code reorganization and output statements in reply 37 expose the rest of the

problems.

Suggestions: When things get out of hand and/or you lose track of what you're doing start

over. Go slowly. Don't build anymore into your app until you get everything working the way

you want. Writing a lot of code and then expecting things to work takes a lot of practice

and trial–and–error experimenting. Images can be extremely frustrating to work with until

you get the hang of it. So nail that down solid right up front.

Otherwise, if you'll fix the row/column vs x/y issue for placement of Pieces in the modified

(which was mostly about getting your images to show up) code of reply 37 you might get it

working.

crwooda at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 41

Ok, well I'm not sure why I'd have trouble loading the pieces and not the whole board, since the boards a bigger file. I'll try just a simple app that draws one piece on a screen, and then maybe I'll try drawing a couple, and then an ArrayList of them. Otherwise I'll try the buffering thing you mentioned. Thanks

**I looked over the code in reply 37 in more detail and it seems to make sense, but I'm not sure what you mean by I need an ImageObserver...you don't refer to it in the code anyhwere, I'm not sure what it is or why I'd need it

Message was edited by:

JFactor2004

JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 42

If you use the antique Toolkit methods to load images you will need a MediaTracker to load

in the image data. A MediaTracker requires a Component (I was wrong) which implements

ImageObserver as its only argument. The "this" is okay above because Applet extends

Component. We couldn't do this in the Board or Piece classes.

// It requires a Component - I was wrong above.

MediaTracker mt = new MediaTracker(this);

crwooda at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 43

But while you're working on the graphics part, why not re-write the program logic part, and use a bit more OOP.

Some recommendations including:

1) create an enum class, ConnectFourColor for your colors: empty, yellow and red.

2) create a non-graphical ConnectFourCell class with fields for row, column, and ConnectFourColor with getters setters and constructor.

3) create a non-graphical ConnectFourGrid which is a 2-dimensional grid of ConnectFourCell's. This could have methods to add a disk, and methods to check for a win (by checking the colors of contiguous cells in the row, column, and diagonals of a newly added cell).

Then after creating the non-graphical classes, testing them to make sure that they work, connect them to the GUI.

petes1234a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 44
What's OOP I'm not familiar...The board class is kind of like that, it uses a matrix as the board and the GUI is connected to that. I'm still a little confused on the MediaTracker, I know that I need it to help load the images but what's the imageobserver, where do I put it?
JFactor2004a at 2007-7-21 22:26:29 > top of Java-index,Java Essentials,New To Java...
# 45
http://en.wikipedia.org/wiki/Object-oriented_programming
CaptainMorgan08a at 2007-7-21 22:26:34 > top of Java-index,Java Essentials,New To Java...
# 46
Right duh! I implemented the Mediatracker and it works great! Thanks! All the images appear at the right times and everything.
JFactor2004a at 2007-7-21 22:26:34 > top of Java-index,Java Essentials,New To Java...