Jump to content
  • Advertisement
Sign in to follow this  
ManaStone

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

This topic is 5054 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!