// File: FrameWithCanvas.java
// Wissal Essah
// A GUI example program demonstartes how to:
// - Create a Frame object on screen
// - Using a Canvas for drawing
// - Add quit button to the frame
// - Add scroll bar to the frame
import java.awt.*;
public class FrameWithCanvas extends Frame
{
private Button quitButton;
private DrawingArea canvas;
private Scrollbar myScrollBar;
public FrameWithCanvas( String title, int width, int height )
{ // Constructor with parameters
super( title );
setSize( width, height );
canvas = new DrawingArea();
add(canvas,"Center");
quitButton=new Button("Quit Button");
add(quitButton,"East");
myScrollBar=new Scrollbar(Scrollbar.HORIZONTAL,0,100,0,width);
add(myScrollBar,"South");
}//end of Constructor
public static void main( String[] args )
{
Frame f = new FrameWithCanvas( "Frame with Canvas", 500, 400 );
f.setVisible( true );
}// end of main
} //end of main class
class DrawingArea extends Canvas
{
public void paint( Graphics g )
{
Dimension d = getSize();
g.setColor( Color.black );
g.drawRect( d.width/3, d.height/3, 100,150);
g.setColor(Color.red);
g.fillOval(d.width/3+50,d.height/3+50,20,20);
}//end of paint
}//end of Canvas class
|