//	Scott DeRuiter
//  3/12/2013 - Greenstein: Change from applet to JFrame
//	March 12, 2013
//  BoxSwing.java
//  Creates a box and draws some words.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BoxSwing 
{
	JFrame frame;
	DisplayBox panel;
	
	public static void main (String[] args) 
	{
		BoxSwing bs = new BoxSwing();
		bs.Run();
	}

	public void Run() 
	{
		frame = new JFrame("BoxSwing");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Create JPanel and add to frame
		panel = new DisplayBox();
		frame.getContentPane().add(panel, BorderLayout.CENTER);	// add panel to frame
				
		frame.setSize(500, 500);		// explicitly set size in pixels
		frame.setVisible(true);		// set to false to make invisible
	}
}

class DisplayBox extends JPanel 
{
	DisplayBox() 
	{
		setBackground(Color.red);
	}

	public void paintComponent(Graphics g) 
	{
		super.paintComponent(g);
           	g.setColor(Color.green);
           	g.fillRect (10,130, 80, 140);
           	g.setColor(Color.black);
           	g.drawString("Java is Fun", 15 , 85);
	}
}

Back to Lesson 24 Examples

Back to Java Main Page