Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Code compiles but nothing happens when I run it

    Hello,

    I have written some Java to draw small circles on a canvas when the mouse is clicked.

    This file is called circles.java.

    I have written an app class to call this class called CirclesApp.java.

    They both compile with no errors, but when I run circlesApp.java nothing happens.

    I know this is probably something REALLY simple. Eventually I would like to run the canvas in circles.java inside a GUI interface. This was the idea behind the app class, as I'd like my GUI to call the circles.java file and run it inside the window.

    Here is my code. I've commented it, so hopefully it will make sense! :
    Code:
    import java.awt.*;
    import javax.swing.*;
    
     public class circles extends java.applet.Applet {
    	private int mouseX, mouseY;
        private boolean mouseclicked = false;
        
       //This is the first method called.  It makes the backround colour.
    	public void init()  {
    		setBackground(Color.magenta);
    	  }
    
    //event handler which decides if the mouse has been clicked.
       public boolean mouseDown(Event e, int x, int y ) {
           mouseX=x; mouseY=y;
           mouseclicked = true;
    
           //calls the paint method
           repaint();
           return true;
       }
    //g is a graphics object
        public void paint(Graphics g) {
    //sets the drawing (pen) color to blue by using the graphics context method setColor(Color). 
           g.setColor(Color.blue);
    //checks to ensure the mouse was clicked otherwise don't draw circle.
           if(mouseclicked) {
    //fill the oval with the blue colour....and the size of the rectangle (that surrounds the circle) to fill. 
               g.fillOval(mouseX, mouseY, 10, 10);
               mouseclicked = false;
           }
       }    
    //overwrite the update method so the previous dots stay on the screen.
      public void update (Graphics g) {
            paint(g);
        }
    }
    App class:
    Code:
    import java.awt.*;
    import javax.swing.*;
    public class circlesApp {
    
    public static void main(String[] args){
    circles circles = new circles();
    }
    }
    Thanks!

    Nicky

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you've to use the appletviewer or the browser to see applets
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    nickyfraggle (04-07-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    You are an absolute angel!! Thanks! Been banging my head against the wall all along and there was nothing wrong with my code!!

    Nicky


  •  

    Posting Permissions

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