Results 1 to 20 of 76
Thread: Applet Game
- 04-05-2014, 01:40 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Applet Game
Alright, so at this point I am pretty desperate. I am taking a distance ed course and it is a disaster (text is terrible, "tutor" never responds, resources a scarce and useless). I have one final project to do and I am really stuck because I do not understand how to make it work from a theoretical or practical stand point.
What I need to do is create a simple applet game where I use two existing classes (Martian and Jupiterian) to show up in the boxes of the game grid that I have created as an applet. The grid needs to filled with buttons that when clicked, will display one of the two images. In total there should be six martians and two jupiterians. I will post what I have so far (which is not particularly good) and if anyone has any guidance I would really really appreciate it as I just want to get out of this course and move on.
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MartianGame extends Applet { public void init() { } public void stop() { } public void paint(Graphics g) { //draws a grid g.drawRect(50,100,450,450); g.drawLine(200,100,200,550); g.drawLine(350,100,350,550); g.drawLine(50,250,500,250); g.drawLine(50,400,500,400); g.fillRect(200,250,150,150); } }
Java Code:public abstract class Alien { //setting up variables to be used. private int numEyes; protected int numLegs; protected int numArms; //This method acts as a container for the variables for the aliens in other classes. public Alien(int numEyes, int numLegs, int numArms) { setNumEyes(numEyes); setNumLegs(numLegs); setNumArms(numArms); } public int getNumEyes() { return numEyes; } public void setNumEyes(int eyes) { numEyes = eyes; } public int getNumLegs() { return numLegs; } public void setNumLegs(int legs) { numLegs = legs; } public int getNumArms() { return numArms; } public void setNumArms(int arms) { numArms = arms; } }
Java Code:import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class Jupiterian extends Alien { //this sets the number of eyes, number of legs, and number of arms for the alien public Jupiterian() { super(1,4,2); } //this sets the information to be displayed in the dialogue box public String toString() { return("The Jupiterian has " + getNumEyes() + " eyes, " + getNumLegs() + " legs, and " + getNumArms() + " arms."); } } //this class creates the frame for the drawing of the alien class DrawJupiterian extends JFrame { //this method sets the properties for the frame public void JShapes2D() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DrawJupiterian frame = new DrawJupiterian(); frame.setSize(500, 400); frame.setVisible(true); } //this method draws the shapes that make up the picture of the alien public void paint(Graphics gr) { super.paint(gr); Graphics2D gr2D = (Graphics2D)gr; Ellipse2D.Float body = new Ellipse2D.Float(100F, 90F, 200F, 200F); Ellipse2D.Float smallEye = new Ellipse2D.Float(140, 150, 20, 20); Ellipse2D.Float bigEye = new Ellipse2D.Float(130, 130, 40, 40); Ellipse2D.Float nose = new Ellipse2D.Float(190, 200, 20, 20); QuadCurve2D curveSmile = new QuadCurve2D.Float(150, 220, 150, 250, 250, 220); Line2D lineLeg1 = new Line2D.Float(140, 270, 140, 330); Line2D lineLeg2 = new Line2D.Float(180, 290, 180, 330); Line2D lineLeg3 = new Line2D.Float(220, 290, 220, 330); Line2D lineLeg4 = new Line2D.Float(260, 270, 260, 330); Line2D lineLeftArm = new Line2D.Float(100, 210, 70, 290); Line2D lineRightArm = new Line2D.Float(300, 210, 330, 290); gr2D.draw(body); gr2D.fill(smallEye); gr2D.draw(bigEye); gr2D.fill(nose); gr2D.draw(curveSmile); gr2D.draw(lineLeg1); gr2D.draw(lineLeg2); gr2D.draw(lineLeg3); gr2D.draw(lineLeg4); gr2D.draw(lineLeftArm); gr2D.draw(lineRightArm); } }
Java Code:import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class Martian extends Alien { public int xStart; public int yStart; public JFrame frame; //this sets the number of eyes, number of legs, and number of arms for the alien public Martian() { super(2, 3, 1); //sets 2 eyes, 3 legs, and 1 arm } //this sets the information to be displayed in the dialogue box public String toString() { return("The Martian has " + getNumEyes() + " eyes, " + getNumLegs() + " legs, and " + getNumArms() + " arms."); } } //this class creates the frame for the drawing of the alien class DrawMartian extends JFrame { //this method sets the properties for the frame public void JShapes2D() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DrawMartian frame = new DrawMartian(); frame.setSize(500, 400); frame.setVisible(true); } //this method draws the shapes that make up the picture of the alien public void paint(Graphics gr) { super.paint(gr); Graphics2D gr2D = (Graphics2D)gr; Rectangle2D.Float rectBody = new Rectangle2D.Float(100F, 150F, 100F, 100F); Rectangle2D.Float rectEye1 = new Rectangle2D.Float(120F, 165F, 10F, 10F); Rectangle2D.Float rectEye2 = new Rectangle2D.Float(170F, 165F, 10F, 10F); Rectangle2D.Float rectNose = new Rectangle2D.Float(145F, 190F, 10F, 20F); QuadCurve2D curveSmile = new QuadCurve2D.Float(120, 220, 150, 250, 170, 220); Line2D lineLeg1 = new Line2D.Float(120, 250, 120, 300); Line2D lineLeg2 = new Line2D.Float(145, 250, 145, 300); Line2D lineLeg3 = new Line2D.Float(170, 250, 170, 300); Line2D lineArm1 = new Line2D.Float(100, 200, 70, 250); gr2D.draw(curveSmile); gr2D.draw(rectBody); gr2D.fill(rectEye1); gr2D.fill(rectEye2); gr2D.fill(rectNose); gr2D.draw(lineLeg1); gr2D.draw(lineLeg2); gr2D.draw(lineLeg3); gr2D.draw(lineArm1); gr2D.drawString("I'm Marvin the Martian", 80, 330); } }
- 04-05-2014, 01:51 AM #2
Re: Applet Game
Work on it one step at a time:
grid needs to filled with buttonsIf you don't understand my response, don't ignore it, ask a question.
- 04-05-2014, 02:43 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
I have added buttons to the code, but do not know how to specify their position; it does not seem to work in the same way as a Swing GUI. And it is giving me an error for the "implements MouseListener"
Java Code:public class MartianGame extends Applet //implements MouseListener { Button topLeft; Button topMid; Button topRight; Button midLeft; Button midRight; Button botLeft; Button botMid; Button botRight; public void init() { setLayout(new FlowLayout()); topLeft = new Button("1"); topMid = new Button("2"); topRight = new Button("3"); midLeft = new Button("4"); midRight = new Button("5"); botLeft = new Button("6"); botMid = new Button("7"); botRight = new Button("8"); add(topLeft); add(topMid); add(topRight); add(midLeft); add(midRight); add(botLeft); add(botMid); add(botRight); }
Last edited by shodai; 04-05-2014 at 02:45 AM.
- 04-05-2014, 02:49 AM #4
Re: Applet Game
it does not seem to work in the same way as a Swing GUI.
What is different?If you don't understand my response, don't ignore it, ask a question.
- 04-05-2014, 03:13 AM #5
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
I would expect that I would use coordinates after the addition of the button.
- 04-05-2014, 03:16 AM #6
Re: Applet Game
What layout manager are you going to use?
How do you want the components positioned on the GUI?If you don't understand my response, don't ignore it, ask a question.
- 04-05-2014, 03:30 AM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
I do not which layout manager to use because well......I did not know that there were options. I have the grid set up the way that I want and I would like a button to be located in the approximate centre of each box in the grid.
- 04-05-2014, 03:44 AM #8
Re: Applet Game
If you want to specify the x,y locauons of the buttons, set layout null ands use the button's setBounds method
If you don't understand my response, don't ignore it, ask a question.
- 04-06-2014, 11:55 PM #9
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
Alright, so I have been able to set the buttons in the locations that I want, but the setSize function does not seem to be working. Any advice?
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MartianGame extends JApplet //implements MouseListener { private JPanel contentPanel; Button topLeft; Button topMid; Button topRight; Button midLeft; Button midRight; Button botLeft; Button botMid; Button botRight; private void add(Container con, Component widget, int left, int top, int width, int height) { widget.setBounds(left, top, width, height); con.add(widget); } public void init() { setSize(500,500); contentPanel = (JPanel)getContentPane(); contentPanel.setLayout(null); topLeft = new Button("1"); topMid = new Button("2"); topRight = new Button("3"); midLeft = new Button("4"); midRight = new Button("5"); botLeft = new Button("6"); botMid = new Button("7"); botRight = new Button("8"); //left, top, width, height add(contentPanel,topLeft,75,130,100,100); add(contentPanel,topMid,225,130,100,100); add(contentPanel,topRight,375,130,100,100); add(contentPanel,midLeft,75,280,100,100); add(contentPanel,midRight,375,280,100,100); add(contentPanel,botLeft,75,430,100,100); add(contentPanel,botMid,225,430,100,100); add(contentPanel,botRight,375,430,100,100); } public void stop() { } public void paint(Graphics g) { //draws a grid g.drawRect(50,100,450,450); g.drawLine(200,100,200,550); g.drawLine(350,100,350,550); g.drawLine(50,250,500,250); g.drawLine(50,400,500,400); g.fillRect(200,250,150,150); } }
- 04-07-2014, 12:15 AM #10
Re: Applet Game
setSize function does not seem to be working
How is the applet being executed? What is in the applet tag in the html?Last edited by Norm; 04-07-2014 at 12:17 AM.
If you don't understand my response, don't ignore it, ask a question.
- 04-07-2014, 03:01 AM #11
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
I have just been running the applet in my compiler (Dr Java). As it stands now it starts up fine, but only shows about 1/4 of the screen size.
I have been confused with the execution of applets and my text and teacher are not too useful. Do I need to make an HTML file to run an applet? Do I just make it in a text editor? How do I go about running the file?
Sorry for all the questions.
- 04-07-2014, 03:13 AM #12
Re: Applet Game
only shows about 1/4 of the screen
Are you saying the applet uses 1/4 of the monitor?
There are several html pages with applets on the forum. Do a Search.If you don't understand my response, don't ignore it, ask a question.
- 04-07-2014, 04:36 AM #13
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
First, i want you to know that I really appreciate your help and that I really am trying; so I am sorry if this is dumb. But what is wrong with the following code?
<html>
<object code = "MartianGame.class" width = 800 height = 500>
*</object>
</html>
When I click on the file it opens in a web page but it just displays the code???
- 04-07-2014, 01:46 PM #14
Re: Applet Game
Change object to applet and remove the *
If you don't understand my response, don't ignore it, ask a question.
- 04-07-2014, 03:12 PM #15
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
I have changed the code to the following but it is still just showing the code in the web browser??
<html>
<applet code = "MartianGame.class" width = 800 height = 500>
</applet>
</html>
- 04-07-2014, 04:13 PM #16
Re: Applet Game
Copy and paste here what is shown in the browser's window.
Try opening the html file in the AppletViewer program.Last edited by Norm; 04-07-2014 at 04:16 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-07-2014, 04:25 PM #17
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
This is what prints out on the screen (I added the body code to the text document)
<html>
<body>
<applet code = "MartianGame.class" width = "800" height = "500">
</applet>
</body>
</html>
- 04-07-2014, 04:31 PM #18
Re: Applet Game
What is the name of the file that contains the html?
What happens when you open the file with the AppletViewer program?If you don't understand my response, don't ignore it, ask a question.
- 04-07-2014, 06:25 PM #19
Senior Member
- Join Date
- Apr 2012
- Posts
- 112
- Rep Power
- 0
Re: Applet Game
The HTML file is called MartianGame.html. I do not seem to be able to identify the AppletViewer as I am using Mac and cannot find instructions on line for guidance.
- 04-07-2014, 06:27 PM #20
Similar Threads
-
game applet example
By hasan2222 in forum New To JavaReplies: 5Last Post: 05-29-2013, 04:47 PM -
Snake Game Applet
By Growler in forum Java AppletsReplies: 6Last Post: 07-11-2010, 03:47 PM -
Applet to Application (Game)
By AndrewM16921 in forum Java AppletsReplies: 3Last Post: 04-12-2009, 01:24 PM -
An Applet Game
By T3000 in forum New To JavaReplies: 1Last Post: 11-18-2008, 05:18 PM -
Battery 1.02 (Applet Game)
By M77 in forum Java SoftwareReplies: 3Last Post: 05-30-2008, 01:54 AM
Bookmarks