开发者

I am trying to create a tictactoe program but am having problems with the JOptionPane and drawing

I am trying to create a TicTacToe java program for school and it doesn't draw at school but draws at home? I also have an issue with the JOptionPanes were the 1st box comes up twice on the first run. I am getting null and security error and think this has something to do with it

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import cs1.Keyboard;

   public class TicTac2 extends Applet
   {
      int white;
      int blue;
      int x;
      String xstr, ystr;
        int s = 0;  // 1,1 o
        int t = 0;  //1,2 o
        int f = 0;  //1,3 o
        int u = 0;  //2,1 o
        int h =0;   //2,2 o
        int j = 0;  //2,3 o
        int k = 0;  //3,1 o
        int l = 0;  //3,2 o
        int m = 0;  //3,3 o
      int y;
      int a = 10;
        int won = 0;
        int who = 0; // 5= x, 10 =o

     // Who goes first in the next game?
      boolean first = true;

      Image notImage;   //white
      Image crossImage; //blue


       public void init() {
         notImage = getImage(getCodeBase(), "images/not.gif");
         crossImage = getImage(getCodeBase(), "images/cross.gif");

      }
    /**
     * Paint it.
     */
       public void paint(Graphics g) 
      {
         Dimension d = getSize();
         g.setColor(Color.black);
         int xoff = d.width / 3;
         int yoff = d.height / 3;
         g.drawLine(xoff, 0, xoff, d.height);
         g.drawLine(2*xoff, 0, 2*xoff, d.height);
         g.drawLine(0, yoff, d.width, yoff);
         g.drawLine(0, 2*yoff, d.width, 2*yoff);

           int i = 0;
         for (int r = 0 ; r < 3 ; r++) 
         {
            for (int c = 0 ; c < 3 ; c++, i++) 
            {
               if ((white & (1 << i)) != 0) 
               {
                  g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this);
               } 
               else if ((blue & (1 << i)) != 0) 
               {
                  g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this);
               }
            }

            }

            /*g.setColor(Color.black);
            g.drawLine(130,0, 130, 700);
            g.drawLine(270,0,270,700);
            g.drawLine(0,125,900,125);
            g.drawLine(0,250,900,250);
            String result;*/

            int z=0;
            while (won == 0)
            {

                //for(i=0; i< 10; i++)
                //{
                x=0;
                y=0;
                while (x >3 || x<1)
                {    
                    xstr = JOptionPane.showInputDialog("Enter an int for x(1-3): ");
               x = Integer.parseInt(xstr);

                }
                z=1;
                System.out.print(x);

                while (y>3 || y<1)
                {
                 ystr = JOptionPane.showInputDialog("Enter an int for y(1-3): "); 
                  y = Integer.parseInt(ystr);
                }   
                System.out.println(y);


                int p = 0 ; 

                if (x==2) 
                {
                   p = 140;
                }

                if (x==3)
                {
                    p = 275;
                }
                System.out.println("test");
                if (x==1 || x==2 || x ==3)
                {
                    g.setColor(Color.blue);
                    if (y==1)
                    {                    
                        g.drawLine(20+p,20,100+p,100);
                        g.drawLine(100+p,20,20+p,100);

                        if (p == 0)
                         {
                          s = 1;
                         }
                        if (p ==140)
                        {
                            t = 1;
                        }
                        if (p==275)
                        {
                            f=1;
                        }

                    }
                    if (y==2)
                    {                     
                        g.drawLine(20+p,150,100+p,220);
                        g.drawLine(100+p,150,20+p,220);

                        if (p == 0)
                         {
                          u = 1;
                         }
                        if (p ==140)
                        {
                            h = 1;
                        }
                        if (p==275)
                        {
                            j=1;
                        }

                    }

                    if (y==3) 
                    {
                        g.drawLine(20+p,280,100+p,340);
                        g.drawLine(100+p,280,20+p,340);

                        if (p == 0)
                        {
                            k = 1;
                        }
                        if (p ==140)
                        {
                             l = 1;
                        }
                        if (p==275)
                        {
                             m=1;
                        }

                    }
                    if (s==1 && h==1 && m==1 || s==1 && t==1 && f==1 || s==1 && u==1 && k==1 || k==1 && h==1 && f==1 || u==1 && h==1 && j==1 || k==1 && l==1 && m==1 || t==1 && h==1 && l==1 || f==1 && j==1 && m==1)
                    {
                        who = 5; //x
                        won =1;
                    }
                    if (won == 1 && who == 5) // if x won
                    {
                    JOptionPane.getRootFrame().dispose();
                        String xwon = "You won!!!!! NICE JOB!!";
                        JOptionPane.showMessageDialog (null, xwon);
                        //outputToApplet.flush();
                        //outputToApplet.close();
                        //destroy();
                        System.exit(0);
                    }



                    if (h==0) //(2,2) center!
                    {
                        g.drawOval (155,155,80,70);
                        h=2;
                    }
                    else if (s==0) //(1,1)
                    {
                        g.drawOval (23,20,70,70);   
                        s=2;
                    }
                    else if(m==0) //(3,3)
                    {
                    g.drawOval (290,280,80,70);
                    m=2;
                    }
                    else if(k==0) //(1,3)
                    {
                    g.drawOval (23,280,80,70);
                    k=2;
                    }
                    else if(f==0) //(3,1)
                    {
                    g.drawOval (280,20,115,70);
                    f=2;
                    }
                    else if(t==0)//(2,1)
                    {
                    g.drawOval (150,20,100,70);
                    t=2;
                    }
                    else if(u==0) //(1,2) 
                    {
                    g.drawOval (25,155,80,70);
                    u=2;
                    }
                    else if(l==0) //(2,3) bottom middle
                    {
                    g.drawOval (155,280,80,70);
                    l=2;
                    }
                    else if (j==0) //(3,2)
                    {
                    g.drawOval (290,155,80,70);
                    j=0;
                    }       
                    if (s==2 && h==2 && m==2 || s==2 && t==2 && f==2 || k==2 && h==2 && f==2 ||s==2 && u==2 && k==2 || u==2 && h==2 && j==2 || k==2 && l==2 && m==2 || t==2 && h==2 && l==2 || f==2 && j==2 && m==2)
                    {
                        who = 10; //o
                        won =1;
                    }

                //}
                else if (s>0 && t>0 && f>0 && h>0 && u>0 && j>0 && k>0 && l>0 && m>0)
                {
                    JOptionPane.getRootFrame().dispose();
                    String tie = "It is a draw!!";
                    JOptionPane.showMessageDialog (null, tie);
                    System.exit(0);
                }   
                /*if (won == 1 && who == 5) // if x won
                {
                    String xwon = "You won!!!!! NICE JOB!!";
                    JOptionPane.showMessageDialog (null, xwon);
                        System.exit(0);
         开发者_JS百科       }*/

                if (won == 1 && who == 10) // if o won
                {
                    JOptionPane.getRootFrame().dispose();
                    String owon = "You LOST!!! HAHAHAHHAHAH";
                    JOptionPane.showMessageDialog (null, owon);
                    System.exit(0);
                }

            }
            }                   

      }

   }

