Currently, I am stuck on fixing my code in order so that the "tokens" I create will not overlap and so I can move my user token around. What I'm trying to do is trying to create 11 tokens. 10 that are red and 1 blue. The 10 that are red must not overlap.The one that is blue must be able to move. Right now, I am stuck on the overlapping problem and I can't seem to move the pattern within the rectangle.
Here's my code :
public class GameTokenPanel extends JPanel {
private CrossPattern patternt;
private Rectangle rect;
private GameToken token1;
private UserToken utoken;
private final int TOKEN_WIDTH=35;
private Random random = new Random();
private ArrayList <GameToken> tokenarr;
public GameTokenPanel()
{
tokenarr= new ArrayList<GameToken>();
for (int i =0; i<10;i++)
{
token1 = new GameToken(random.nextInt(400),random.nextInt(400),TOKEN_WIDTH,TOKEN_WIDTH);
tokenarr.add(token1);
}
utoken = new UserToken(0, 150,150,TOKEN_WIDTH,TOKEN_WIDTH);
// token1= new GameToken(random.nextInt(200),random.nextInt(200),TOKEN_WIDTH,TOKEN_WIDTH);
repaint();
class MyListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
int x = event.getX()-((int)utoken.getbbox().getWidth()/2);
int y = event.getY()-((int)utoken.getbbox().getWidth()/2);
utoken.getbbox().setLocation(x,y);
repaint();
}
public void mouseReleased(MouseEvent event){}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
}
MyListener listener=new MyListener();
this.addMouseListener(listener);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2= (Graphics2D) g;
g2.setColor(Color.BLUE);
utoken.draw(g2);
g2.setColor(Color.RED);
token1.draw(g2);// set token color red
for (int i =0; i<10;i++)
{
//if(!tokenarr.get(i).overlaps(tokenarr.get(i+1)))
tokenarr.get(i).draw(g2);
}
}
}
Here is my overlaps method for my Token Class. I would like to create a new token in the arraylist if two tokens randomly created overlaps.
public boolean overlaps(VisibleShape other)
{
GameToken other1 = (GameToken) other;
if(this.getbbox().intersects(other1.getbbox()))
{
return true;
}
else return false;
Here is my update method for my Pattern class which gets created within Game Token. Right now, I am stuck on what to do. I would like to move the pattern with the rectangle as I click around. So far, only the rectangle moves with the Listener.
// Updates the pattern objects and positions
public void update(Rectangle bbox)
{
switch(this.type)
{
}
}
Aucun commentaire:
Enregistrer un commentaire