GUI in Java Photo Album Create a Photo

GUI in Java

Photo. Album • Create a Photo. Album application that displays a new picture each time Next is clicked. Use the grayangel. jpg, scorpionfish. jpg, sponges. jpg, and starfish. jpg files supplied as data files for this text. • The application should allow the user to continously cycle through the images.

Adding images • Labels and buttons are often used to display an image, but many Swing components support images. • Images that are GIF and JPG format work best. • Unless a different path is specified in the application code, the image files must be in the same location as the compiled code

Using JLabel to add images • JLabel(Image. Icon pic) creates a JLabel object containing pic. • set. Icon(Image. Icon pic) sets the JLabel to contain pic. • example JLabel label= new JLabel(new Image. Icon("dice. jpg"));

Using JButton to add images • JButton(String str, Image. Icon pic) creates a JButton object containing the text str and the image pic. • JButton(Image. Icon pic) creates a JButton object containing the image pic. • set. Icon(Image. Icon pic) sets the JButton to contain pic.

Dice roll application • The Roll application displays a die face. The user can click Roll Die to “roll the die” and display the outcome of the roll. • Need to use Random class to get the next number when click on button. • Change the picture. label. set. Icon (new Image. Icon("dice 5. jpg"));

Tic. Tac. Toe GUI • Create a Tic. Tac. Toe. GUI application that allows two players to play a computerized tic-tac-toe game. The TTT class code from last time will need to be modified for the GUI version of the application. Use a button for each “box” in the tic-tac-toe board
- Slides: 7