Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with translacte!!  RSS feed

 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I hope someone out there can give me a hand with this... I�m writing a game where I want to move a piece to a different position on the screen at the user�s input. My Piece object, which extends JPanel, has the following method:
public void movePiece(Graphics g)
{
erasePiece(g);
g.translate(0, 20);
g.setColor(color);
g.fillPolygon(poly);
}
This works Ok the first time I use it; the piece "disappears" and is redrawn according to g.transform, and it look as if it has moved, which is what I want. The problem is that it doesn't work on subsequent calls. Instead of translate() moving the piece from the new position, it appears to use the origal origin.
Please help me!! What am I doing wrong? Here's the main stuff from the Piece class..
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Piece extends JPanel
{
private Polygon poly;
private Color color;
private Vector colors, yellow, green, blue, purple, red, pieces;
public Piece()//Polygon p, Color c)
{
colors = new Vector();
yellow = new Vector();
green = new Vector();
blue = new Vector();
purple = new Vector();
red = new Vector();
pieces = new Vector();
setPieces();
setColors();
poly = choosePiece();
color = chooseColor();
}
public void paintComponent(Graphics g)
{
g.setColor(color);
g.fillPolygon(poly);
}
public Polygon getPolygon()
{
return poly;
}
public Color getColor()
{
return color;
}
public void movePiece(Graphics g)
{
erasePiece(g);
g.translate(0, 20);
g.setColor(color);
g.fillPolygon(poly);
}
public void erasePiece(Graphics g)
{
g.setColor(Color.black);
g.fillPolygon(poly);
}
public void setPieces()
{
//puts polygons in the pieces vector
}
public void setColors()
{
//puts colors in the colors vector
}
public Polygon choosePiece()
{
//chooses a piece at random from the vector of pieces
}
public Color chooseColor()
{
//chooses a color at random from the vector of colors
}


}
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I made some tests with your code and at least I got, what you need. First, why your Piece extends from JPanel? I think, it would be better to extend from JComponent. Second, in my opinion, it would be better to provide a piece container, which contains and paints all pieces. Then you do not have to call erasePiece, because it is all done with a repaint.

And here is my PieceTester (my Container)

Do you got the Idea? Now it is easy for you to add the container to a JFrame or whatever you want.
Hope this helps
Rene
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rene,
Thanks for the posting, but it doesn�t seem to work with what I�m trying to do. The container I�m adding the piece to is the "board" for the game, which contains other graphics, so I don�t want to have to repaint() every time I move a piece. I have a slightly different version which I've decided to do where I create a piece in the in my Game class:
piece Piece = new Piece(chooseColor(), choosePolygon);
..then I add the piece to the Board and then call piece.move() when the user pushes a particular key. I also have an infinite loop in Game's run() method that calls piece.move() every second. As I said before, I'm trying to move it with translate{}. The really annoying thing is, I had another version of this program which I started ages ago but never completed. At first, I had the same problem, but I can't remember how I got round it!! Maybe I'm using the wrong approach..
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. I reckon it might work if I can manipulate piece in it�s own separate Graphics conext. Is this a good idea, and does anyone know how I could do this? I tried a this:
piece.move(piece.getGraphics)
in the board�s paintComponet(), but it didn�t work (I got a null pointer exception).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!