| Wizard1988 |
Oct 22nd, 2007 11:24 PM |
Painting, repainting issues
I have two classes which represent squares. The first class represents tiles which can contain different walls or simply be empty. My second class (Robot) is very similar to the Square class but it has the ability to move.
My problem is that I can not get instances of the Robot class to repaint after they move.
:
import java.awt.*;
enum TYPE
{
ROBOT,BLANK,LEFT,TOP,RIGHT,BOTTOM,NW_L,NE_L,SE_L,SW_L
}
enum Direction
{
N,E,S,W
}
class Square extends Rectangle
{
protected TYPE type;
protected Color color;
protected String label;
private boolean occupied;
protected boolean visible;
private static Graphics2D g2;
public Square(int x, int y, TYPE t)
{
this(x,y,t,"",Color.BLUE);
}
public Square(int x, int y, TYPE t, String l, Color c)
{
this.x = x;
this.y = y;
this.width = GameManager.SQUARE_SIZE;
this.height = GameManager.SQUARE_SIZE;
this.type = t;
this.label = l;
this.color = c;
this.visible = true;
}
public void setOccupied(boolean status)
{
this.occupied = status;
}
public boolean isOccupied()
{
return occupied;
}
public void setLabel(String l)
{
this.label = l;
}
public String getLabel()
{
if(label == null)
{
return "";
}
return label;
}
public void setVisible(boolean visible)
{
this.visible = visible;
}
public boolean isVisible()
{
return visible;
}
public void drawLabel()
{
if(label != null)
{
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.setColor(Color.BLACK);
g2.drawString(label,(this.x+(this.width/2)- label.length()/2*6)-4,this.y+(this.width/2)+5);
}
}
public void redraw()
{
draw(g2);
}
public void draw(Graphics g)
{
if(!visible)
{
return;
}
if(g2 == null)
{
g2 = (Graphics2D)g;
}
g2.setColor(color);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.45f));
switch(type)
{
case ROBOT:
{
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fill(this);
break;
}
case BLANK:
{
g2.fill(this);
break;
}
case LEFT:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x,this.y,2,this.height);
break;
}
case TOP:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x,this.y,this.width,2);
break;
}
case RIGHT:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x+(this.width-2),this.y,2,this.height);
break;
}
case BOTTOM:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x,this.y+this.height-2,this.width,2);
break;
}
case NW_L:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x,this.y,this.width,2);
g2.fillRect(this.x,this.y,2,this.height);
break;
}
case NE_L:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x+(this.width-2),this.y,2,this.height);
g2.fillRect(this.x,this.y,this.width,2);
break;
}
case SE_L:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x+(this.width-2),this.y,2,this.height);
g2.fillRect(this.x,this.y+this.height-2,this.width,2);
break;
}
case SW_L:
{
g2.fill(this);
g2.setColor(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
g2.fillRect(this.x,this.y+this.height-2,this.width,2);
g2.fillRect(this.x,this.y,2,this.height);
break;
}
}
}
}
:
import java.awt.*;
import java.awt.Graphics2D;
class Robot extends Square
{
public Robot(int x, int y, Color color)
{
super((4 + (GameManager.SQUARE_GAP + GameManager.SQUARE_SIZE) * (x-1)),
(4 + (GameManager.SQUARE_GAP + GameManager.SQUARE_SIZE) * (y-1)),
TYPE.ROBOT,"",color);
setSize(GameManager.ROBOT_SIZE,GameManager.ROBOT_SIZE);
}
public void setColor(Color c)
{
this.color = c;
}
public Color getColor()
{
return color;
}
public void move(Direction d)
{
int delta = (this.height + GameManager.SQUARE_SIZE);
switch(d)
{
case N:
{
translate(0,-delta);
break;
}
case E:
{
translate(delta,0);
break;
}
case S:
{
translate(0,delta);
break;
}
case W:
{
translate(-delta,0);
break;
}
}
redraw();
}
}
:
import java.util.Vector;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Color;
class RobotManager
{
private Vector robots;
private Robot inFocus;
private Point lastClick;
public RobotManager()
{
robots = new Vector();
}
public void addRobot(int x, int y, Color c)
{
robots.add(new Robot(x,y,c));
inFocus = (Robot)robots.lastElement();
}
public void testRobots()
{
inFocus.move(Direction.S);
}
public void clicked(Point p)
{
for(int i = 0; i < robots.size(); i++)
{
if(((Robot)robots.elementAt(i)).contains(p))
{
inFocus = (Robot)robots.elementAt(i);
lastClick = p;
break;
}
}
}
public void dragged(Point p)
{
if((lastClick == null) || (inFocus.contains(p)))
{
return;
}
if(lastClick.getX() > p.getX())
{
inFocus.move(Direction.E);
}else{
inFocus.move(Direction.W);
}
if(lastClick.getY() > p.getY())
{
inFocus.move(Direction.S);
}else{
inFocus.move(Direction.N);
}
}
public void drawRobots(Graphics g)
{
for(int i = 0; i < robots.size(); i++)
{
((Robot)robots.elementAt(i)).draw(g);
}
}
}
:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class GamePanel extends JPanel
{
private GridManager gridManager;
private RobotManager robotManager;
public GamePanel()
{
setLayout(null);
setDoubleBuffered(true);
gridManager = new GridManager(16,16);
robotManager = new RobotManager();
setPreferredSize(gridManager.getSize());
robotManager.addRobot(1,1,Color.GREEN);
robotManager.addRobot(3,1,Color.RED);
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
robotManager.testRobots();
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
gridManager.drawBoard(g);
robotManager.drawRobots(g);
}
}
When I click the panel just goes gray....:sad: What can be done to fix this???
Any help, tips and or constructive criticism will be appreciated.
|