The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet won't run!!! Need help!

2 replies on 1 page. Most recent reply: Sep 23, 2003 10:16 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Anthony

Posts: 1
Nickname: aznstyl
Registered: Sep, 2003

Applet won't run!!! Need help! Posted: Sep 23, 2003 9:03 AM
Reply to this message Reply
Advertisement
DrawHouse.Java ===
==================
import java.applet.Applet;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
 
public class DrawHouses extends Applet{
	ArrayList houses;
	Point2D.Double topLeft, bottomRight;
 
	public DrawHouses(){
		houses = new ArrayList();
		addMouseListener(new MyMouseListener());
	}
 
	private class MyMouseListener extends MouseAdapter{
		public void mousePressed(MouseEvent e){
			topLeft = new Point2D.Double(e.getX(),e.getY());
		}
		public void mouseReleased (MouseEvent e){
			bottomRight = new Point2D.Double(e.getX(),e.getY());
			houses.add(new House(bottomRight.getX() - topLeft.getX(),
			                     bottomRight.getY() - topLeft.getY(),
			                     topLeft));
			repaint();
		}
	}
 
	public void paint(Graphics g){
		for(int i = 0; i < houses.size(); i++)
			((House)(houses.get(i))).draw(g);
	}
 
 
}

==============================
House.java
==============================
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.Graphics2D;
 
public class House extends Applet
{
	public House(double x,double y)
	{
     	xLeft = x;
     	yTop = y;
    }
 
    public void draw(Graphics g)
    {
		Graphics2D g2 = (Graphics2D)g;
 
      	resize(210,210);
 
      	Point2D.Double k1 = new Point2D.Double(100,100);
      	Point2D.Double k2 = new Point2D.Double(50,150);
      	Point2D.Double k3 = new Point2D.Double(150,150);
 
      	Line2D.Double sideOne = new Line2D.Double(k1,k2);
      	Line2D.Double sideTwo = new Line2D.Double(k1,k3);
      	Line2D.Double sideThree = new Line2D.Double(k2,k3);
 
 
 
     	Rectangle2D.Double leftRectangle = new Rectangle2D.Double(100,150,30,60);
     	Rectangle2D.Double rightRectangle = new Rectangle2D.Double(160,150,30,60);
     	Rectangle2D.Double windowRectangle = new Rectangle2D.Double(40,50,20,40);
     	Rectangle2D.Double panesRectangle = new Rectangle2D.Double(20,25,10,20);
     	Rectangle2D.Double doorrectangle = new Rectangle2D.Double(85,100,20,40);
 
		Line2D.Double leftLine = new Line2D.Double(100,150,30,60);
      	Line2D.Double rightLine = new Line2D.Double(160,150,30,60);
      	Line2D.Double window = new Line2D.Double(40,50,20,40);
      	Line2D.Double panes = new Line2D.Double(20,25,10,20);
      	Line2D.Double door = new Line2D.Double(85,100,20,40);
 
       	g2.translate(210,210);
       	g2.draw(sideOne);
       	g2.draw(sideTwo);
       	g2.draw(sideThree);
       	g2.draw(leftLine);
       	g2.draw(rightLine);
       	g2.draw(window);
       	g2.draw(panes);
       	g2.draw(door);
	}
 
       private double xLeft;
       private double yTop;
}

=======================================
the House compile but then the java applet won't run... what did I do wrong? Can some1 fix it and comment where I did wrong? Much Appreciated. Thanks. (I'm new to this forum)


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Applet won't run!!! Need help! Posted: Sep 23, 2003 10:15 PM
Reply to this message Reply
Applets

In an applet, the JVM looks for the init method first. For it to be recognized, its signature must be:

public void init()

Next the start with the following signature executes:

public void start()

The paint method is next, with the signature:

public void paint(Graphics g)

The paint method is actually executed whenever it is necessary for the JVM to render the applet on the screen, so it may run many times duirng the execution of the applet.

Other methods may also run in response to events, such as the clicking of a mouse, but this depends on whether the code for the applet invokes methods in response to events.

If the browser leaves the page on which the applet is located, the stop method is called:

public void stop()

If the browser returns to the applet's page, start runs again, theh paint and any other methods invoked by events that may occur.

At some time the applet terminates, either because the browser is closed or leaves the applet's page for a sufficient amount of time, or because the code explicitly terminates it. When this happens, the destroy method is called, which as the signature:

public void destroy()

All the methods described above for applets are inherited from JApplet. Therefore, they exist already, even if you do not write them for your applet. In fact, you can create an applet without writing any methods at all, but you will find it rather dull to execute, since it will not do anything. If you are skeptical, try running this!


This is like you java class without main method

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Applet won't run!!! Need help! Posted: Sep 23, 2003 10:16 PM
Reply to this message Reply
http://www.cs.man.ac.uk/~chris/cs2092/ch9.pdf

Flat View: This topic has 2 replies on 1 page
Topic: Please Help! Previous Topic   Next Topic Topic: extended hierarchy!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use