The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet Problems..

1 reply on 1 page. Most recent reply: Jun 10, 2005 1:41 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Andrew

Posts: 4
Nickname: barkera0
Registered: Jun, 2005

Applet Problems.. Posted: Jun 10, 2005 12:47 AM
Reply to this message Reply
Advertisement
Hi! I'm new to Java and I was just putting together a simple applet. Heres my code:

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet {
{

public abstract void appletResize(100,200);
}
public void paint(Graphics g) {
Color backgroundcolor = new Color(0,0,255);
g.setColor(backgroundcolor);
g.fillRect(0,0,100,200);

//Draws the text in black
g.setColor(Color.black);
g.drawString("Welcome to Java!!", 50, 60 );
}
}

My Applet is currently too big and I want it to be resized? I tied putting in this code:

{

public abstract void appletResize(100,200);
}

But it just brings up an error. How can I resize my applet sucessfully?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Applet Problems.. Posted: Jun 10, 2005 1:41 AM
Reply to this message Reply
"public abstract void" is the description of the method, meaning
public: everyone can call it
void: no return type
abstract: it is not implemented, there is no written code behind it.
You should have called it with
appletResize(100,200);

However, this line does not what you want.

try it this way:
import java.awt.*;
import java.applet.*;
 
public class HelloWorld extends Applet {
  public HelloWorld() {
    resize (100, 200); // is the same as: this.resize(100, 200);
  }
  public void paint(Graphics g) {
    Color backgroundcolor = new Color(0,0,255);
    g.setColor(backgroundcolor);
    g.fillRect(0,0,100,200);
    //Draws the text in black
    g.setColor(Color.black);
    g.drawString("Welcome to Java!!", 50, 60 );
  }
}


Some hints:
1. Instead of painting the rectangle everytime, you should set the backgroundcolor of the applet ONCE.
setBackground(backgroundcolor);
2. if you override the paint method, your first line should be super.paint(g);, because otherwise buttons, labels ... in thiscontainer would not be painted.
3. instead of overriding the paint method, override the paintComponent method, this prevents flickering. If you do so, you must of course call super.paintComponent(g);

Flat View: This topic has 1 reply on 1 page
Topic: Can't run Java Jackcess Previous Topic   Next Topic Topic: primitiveType[1] Confusing Java POS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use