Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By Norm
  • 1 Post By DarrylBurke
  • 1 Post By Norm

Thread: Convert applet from AWT to Swing

  1. #1
    willp is offline Member
    Join Date
    Sep 2012
    Location
    Cambridge UK
    Posts
    5
    Rep Power
    0

    Default Convert applet from AWT to Swing

    Hi I'm trying to convert my applet from awt to swing so that when I drag an object it doesn't flicker using swing auto double buffering. I've made a small change of changing Applet to JApplet and importing swing, this does stop the flickering however it also draws the path that object was dragged across (therefore drawing a line to the screen). How can I stop it from drawing the objects path and just dragging the object across the screen? are there more changes needed to fully convert the Applet to swing? Any help will be much appreciated. Here is my code:
    Java Code:
    package mandAndDog;
    
    import javax.swing.*;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.Random;
    
    
    public class SheepDog extends JApplet implements MouseListener, MouseMotionListener
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	/**
    	 * 
    	 */
    
    	Dog dog;
    	Sheep sheep;
    	int[] directionNumbersLeft = {0, 1, 3};
    	int[] directionNumbersUp = {0, 1, 2};
    	int x;
    	int selection;
    	int xposR;
    	int yposR;
    	int sheepx;
    	int sheepy;
    	int sheepBoundsx;
    	int sheepBoundsy;
    	int MAX_DISTANCE = 50;
        int direction;
    	int distance;
    	Boolean sheepisclosetodog;
    
    
    	public void init()
    	{
    		addMouseListener(this);
    	    addMouseMotionListener(this);
    	    dog = new Dog(10, 10);
    	    sheepx = 175;
    	    sheepy = 75;
    	    sheep = new Sheep(sheepx, sheepy);
    	    sheepBoundsx = 30;
    	    sheepBoundsy = 30;
    	    direction = (int)(Math.random()*4); 
    	    distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE;
    	    sheepisclosetodog = false;
    	    Random rand = new Random();
    	    x = rand.nextInt(3);
    	    selection = directionNumbersLeft[x];
    
    	}
    	public void paint(Graphics g)
    	{
    		dog.display(g);
    		sheep.display(g);
    		g.drawString(Integer.toString(distance), 85, 100);
    		g.drawString(Integer.toString(direction), 85, 125);
    		g.drawString(Integer.toString(selection), 85, 140);
    
    	}
    
    	public void mousePressed(MouseEvent e)
    	{}
    	public void mouseReleased(MouseEvent e)
    	{}
    	public void mouseEntered(MouseEvent e)
    	{}
    	public void mouseExited(MouseEvent e)
    	{}
    	public void mouseMoved(MouseEvent e)
    	{
    	}
    	public void mouseClicked(MouseEvent e)
    	{}
    	public void mouseDragged(MouseEvent e)
    	{
    		dog.setLocation(xposR, yposR);
    		sheep.setLocation(sheepx, sheepy);
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 0){
    			sheepx = sheepx + 50;
    			direction = (int)(Math.random()*4); 
    		}
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 1){
    			sheepy = sheepy + 50;
    			direction = (int)(Math.random()*4); 
    		}
    		
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 2){
    			sheepx = sheepx - 50;
    			direction = (int)(Math.random()*4); 
    		}
    		if (sheepx <= 20){
    			direction = directionNumbersLeft[x];
    		}
    		
    		
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 3){
    			sheepy = sheepy - 50;
    			direction = (int)(Math.random()*4); 
    		}
    		if (sheepy <=20){
    			direction = directionNumbersUp[x];
    		}
    		
    		xposR = e.getX();
    		yposR = e.getY();
    		repaint();
    
    	}
    
    }
    
    class Dog 
    {
    	int xpos;
    	int ypos;
    	int circleWidth = 30;
    	int circleHeight = 30;
    
    	public Dog(int x, int y)
    	{
    		xpos = x;
    		ypos = y;
    
    	}
    	
    	public void setLocation(int lx, int ly)
    	{
    		xpos = lx;
    		ypos = ly;
    	}
    
    	public void display(Graphics g)
    	{
    		g.setColor(Color.blue);
    		g.fillOval(xpos, ypos, circleWidth, circleHeight);
    	}		
    }
    class Sheep
    {
    	int xpos;
    	int ypos;
    	int circleWidth = 30;
    	int circleHeight = 30;
    
    	public Sheep(int x, int y)
    	{
    		xpos = x;
    		ypos = y;
    
    	}
    	
    	public void setLocation(int lx, int ly)
    	{
    		xpos = lx;
    		ypos = ly;
    	}
    
    	public void display(Graphics g)
    	{
    		g.setColor(Color.green);
    		g.fillOval(xpos , ypos, circleWidth, circleHeight);
    		g.drawOval(xpos - 20, ypos - 20, 50, 50);
    	}
    
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Convert applet from AWT to Swing

    You've overriden the paint() of the JApplet class. I don't think you are getting the double buffering.
    Try creating a JPanel, moving and renaming the paint() method to paintComponent() into the JPanel class.
    Add a call to the super method.
    willp likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    willp is offline Member
    Join Date
    Sep 2012
    Location
    Cambridge UK
    Posts
    5
    Rep Power
    0

    Default Re: Convert applet from AWT to Swing

    Quote Originally Posted by Norm View Post
    You've overriden the paint() of the JApplet class. I don't think you are getting the double buffering.
    Try creating a JPanel, moving and renaming the paint() method to paintComponent() into the JPanel class.
    Add a call to the super method.
    Hi thanks for your reply but sorry I'm still a bit lost, im new to java. I changed the main "SheepDog" class to a JPanel and changed paint() to paintComponent() and also added the line super.paintComponent(g); in the paintComponent() but it wont run now. Is there a way of adding the JPanel to a JApplet?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Convert applet from AWT to Swing

    Better to go through the tutorial than to keep guessing at stuff.
    Trail: Creating a GUI With JFC/Swing (The Java Tutorials)

    db
    willp likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Convert applet from AWT to Swing

    Leave SheepDog as a JApplet. Create a new class that extends JPane and move the paintComponent() method into the new class.
    Create an instance of the new class and add it to the SheepDog class.
    Is there a way of adding the JPanel to a JApplet?
    Use the add() method.
    willp likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    willp is offline Member
    Join Date
    Sep 2012
    Location
    Cambridge UK
    Posts
    5
    Rep Power
    0

    Default Re: Convert applet from AWT to Swing

    Hi, I'm afraid I still cant get it to work, I created a separate class (MainPanel) for the JPanel, put the paintComponent() in that class, I then created an instance of the new class in the SheepDog class, however it still doesn't work, it compiles but nothing is shown in the applet. I feel like I'm really close, any ideas whats wrong.
    here is the code:

    SheepDog.java

    Java Code:
    package mandAndDog;
    
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.util.Random;
    
    import javax.swing.*;
    
    
    public class SheepDog extends JApplet implements MouseMotionListener
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	/**
    	 * 
    	 */
    
    	Dog dog;
    	Sheep sheep;
    	int[] directionNumbersLeft = {0, 1, 3};
    	int[] directionNumbersRight = {1, 2, 3};
    	int[] directionNumbersUp = {0, 1, 2};
    	int[] directionNumbersDown = {0, 2, 3};
    	int x;
    	int selection;
    	int xposR;
    	int yposR;
    	int sheepx;
    	int sheepy;
    	int sheepBoundsx;
    	int sheepBoundsy;
    	int MAX_DISTANCE = 50;
        int direction;
    	int distance;
    	MainPanel mainPanel;
    
    
    
    	public void init()
    	{
    	    mainPanel = new MainPanel();
    	    add(mainPanel);
    		addMouseMotionListener(this);
    	    dog = new Dog(10, 10);
    	    sheepx = 175;
    	    sheepy = 75;
    	    sheep = new Sheep(sheepx, sheepy);
    	    sheepBoundsx = 30;
    	    sheepBoundsy = 30;
    	    direction = (int)(Math.random()*4); 
    	    distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE;
    	    Random rand = new Random();
    	    x = rand.nextInt(3);
    	    selection = directionNumbersLeft[x];
    
    	}
    
    
    	public void mousePressed(MouseEvent e)
    	{}
    	public void mouseReleased(MouseEvent e)
    	{}
    	public void mouseEntered(MouseEvent e)
    	{}
    	public void mouseExited(MouseEvent e)
    	{}
    	public void mouseMoved(MouseEvent e)
    	{
    	}
    	public void mouseClicked(MouseEvent e)
    	{}
    	public void mouseDragged(MouseEvent e)
    	{
    		dog.setLocation(xposR, yposR);
    		sheep.setLocation(sheepx, sheepy);
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 0){
    			sheepx = sheepx + 50;
    			direction = (int)(Math.random()*4); 
    		}
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 1){
    			sheepy = sheepy + 50;
    			direction = (int)(Math.random()*4); 
    		}
    		
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 2){
    			sheepx = sheepx - 50;
    			direction = (int)(Math.random()*4); 
    		}
    		
    		if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
    		        && yposR < sheepy+sheepBoundsy && direction == 3){
    			sheepy = sheepy - 50;
    			direction = (int)(Math.random()*4); 
    		}
    		if (sheepx <= 20){
    			direction = directionNumbersLeft[x];
    		}
    		if (sheepy <=20){
    			direction = directionNumbersUp[x];
    		}
    		if (xposR < sheepx){
    			direction = directionNumbersLeft[x];
    		}
    		if (xposR > sheepx){
    			direction = directionNumbersRight[x];
    		}
    		if (yposR < sheepy){
    			direction = directionNumbersUp[x];
    		}
    		if (yposR > sheepy){
    			direction = directionNumbersDown[x];
    		}
    		
    		
    		xposR = e.getX();
    		yposR = e.getY();
    		repaint();
    
    	}
    
    }
    class Dog 
    {
    	int xpos;
    	int ypos;
    	int circleWidth = 30;
    	int circleHeight = 30;
    
    	public Dog(int x, int y)
    	{
    		xpos = x;
    		ypos = y;
    
    	}
    	
    	public void setLocation(int lx, int ly)
    	{
    		xpos = lx;
    		ypos = ly;
    	}
    
    	public void display(Graphics g)
    	{
    		g.setColor(Color.blue);
    		g.fillOval(xpos, ypos, circleWidth, circleHeight);
    	}		
    }
    
    class Sheep
    {
    	int xpos;
    	int ypos;
    	int circleWidth = 30;
    	int circleHeight = 30;
    
    	public Sheep(int x, int y)
    	{
    		xpos = x;
    		ypos = y;
    
    	}
    	
    	public void setLocation(int lx, int ly)
    	{
    		xpos = lx;
    		ypos = ly;
    	}
    
    	public void display(Graphics g)
    	{
    		g.setColor(Color.green);
    		g.fillOval(xpos , ypos, circleWidth, circleHeight);
    		g.drawOval(xpos - 10, ypos - 10, 50, 50);
    	}
    
    
    }
    MainPanel.java

    Java Code:
    package mandAndDog;
    
    import java.awt.Graphics;
    
    
    import javax.swing.JPanel;
    
    public class MainPanel extends JPanel 
    {
    
    	private static final long serialVersionUID = 1L;
    	Dog dog;
    	Sheep sheep;
    	
    	public MainPanel()
    	{}
    
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		dog.display(g);
    		sheep.display(g);
    	}
    
    	
    
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Convert applet from AWT to Swing

    it still doesn't work, it compiles but nothing is shown
    What errors do you get when you execute the code?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Convert applet from AWT to Swing

    You MainPanel class is never passed instances of Dog and Sheep, so it doesn't know about the variables in the SheepDog class.

    How are you running that Applet? If in a browser, check the Java console for the NullPointerExceptions arising from MainPanel's paintComponent(...) method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    willp is offline Member
    Join Date
    Sep 2012
    Location
    Cambridge UK
    Posts
    5
    Rep Power
    0

    Default Re: Convert applet from AWT to Swing

    I'm using eclipse and viewing the applet with appletviewer. Appletviewer opens but nothing is shown, when i click on the screen the following error message is shown in the console:
    Exception in "AWT-EventQueue-1" java.lang.NullPointerException at mandAndDog MainPanel paintComponent(MainPanel.java 22).

    Do I need to create instances of the sheep and dog objects in both the SheepDog class and the MainPanel class?

    Thanks for your help guys its much appreciated.

  10. #10
    willp is offline Member
    Join Date
    Sep 2012
    Location
    Cambridge UK
    Posts
    5
    Rep Power
    0

    Default Re: Convert applet from AWT to Swing

    Got it working, thanks guys for the help.

Similar Threads

  1. Convert THE APPLET CODE TO SWING USING JPANEL
    By jammyjamsticy in forum AWT / Swing
    Replies: 2
    Last Post: 12-22-2011, 09:16 AM
  2. convert swings to applet
    By prakash_moturu in forum Java Applets
    Replies: 1
    Last Post: 09-08-2009, 03:12 PM
  3. how to convert SWING GUI into exe format
    By santhosh_el in forum AWT / Swing
    Replies: 7
    Last Post: 04-04-2009, 12:36 PM
  4. Convert to Applet
    By Urgle in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 01:15 PM
  5. Convert a program to Applet
    By carl in forum Java Applets
    Replies: 2
    Last Post: 08-09-2007, 09:33 PM

Tags for this Thread

Posting Permissions

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