|
I'm trying to create a simple program within an applet and I cant get it to do anything after I set my size. Here is the whole program, its very basic and not meant to be viewed in a browser.
import javax.swing.*;
import java.awt.*;
public class GettingGraphic extends JApplet
{
public void paint( Graphics fun )
{
setSize(1000,1000);
//Sets background color to orange
setBackground (Color.orange);
//Welcome Message with set font command
fun.setFont(new Font("Broadway", Font.BOLD, 36));
fun.drawString("Welcome to Strider\'s Getting Graphic",30,40);
}
}
I would also like to set a title for the applet, any suggestions?
Thanks
Strider
|
|
|
setSize should go in the init method.
Titles usually go with top–level containers. You can try the setWindowDecorationStyle method (j2se 1.4+).
// <applet code="GettingGraphic" width="400" height="400"></applet>
// use: >appletviewer GettingGraphic.java
import java.awt.*;
import javax.swing.*;
public class GettingGraphic extends JApplet
{
public void init()
{
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new GraphicPanel());
setSize(1000,600);
JRootPane rootPane = getRootPane();
System.out.println(rootPane.getWindowDecorationStyle()); // 0
// see JRootPane api for other choices of style
rootPane.setWindowDecorationStyle(JRootPane.FRAME); // 1
}
}
class GraphicPanel extends JPanel
{
Font font;
public GraphicPanel()
{
font = new Font("Broadway", Font.BOLD, 36);
setBackground(Color.orange);
}
protected void paintComponent( Graphics fun )
{
super.paintComponent(fun);
int w = getWidth();
int h = getHeight();
//Sets background color to orange
// don't need the call (above) to "super" if you do this:
//setBackground (Color.orange);
//fun.clearRect(0,0,w,h);
// ...or this:
//fun.setColor(Color.orange);
//fun.fillRect(0,0,w,h);
//Welcome Message with set font specified
fun.setFont(font);
fun.drawString("Welcome to Strider\'s Getting Graphic",30,40);
}
}
|
|
|
|
|
|
|
|
|
|