CH 02 Introduction GUI
CH 02 Introduction GUI
10636212
Frame
• A Frame is a top-level window with a title and a
border. Object
import javax.swing.JFrame;
Ex2: Shapes
import java.awt.Graphics; //handle the display
import javax.swing.JPanel;
public class Shapes extends JPanel
{
private int choice; // user's choice of which shape to draw
// constructor sets the user's choice
public Shapes(int userChoice)
{
choice = userChoice;
}
// draws a cascade of shapes starting from the top-left corner
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int i = 0; i < 10; i++)
{ // pick the shape based on the user's choice
switch (choice)
{
case 1: // draw rectangles
g.drawRect(10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10);
break;
case 2: // draw ovals
g.drawOval(10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10);
break;
}
}
}
} // end class Shapes
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setVisible(true);
}
} // end class ShapesTest
© 2020 Dr. Ashraf Armoush , An-Najah National University 11
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(230, 250);
application.setVisible(true);
}
} // end class DrawSmileyTest