import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JPanel;
/**
* Draws "Hello World" directly onto this extended JPanel
*
* @author Roedy Green
* @version 1.0
*/
class Draw extends JPanel
{
public Draw()
{
this.setOpaque( true );
}
/**
* does drawing
*
* @param g where to paint
*/
public void paintComponent ( Graphics g )
{
super.paintComponent();
Rectangle r = g.getClipBounds();
int x = 4;
int y = 16;
g.setColor( this.getForeground() );
String text = "Hello World";
g.drawString( text, x, y );
x += g.getFontMetrics().stringWidth( text );
g.setColor( Color.red );
g.fillRect ( x, y, 10 , 10 );
x += 20;
}
/**
* get size to render the panel
*
* @return Dimensions needed for full rendering.
*/
public Dimension getPreferredSize()
{
return new Dimension( 200, 30 );
}
}