当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Custom Repaint not working at all

问题描述:

I am trying to create an X/O game and in that i am not able to call drawString() when user clicks on the required position.

I am able to display grid and take mouse co-ordinated where user clicks but i am not able to repaint "X" and "O" on that.

Please help.Thanks

Here is code snippet:

 public class Tic_Tac_Toe extends JPanel

{

int xpos,ypos;

ArrayList xlist = new ArrayList();

ArrayList ylist = new ArrayList();

Point cursorPosition;

public static void main(String[] args)

{

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

int x=0,y=0;

boolean win,mark;

Board b=new Board();

Tic_Tac_Toe t=new Tic_Tac_Toe();

t.dispFrame();

while(!b.isGridFull())

{

b.printboard();

System.out.println("Enter the desired character position:");

try

{

x=Integer.parseInt(br.readLine());

y=Integer.parseInt(br.readLine());

}

catch(Exception e)

{

System.out.println("Exception is:");

}

mark=b.placeMark(x,y);

win=b.checkforwin();

if(b.flag==true)

{

System.out.println("Congratulations player with character "+b.CurrentPlayerMark+" has won");

break;

}

b.changePlayer();

}

if(b.flag==false)

System.out.println("The game has been drawn!!!");

}

public void dispFrame()

{

JFrame frame = new JFrame("My New Frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setPreferredSize(new Dimension(300, 300));

frame.setResizable(false);

JPanel panel=new JPanel()

{

@Override

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.drawLine(30,100,270,100);

g.drawLine(30,200,270,200);

g.drawLine(100,35,100,250);

g.drawLine(200,35,200,250);

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

{

Object a1=xlist.get(i);

Object b1=ylist.get(i);

int k=(Integer)a1;

int j=(Integer)b1;

g.drawString("X",k,j);

}

}

@Override

public void update(Graphics g)

{

paint(g);

if(Board.CurrentPlayerMark=='x')

g.drawString("X",xpos,ypos);

else if(Board.CurrentPlayerMark=='o')

g.drawString("O",xpos,ypos);

}

};

panel.addMouseListener(new MouseListener()

{

public char c;

@Override

public void mouseClicked(MouseEvent e)

{

c=Board.CurrentPlayerMark;

System.out.println(":MOUSE_CLICK_EVENT:");

cursorPosition = MouseInfo.getPointerInfo().getLocation();

xpos=cursorPosition.x;

ypos=cursorPosition.y;

xlist.add(xpos);

ylist.add(ypos);

Graphics g = getGraphics();

update(g);

repaint(); //Want to repaint here after user clicks on desired position in X/O grid

//Does update method is useful to call or not???

System.out.println("xlist: " + xlist +"ylist: " + ylist);

System.out.println("X: " + xpos + " Y: " + ypos);

System.out.println("CurrentPlayerMark is:"+c);

Board.changePlayer();

}

@Override

public void mousePressed(MouseEvent e) {

}

@Override

public void mouseReleased(MouseEvent e) {

}

@Override

public void mouseEntered(MouseEvent e) {

}

@Override

public void mouseExited(MouseEvent e) {

}

});

frame.add(panel);

frame.pack();

frame.setVisible(true);

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: