View Full Version : Code compiles but nothing happens when I run it

04-02-2008, 11:57 AM

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! :

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() {

//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
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).
//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) {

App class:

import java.awt.*;
import javax.swing.*;
public class circlesApp {

public static void main(String[] args){
circles circles = new circles();



04-07-2008, 05:04 AM
you've to use the appletviewer (http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/appletviewer.html) or the browser to see applets

04-07-2008, 10:21 AM
You are an absolute angel!! Thanks! Been banging my head against the wall all along and there was nothing wrong with my code!!


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum