Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Applets
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Sat Sep 30, 2006 10:06 am   Post subject: Java Applets

I'm trying to learn Java applets from my textbook and I'm a super-beginner, but the textbook is really horrible.
Can anyone anyone tell me what to do with this code?
code:
import java.awt.*;
import java.applet.*;
public class Shapes_Color extends Applet {
        public void paint (Graphics g) {
                setBackground (Color.darkGray);
                g.setColor (Color.pink);
                g.drawRect (50, 50, 40, 40);
                g.setColor (Color.blue);
                g.fillRect (100, 100, 150, 150);
                g.setColor (Color.green);
                g.drawLine (20, 20, 300, 20);
                g.setColor (Color.red);
                g.fillOval (250, 250, 50, 50);
        }
}

I tried compiling it and opening the class file (don't laugh!), but that did nothing. I also tried running it like a normal java program, but obviously it didn't work. I have no idea how to execute this code. It's supposed t draw stuff in an applet. Thanks in advance!
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Sep 30, 2006 10:44 am   Post subject: (No subject)

You will either need to embed it in an HTML page, or open it using the appletviewer program distributed with the Java Development Kit.

While we're at it, let's adhere to a more standard formatting.

code:
import java.awt.*;
import java.applet.*;

public class ShapesColor extends Applet {
   public void paint(Graphics g) {
      setBackground(Color.darkGray);
      g.setColor(Color.pink);
      g.drawRect(50, 50, 40, 40);
      g.setColor(Color.blue);
      g.fillRect(100, 100, 150, 150);
      g.setColor(Color.green);
      g.drawLine(20, 20, 300, 20);
      g.setColor(Color.red);
      g.fillOval(250, 250, 50, 50);
   }
}
richcash




PostPosted: Sat Sep 30, 2006 11:51 am   Post subject: (No subject)

wtd wrote:
embed it in an HTML page

Could you tell me how to? I've tried to put it in every tag I know. Is there some special way to embed something?
wtd wrote:
or open it using the appletviewer program

The appletViewer in the bin? How do I open something with it (and do I open the .class file)?
wtd




PostPosted: Sat Sep 30, 2006 2:18 pm   Post subject: (No subject)

richcash wrote:
wtd wrote:
embed it in an HTML page

Could you tell me how to?


Better yet, Google "applet html".
richcash




PostPosted: Sat Sep 30, 2006 3:13 pm   Post subject: (No subject)

Oh, thanks for the help. The applet tag, how obvious! Laughing
[Gandalf]




PostPosted: Sat Sep 30, 2006 8:09 pm   Post subject: (No subject)

As for running something using appletviewer use:
Quote:
appletviewer program.class

Instead of:
Quote:
java program.class
richcash




PostPosted: Sat Sep 30, 2006 8:30 pm   Post subject: (No subject)

Oh, that's how you do it. Thanks.
It doesn't work for me, for some reason. When I execute it, nothing happens (there's not even an error message). Do I have to set new environment variables (or was it included with the java ones)?
McKenzie




PostPosted: Sat Sep 30, 2006 11:41 pm   Post subject: (No subject)

Your Java is fine. Post the HTML you are using.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Mon Oct 02, 2006 12:58 am   Post subject: (No subject)

Yeah, oops, it should be:
Quote:
appletviewer applet.html

Where applet.html has the applet embedded in it.
richcash




PostPosted: Mon Oct 02, 2006 2:59 pm   Post subject: (No subject)

Oooh! I never thought to try that, lol. Now it works, thanks everyone for the help!
richcash




PostPosted: Mon Oct 02, 2006 5:19 pm   Post subject: (No subject)

I should have asked this a long time ago, but I'm wondering why when you make an applet in Java like this you don't need the main method (and why the program doesn't start from the main method)? Isn't it still Java?
Sorry for the double post, but I didn't want to make a new topic.
[Gandalf]




PostPosted: Mon Oct 02, 2006 5:28 pm   Post subject: (No subject)

Yes it is, it only has a different entry point. I forget exactly how it goes, but google should have the answer. Something like this.
richcash




PostPosted: Mon Oct 02, 2006 7:15 pm   Post subject: (No subject)

Ok, I think I get it. Thanks!
richcash




PostPosted: Fri Oct 06, 2006 10:57 pm   Post subject: (No subject)

This also has to do with applets.
I got the following code from a website :

code:
import java.applet.*;
import java.awt.*;

public class DrawingLines extends Applet {

   int width, height;

   public void paint( Graphics g ) {
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) {
         g.drawLine( width, height, i * width / 10, 0 );
      }
   }
}


It doesn't seem to make a difference when I initialize the height and width variables, is this because those variables are set to values equal to the hieght and width of the applet in the html? If so, why (where's the assignment of the variables)?
Also, even when I change the value of height and width inside the for loop it doesn't make a difference (or is it just that I'm not noticing a difference)?
wtd




PostPosted: Fri Oct 06, 2006 11:16 pm   Post subject: (No subject)

Look for things like:

code:
<PARAM NAME=TEXT VALUE="Hi There">
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: