Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    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.

    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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 07:25 AM.

Similar Threads

  1. Converting a String into a array.
    By taraxgoesxboom in forum New To Java
    Replies: 12
    Last Post: 02-22-2009, 05:29 AM
  2. Converting asp files to jsp
    By vrk in forum Advanced Java
    Replies: 5
    Last Post: 02-11-2009, 09:44 AM
  3. Converting netbeans app to applet
    By paulious in forum New To Java
    Replies: 3
    Last Post: 10-23-2008, 11:25 AM
  4. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:15 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 08:45 AM

Posting Permissions

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