Java – how do you call the paint method in the Applet extension class?
I have two called paintme Java and starter Java class file
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 includes:
import java.applet.Applet; import java.awt.Graphics; public class Starter { public static void main(String[] args) { PaintMe ring = new PaintMe(); ring.paint(); } }
So the question is, how to get from starter Java calls the paint method to draw my string?
Solution
To compile, make changes
ring.paint();
.. To
ring.repaint();
note
>In this millennium, do not use AWT for coding Use swing (provide a applet). > Do not start the applet from main (string []) When JRE embeds a web page (or starts using JWs), the applet starts the applet You can design a GUI in a panel and then put it into a free floating application or applet This is called a hybrid However, the framework and applet add GUIs respectively, which are usually (usually) different classes. > Its existence is mainly useless Unless you add the applet to the container and make it visible, the code will run successfully, but will end without displaying anything in a few minutes
Update 1
Try this
resources
// <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",25); } }
Tips
> javac PaintMe.java > appletviewer PaintMe.java
screenshot
Update 2
I think this is a stupid request. It seems that starting JFrame by JWs (as mentioned and linked in the comments) is the best way to view this GUI Otoh, which is a (very) naive starter class implementation that will display the applet on the screen
It mixes AWT and swing (bad). It will not try to implement any type of applet context, nor will it call the applet init / start / stop / destroy methods, but it is enough to get the applet from another class
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); } }