View Single Post
  #1 (permalink)  
Old 11-08-2007, 03:58 PM
DarkSide1 DarkSide1 is offline
Member
 
Join Date: Nov 2007
Posts: 2
DarkSide1 is on a distinguished road
Drawing outside paintComponent()
This is my first time posting on here, so any help at all is appreciated and I'm sorry if i just sound dumb.

I have a practical to do which involves creating a noughts and crosses board using Graphics2D, but I have found myself experiencing problems quite early, which I cant seem to fix.

I can get the board, etc drawn up correctly, but, I'm not sure how to go about drawing a new X or O every time someone clicks a square.

Currently, I have placed a circle in the top left square, just to try this out.
When I click in any other square, the circle moves, but what I need is for the original circle to stay in place, so I can get on to making rows etc.

Below is my basic code so far, but I'm planning on removing the individual squares etc, that seems a bit simple I know. (clicked is called from the main class)

I suppose the basic question is, how do I draw a new circle outside of the paintComponent() method?

Thanks for any help you guys can give, and sorry so seem so confusing!!

Code:

Code:
public class graphicsPanel extends JPanel { int circleX =15; int circleY =15; int size= 50; Rectangle2D.Double topLeft, topCentre, topRight, midLeft, midCentre, midRight, bottomLeft, bottomCentre, bottomRight; Ellipse2D.Double computer; Map<Rectangle2D.Double, String> squares = new HashMap<Rectangle2D.Double, String>(); public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(new Color(236,233,216));//sets colour //Sets up the 9 squares (to be changed) topLeft = new Rectangle2D.Double(0,0,77,77); topCentre = new Rectangle2D.Double(77,0,77,77); topRight = new Rectangle2D.Double(154,0,77,77); midLeft = new Rectangle2D.Double(0,77,77,77); midCentre = new Rectangle2D.Double(77,77,77,77); midRight = new Rectangle2D.Double(154,77,77,77); bottomLeft = new Rectangle2D.Double(0,154,77,77); bottomCentre = new Rectangle2D.Double(77,154,77,77); bottomRight = new Rectangle2D.Double(154,154,77,77); //puts squares into an array squares.put(topLeft, ""); squares.put(topCentre, ""); squares.put(topRight, ""); squares.put(midLeft,""); squares.put(midCentre, ""); squares.put(midRight, ""); squares.put(bottomLeft, ""); squares.put(bottomCentre, ""); squares.put(bottomRight, ""); //draws the squares for(Rectangle2D square: squares.keySet()){ g2.draw(square); } g2.setColor(Color.black);//changes colour back g2.setStroke(new BasicStroke(5));//sets the line width //Draws up the grid g2.drawLine(0,77,231,77); g2.drawLine(0,154,231,154); g2.drawLine(77,0,77,231); g2.drawLine(154,0,154,231); //Draws the EXAMPLE CIRCLE computer = new Ellipse2D.Double(circleX,circleY,size, size); g2.draw(computer); } //Point clicked method public void clicked(Point p) { for(Rectangle2D square: squares.keySet()){ if(square.contains(p)){ //changes the x and y coords circleX = (int)square.getX() +15; circleY = (int)square.getY() +15; repaint(); } } } /** Creates a new instance of graphicsPanel */ public graphicsPanel() { } }
Reply With Quote
Sponsored Links