Sign in to follow this  

[java] Help! How do I convert Applet to JApplet when drawing primitives ?

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to Java and I am trying to convert an applet from an AWT base applet to a Swing applet. Could somebody please tell me what I am doing wrong? This is the program importing java.applet which works:

import java.applet.*;

import java.awt.*;
import java.awt.event.*;

public class Robotz extends Applet
{
    public void init()
    {
        add( new Robot( Color.blue ) );
        add( new Robot( Color.red  ) );
        add( new Robot( Color.green) );
    }
}

class Robot extends    Canvas
            implements MouseMotionListener
{
    private Color myColor;
    private Point myLocation;

    public Robot( Color theColor )
    {
        myColor = theColor;
        setSize( 50, 50 );
        setBackground( theColor );
        addMouseMotionListener( this );
    }

    public void mouseMoved( MouseEvent evt )
    {
        myLocation = getLocation( );
        int dx = evt.getX( ), dy = evt.getY( );
        if ( dx > 25 )
            dx = dx - 50;
        if ( dy > 25 )
            dy = dy - 50;
        
        setLocation( myLocation.x + dx,
                     myLocation.y + dy );
    }

    public void mouseDragged( MouseEvent evt )
    {
        myLocation = getLocation();
        setLocation( myLocation.x + (evt.getX( ) - 25),
                     myLocation.y + (evt.getY( ) - 25));
    }

    public void paint( Graphics g )
    {
        g.drawLine( 10, 40, 40, 40 );
        g.drawRect( 15,  5,  5, 15 );
        g.drawRect( 30,  5,  5, 15 );
    }
}


This is my conversion which doesn’t work:
import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;
public class Robotz extends JApplet
{
    public void init()
    {
        add( new Robot( Color.blue ) );
        add( new Robot( Color.red  ) );
        add( new Robot( Color.green) );
    }
}

class Robot extends    JPanel 
            implements MouseMotionListener
{
    private Color myColor;
    private Point myLocation;

    public Robot( Color theColor )
    {
        myColor = theColor;
        setSize( 50, 50 );
        setBackground( theColor );
        addMouseMotionListener( this );
    }

    public void mouseMoved( MouseEvent evt )
    {
        myLocation = getLocation( );
        int dx = evt.getX( ), dy = evt.getY( );
        if ( dx > 25 )
            dx = dx - 50;
        if ( dy > 25 )
            dy = dy - 50;
        
        setLocation( myLocation.x + dx,
                     myLocation.y + dy );
    }

    public void mouseDragged( MouseEvent evt )
    {
        myLocation = getLocation();
        setLocation( myLocation.x + (evt.getX( ) - 25),
                     myLocation.y + (evt.getY( ) - 25));
    }

    public void paint( Graphics g )
    {
        g.drawLine( 10, 40, 40, 40 );
        g.drawRect( 15,  5,  5, 15 );
        g.drawRect( 30,  5,  5, 15 );
        
    }
}

Share this post


Link to post
Share on other sites
Like other swing container, use getContentPane().add() instead directly add()
So in init() it should be like this:

public void init()
{
getContentPane().add( new Robot( Color.blue ) );
getContentPane().add( new Robot( Color.red ) );
getContentPane().add( new Robot( Color.green) );
}





EDIT: I think in swing, painting should be override public void paintComponent() instead public void paint()
But I'm not sure, please read the API, it's been long time since I use swing component :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Correctamundo on paintComponent(). Also you should never call paint() directly, call repaint() instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by role
Like other swing container, use getContentPane().add() instead directly add()
So in init() it should be like this:
*** Source Snippet Removed ***

EDIT: I think in swing, painting should be override public void paintComponent() instead public void paint()
But I'm not sure, please read the API, it's been long time since I use swing component :)


as of jdk1.4 getContentPane() is not necessary

Share this post


Link to post
Share on other sites

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this