Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-20-2009, 08:57 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
josephdcoleman is on a distinguished road
Default Converting an App to Applet
I am having a problem converting an old paint program to an applet. I can get it to compile and run, but when the applet window comes up it is blank. Thanks for any help.

Code:
/**
 * @(#)TestApplet2.java
 *
 * TestApplet2 Applet application
 *
 * @author 
 * @version 1.00 2009/2/20
 */
 
import java.awt.*;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;

public class TestApplet2 extends JApplet {
	
   private int pointCount = 0; // count number of points

   // array of 2 java.awt.Point references
   private Point points[] = new Point[ 2 ];  
   
   private boolean p0Exists = false; 
   private boolean p1Exists = false; 
   private int r = 8;
	
	public void init() {
		
		JFrame mainframe = new JFrame();
		TestApplet2 paintPanel = new TestApplet2();
		mainframe.add(paintPanel, BorderLayout.CENTER);
		
		JPanel bottomPanel = new JPanel();
		
		//label 1
		JLabel label1 = new JLabel("1. Click in two Locations.");
		label1.setBackground(Color.CYAN);
		label1.setOpaque(true);
		bottomPanel.add(label1, BorderLayout.SOUTH); //add label to JPanel
		
		//label 2
		JLabel label2 = new JLabel("2. Drag the points.");
		label2.setBackground(Color.YELLOW);
		label2.setOpaque(true);
		bottomPanel.add(label2, BorderLayout.SOUTH);
		
		mainframe.add(bottomPanel, BorderLayout.SOUTH);
	}
	
	public TestApplet2()
	{
		super();
		this.setBackground( Color.WHITE );
		
		// handle frame mouse  event
		addMouseListener(
		
			new MouseAdapter() // anonymous inner class
			{  
				// define a point and repaint
				public void mousePressed( MouseEvent event )
				{
					if ( !p0Exists ) 
					{
						// create point0
						points[ 0 ] = new Point( event.getX(), event.getY() ); 
						//System.out.println("Point 0 created: " + points[ 0 ] );
						p0Exists = true;
						repaint();	
					} else if ( !p1Exists ) 
					{
						// create point1
						points[ 1 ] = new Point( event.getX(), event.getY() ); 
						//System.out.println("Point 1 created: " + points[ 1 ] );
						p1Exists = true;
						repaint();	
					}
				
				} // end method mousePressed
				
			} // end anonymous inner class
		); // end call to addMouseMotionListener
		
		// handle frame mouse motion event
		addMouseMotionListener(
		
			new MouseMotionAdapter() // anonymous inner class
			{  
				// drag the point and repaint
				public void mouseDragged( MouseEvent event )
				{
					//System.out.println("Mouse dragged: x=" + event.getX() + "  y=" + event.getY() );
					if ( p0Exists ) 
					{
						if ( Math.abs(points[ 0 ].x - event.getX()) < r 
							&& Math.abs(points[ 0 ].y - event.getY()) < r ) 
						{
							// set new coordinates for point0
							points[ 0 ] = new Point( event.getX(), event.getY() ); 
							repaint();	
						}
					} 
					if ( p1Exists ) 
					{
						if ( Math.abs(points[ 1 ].x - event.getX()) < r 
							&& Math.abs(points[ 1 ].y - event.getY()) < r ) 
						{
							// set new coordinates for point1
							points[ 1 ] = new Point( event.getX(), event.getY() ); 
							repaint();	
						}
					}					
				} // end method mouseDragged
			} // end anonymous inner class
		); // end call to addMouseMotionListener
	} // end TestApplet2 constructor

	// draw oval in a 4-by-4 bounding box at specified location on window
	public void paintComponent( Graphics g )
	{
		this.paintComponent( g ); // clears drawing area
		if ( p0Exists ) 
		{
			g.drawOval( points[ 0 ].x - r/2, points[ 0 ].y - r/2, r, r );
			g.drawString("Point0", points[ 0 ].x, points[ 0 ].y - 3*r );
			g.drawString("x=" + points[ 0 ].x + " y=" + points[ 0 ].y, 
										points[ 0 ].x, points[ 0 ].y - r );
		}
		if ( p1Exists ) 
		{
			g.drawOval( points[ 1 ].x - r/2, points[ 1 ].y - r/2, r, r );
			g.drawString("Point1", points[ 1 ].x, points[ 1 ].y - 3*r );
			g.drawString("x=" + points[ 1 ].x + " y=" + points[ 1 ].y, 
										points[ 1 ].x, points[ 1 ].y - r );
		}
	} // end method paint
	
} // end class TestApplet2
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-21-2009, 08:07 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,483
Rep Power: 8
Fubarable is on a distinguished road
Default
You need to read up more on how to create applets as yours I'm afraid is completely mixed up. You appear to be creating a new object of your applet class in its init method -- not a good thing to do, and then try to add this pseudo-applet object to the contentPane of a JFrame -- an even worse thing to do. You can't band-aid these two beasts together like this.

Then your class that creates the JApplet has a constructor which is probably not what you want to do.

What you likely want is a class that creates a GUI JPanel that has a constructor and all. Then I'd have a separate class with an init method that extends JApplet and would add a newly created JPanel GUI to the JApplet's contentPane. Get rid of all references to JFrames, and most importantly, read the tutorial on how to make applets.

Best of luck.

edit: also your paintComponent method currently will do nothing since it's not held in a class that will call this as an override when the JVM calls paint. It's a good idea to use an @Override annotation here to see if the method you've created is actually overriding an existing method (as it should be doing).

Last edited by Fubarable; 02-21-2009 at 08:25 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a String into a array. taraxgoesxboom New To Java 12 02-22-2009 06:29 AM
Converting asp files to jsp vrk Advanced Java 5 02-11-2009 10:44 AM
Converting netbeans app to applet paulious New To Java 3 10-23-2008 12:25 PM
Converting URL to URI Java Tip Java Tips 0 12-26-2007 11:15 AM
help with converting to JApplet Simmy AWT / Swing 2 08-09-2007 09:45 AM


All times are GMT +2. The time now is 02:54 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org