自定义重绘不起作用

时间:2015-06-23 16:12:28

标签: java swing

我正在尝试创建一个X / O游戏,因为当用户点击所需位置时,我无法调用drawString()。 我能够显示网格并在用户点击的地方采取鼠标协调,但我无法重新绘制" X"和" O"在那。 请帮忙。谢谢

以下是代码段:

    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);       
     }
}

0 个答案:

没有答案