here is the error code

Exception occurred during event dispatching:
java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.SecurityManager.checkExit(SecurityManager.java:744)
    at java.lang.Runtime.exit(Runtime.java:88)
    at java.lang.System.exit(System.java:904)
    at TicTac2.paint(TicTac2.java:264)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
    at sun.awt.RepaintArea.paint(RepaintArea.java:224)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
    at java.awt.Component.dispatchEventImpl(Component.java:4706)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
    at java.awt.Dialog$1.run(Dialog.java:1046)
    at java.awt.Dialog$3.run(Dialog.java:1098)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Dialog.show(Dialog.java:1096)
    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:566)
    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:508)
    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:458)
    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:424)
    at TicTac2.paint(TicTac2.java:101)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
    at sun.awt.RepaintArea.paint(RepaintArea.java:224)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
    at java.awt.Component.dispatchEventImpl(Component.java:4706)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:417)
    at java.lang.Integer.parseInt(Integer.java:499)
    at TicTac2.paint(TicTac2.java:102)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
    at sun.awt.RepaintArea.paint(RepaintArea.java:224)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
    at java.awt.Component.dispatchEventImpl(Component.java:4706)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Replace line 264..

System.exit(0);

..with..

getAppletContext().showDocument(new URL("ThanksForUsingOurApplet.html"));

An applet is a guest in a web page & might share a VM. Calling System.exit(n) is like the guest burning down the guest house.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