Results 1 to 10 of 10
Thread: Convert applet from AWT to Swing
- 09-03-2012, 02:27 AM #1
Member
- Join Date
- Sep 2012
- Location
- Cambridge UK
- Posts
- 5
- Rep Power
- 0
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); } }
- 09-03-2012, 03:15 AM #2
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.If you don't understand my response, don't ignore it, ask a question.
- 09-03-2012, 11:03 AM #3
Member
- Join Date
- Sep 2012
- Location
- Cambridge UK
- Posts
- 5
- Rep Power
- 0
Re: Convert applet from AWT to Swing
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?
- 09-03-2012, 12:12 PM #4
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-03-2012, 01:39 PM #5
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.
Use the add() method.Is there a way of adding the JPanel to a JApplet?If you don't understand my response, don't ignore it, ask a question.
- 09-03-2012, 10:45 PM #6
Member
- Join Date
- Sep 2012
- Location
- Cambridge UK
- Posts
- 5
- Rep Power
- 0
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
MainPanel.javaJava 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); } }
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); } }
- 09-03-2012, 11:12 PM #7
Re: Convert applet from AWT to Swing
What errors do you get when you execute the code?it still doesn't work, it compiles but nothing is shownIf you don't understand my response, don't ignore it, ask a question.
- 09-03-2012, 11:12 PM #8
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-03-2012, 11:27 PM #9
Member
- Join Date
- Sep 2012
- Location
- Cambridge UK
- Posts
- 5
- Rep Power
- 0
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.
- 09-04-2012, 12:08 AM #10
Member
- Join Date
- Sep 2012
- Location
- Cambridge UK
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Convert THE APPLET CODE TO SWING USING JPANEL
By jammyjamsticy in forum AWT / SwingReplies: 2Last Post: 12-22-2011, 09:16 AM -
convert swings to applet
By prakash_moturu in forum Java AppletsReplies: 1Last Post: 09-08-2009, 03:12 PM -
how to convert SWING GUI into exe format
By santhosh_el in forum AWT / SwingReplies: 7Last Post: 04-04-2009, 12:36 PM -
Convert to Applet
By Urgle in forum New To JavaReplies: 1Last Post: 11-12-2008, 01:15 PM -
Convert a program to Applet
By carl in forum Java AppletsReplies: 2Last Post: 08-09-2007, 09:33 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks