Mouse Maze Plz Help...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Mouse Maze Plz Help...

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Unhappy Mouse Maze Plz Help...

    This is my code..

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    /*
     * <html>
     * <head>
     * <title>My Game</title>
     * </head>
     * <body>
     * <applet code="C.class" width=500 height=410></applet>
     * </body>
     * </html>
     */
    
    
    
    public class C extends Applet implements 
    ActionListener,MouseMotionListener,Runnable
    {
        Button b;
        Label l1,l2,l3;
        Font f;
        AudioClip au;
        int min,sec;
        Thread t;
        int xpos,ypos,ht,wd;
        boolean failed,success,button;
        public void initialise()
        {
            l1=new Label("Move The Pointer Without Touching The Sides : ");
            min=sec=0;
            au=getAudioClip(getDocumentBase(),"");
            t=new Thread(this,"Timer");
            setBackground(Color.black);
            setForeground(Color.white);
            f=new Font("Trash",Font.BOLD|Font.ITALIC,16);
            setFont(f);
            try
            {
            Thread.sleep(1500);
            }catch(Exception e)
            {
            }
            addMouseMotionListener(this);
            t.start();
            add(l1);
            success=false;
            failed=false;
            button=true;  
        }
        
        public void start()
        {
            initialise();
        }
        
        public void mouseDragged(MouseEvent e)
        {
        }
        
        public void mouseMoved(MouseEvent e)
        {
            xpos=e.getX();
            ypos=e.getY();
            if(!((xpos>=0 && ypos>200 && xpos<=70 && ypos<=230) || 
    (xpos>70 && xpos<=80 && ypos<220 && ypos>=160) || (xpos>70 && 
    xpos<=200 && 
    ypos<160 && ypos>=150) || (xpos>200 && xpos<=210 && ypos>150 
    && ypos<=310) || (xpos>210 && xpos<=300 && ypos<310 && 
    ypos>300)))
            {
               failpaint();
            }
            
            if(xpos>285 && xpos<355 && ypos <340 && ypos>270)
            { 
                successpaint();
            }
            
        }
        
        public void failpaint()
        {
            removeMouseMotionListener(this);
            failed=true;
            repaint();
            
        }
        
        public void successpaint()
        {
            removeMouseMotionListener(this);
            success=true;
            repaint();
        }
        
        public void paint(Graphics g)
        {
            if(!failed && !success)
            {
               g.setFont(new Font("Harlow Solid Italic",Font.BOLD,20));
               g.setColor(Color.white);
               g.fillRect(0,200,70,30);
               g.fillRect(70,160,10,60);
               g.fillRect(70,150,130,10);
               g.fillRect(200,150,10,160);
               g.fillRect(210,300,90,10);
               g.setColor(Color.LIGHT_GRAY);
               g.fillRect(285,270,70,70);
               g.setColor(Color.white);
               g.drawString("Reach Here ! ",310,300);
               g.setFont(new Font("Algerian",Font.BOLD|Font.ITALIC,25));
               g.drawString("Time - " + min + " : " + sec, 40,380);
            }
            
            else if(failed && button)
            {
                remove(l1);
                g.setFont(new Font("Algerian",Font.BOLD|Font.ITALIC,25));
                g.setColor(Color.white);
                g.drawString("YOU LOST !!!",110,190);
                g.setFont(f);
                g.setColor(Color.white);
                button=false;
                addButton();
            }
            
            else if(success && button)
            {
                g.setColor(Color.white);
                remove(l1);
                g.setFont(new Font("Algerian",Font.BOLD|Font.ITALIC,18));
                g.drawString("YOU WON !!! And You Took " + min + " Mins and " 
    + sec + " Secs !!",10,190);
                g.setFont(f);
                g.setColor(Color.white);
                button=false;
                addButton();
            }
            
               
            
        }
        
        
        public void addButton()
        {
            b=new Button("Retry");
            add(b);
            b.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==b)
            initialise();
        }
        
        public void run()
        {
            while(!success && !failed)
            {
                try
                {
                    repaint();
                    sec++;
                    if(sec>=60)
                        min++;
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    
                }
            }
        }
        
    }
    What I am trying to do is to create a mouse maze and if the user wins or loses it, I display another Screen dislaying that he has won or has host and add one more button "Retry" to start from the first.. Maze works fine but my Problem is that the Button on the second screen doesnt appear... Plz run the applet for more details.. Well the Applet does not hav a good look as am yet to finesse it
    Thanks in Advance

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mouse Maze Plz Help...

    You can't expect the button to display if you are handling the painting yourself but don't ever call the button's paint method.

    Your design is flawed. If you want to mix AWT components with your own drawing you should add a Canvas which you draw on rather than overriding the applets paint method.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center