3

I have 2 class files called PaintMe.java and Starter.java. PaintMe.java contains:

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

public class PaintMe extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawString("HELLOOO", 15, 25);
    }
}

Starter.java contains:

import java.applet.Applet;
import java.awt.Graphics;


public class Starter {
    public static void main(String[] args) {
        PaintMe ring = new PaintMe();
        ring.paint();
    }
}

So question is, how can I paint my string with calling a paint method from Starter.java?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You shouldn't be calling the `paint(Graphics g)` method yourself. It is called by the Event Dispatch Thread. If you remove `ring.paint();` from your code, things work as expected. – Zéychin Dec 29 '11 at 09:33

4 Answers4

3

To get it to compile, change

ring.paint();

..to..

ring.repaint();

Notes

  1. Don't code using AWT in this millennium. Use Swing (which offers a JApplet).
  2. Don't start an applet from the main(String[]). An applet is started by the JRE when it is embedded in a web page (or launched using JWS). A GUI can be designed in a panel, that is then put in a free-floating application or applet. That is known as a hybrid. But both frame and applet separately add the GUI, which is (most often) a different class to either.
  3. The main as it exists is useless. Unless the applet is added to a container and made visible, the code will run successfully but end in a few moments without displaying anything.

Update 1

..tried that, but it still doesn't draw my string in the applet window.

Try this.

Source

// <applet code='PaintMe' width=300 height=50></applet>
import java.applet.Applet;
import java.awt.*;

public class PaintMe extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawString("HELLOOO", 15, 25);
    }
}

Prompt

> javac PaintMe.java
> appletviewer PaintMe.java

Screenshot

PaintMe


Update 2

..I need to have it started from Starter.java class.

I think that is a silly requirement, and it seems like JWS (as mentioned & linked in comments) launching a JFrame is the best way to view this GUI. OTOH, here is a (very) naive implementation of the Starter class that will show that applet on-screen.

It mixes AWT and Swing (bad), it does not attempt to implement any sort of applet context, and does not call the applet init/start/stop/destroy methods, but is enough to get the applet on-screen from another class.

Starter

import java.awt.Dimension;
import javax.swing.JOptionPane;

public class Starter {
    public static void main(String[] args) {
        PaintMe ring = new PaintMe();
        ring.setPreferredSize(new Dimension(250,30));
        JOptionPane.showMessageDialog(null, ring);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • tried that, but it still doesn't draw my string in the applet window. –  Dec 29 '11 at 09:42
  • compiling and running PrintMe.java works fine, but I need to have it started from Starter.java class. –  Dec 29 '11 at 09:55
  • 1
    Then forget the applet, use a frame. Note that a frame can be launched from a link on a web page using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). BTW an up-vote for people who have offered answers is usually a good idea. – Andrew Thompson Dec 29 '11 at 10:08
  • Thank you, I get it now. Yes I've noticed the up-vote :) I cant vote up since I don't have reputation of 15. –  Dec 29 '11 at 10:32
2

See Component.paint(Graphics).

You shouldn't call the paint method yourself. paint is a method that AWT will call when needed, and it will provide the Graphics object to the method. To display your applet, you need to put it inside a Frame, and make the Frame visible. AWT will call the paint method each time it needs to.

Note that an Applet is normally used to embed some Java... applet inside an HTML page. If all you need is a standalone Java application, you should use a Canvas, or even better, not use AWT at all and use Swing instead. Google for "swing tutorial".

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I need to display it in Applet and im intending to use it on my test home page. –  Dec 29 '11 at 09:41
  • Then you don't need a main method. The Starter class isn't needed, unless you want to test your applet ouside of the browser. But there's appletviewser for that. Read the tutorial on applets: http://docs.oracle.com/javase/tutorial/deployment/applet/ – JB Nizet Dec 29 '11 at 09:46
1

You can change it by adding constructor to PaintMe method and calling repaint from there.

mevada.yogesh
  • 1,118
  • 3
  • 12
  • 35
1

To draw your painting from Starter.java you need to do it by frame. First create a frame in main, then add the object of PaintMe class to that frame. Sample code below:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class PaintMe extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawString("HELLOOO", 15, 25);
    }
} 

public class Starter {
    public static void main(String[] args) {
        Frame f = new Frame("Painting");
        f.setSize(200,200);
        PaintMe paintMe = new PaintMe();
        f.add(paintMe);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter()
          {
           public void windowClosing(WindowEvent we)
           {  System.exit(0); }
          }
         );
    }
}
ılǝ
  • 3,440
  • 2
  • 33
  • 47
Shasha
  • 11
  • 1