Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Java program) DESCRIPTION Write a GUI application that displays a variety of shapes and pictures. The application should provide three buttons labeled, Happy, Sad and

(Java program)

DESCRIPTION

Write a GUI application that displays a variety of shapes and pictures.

The application should provide three buttons labeled, Happy, Sad and Picture for selecting a shape or a picture to be displayed on a panel.

If the user clicks the Happy, a happy face will show and if the user clicks a Sad button, a sad face will show. If the user clicks the Picture button, a picture selected randomly amongst several (from 4 to 6) pictures will show.

IMPLEMENTATION

Use a regular JPanel for containing three buttons and an extended JPanel for doing the drawings.

Add the extended JPanel to the larger upper part (center) of the content pane and add the regular JPanel containing the three JButton objects to the smaller lower part (south) of the content pane.

Extended JPanel Implementation

Create a class (say JPanelExt) that extends JPanel.

In the extended JPanel, provide the following two methods:

paintComponent Method

public void paintComponent (Graphics g)

In the extended JPanel, provide the paintComponent ( ) method that over rides the same method in the parent class.

From within this method, call the parent paintComponent ( ) method. This will clear the drawing surface and will also enable the double buffering which will result in creating a smoother and jitter free drawing.

After the call to the parent paintComponent ( ) method, include the code for performing the drawing as shown below.

public void paintComponent (Graphics g)

{ //call the parent method

super.paintComponent (g);

//include the code that does the drawing here.

}

setPictureType Method

public void setPictureType (String pictureType)

In the extended JPanel, provide a method setPictureType ( ) to set the picture type for the drawing.

Whenever the user clicks one of the buttons for drawing a picture, the button event handler calls this method of the JPanelExt object and passes it the pictureType. It may use one of the following values for a pictureType: Happy, Sad, Picture.

This method receives the picture type as a parameter and saves it in an instance variable. Then it calls the method repaint ( ). A call to repaint ( ) results in the method paintCompoent ( ) being called eventually. The paintComponent ( ) method examines the instance variable containing the pictureType and draws the appropriate picture.

DRAWING A SHAP

For doing the drawing, use any of the number of Graphics method available such as drawOval( ), drawArc ( ) etc.

Drawing A Happy Face

The code below draws a happy face in a rectangle with starting co-ordinates (0,0), width 100 and height 100. You may rewrite the code as needed.

g.drawOval(0,0,100,100);

g.fillOval(25,25,10,10);

g.fillOval(65,25,10,10);

g.drawArc(25,25,50,50,0,-180);

or the following:

g.setColor(Color.yellow); g.fillOval(0,0,300,300); g.setColor(Color.RED); g.fillOval(80,75,30,30); g.fillOval(190,75,30,30); g.setColor(Color.black); g.fillArc (75,100,150,150,0,-180

Drawing A Sad Face

The code below draws a sad face in a rectangle with starting co-ordinates (0,0), width 100 and height 100. You may rewrite the code as needed.

g.drawOval(0,0,100,100);

g.fillOval(25,25,10,10);

g.fillOval(65,25,10,10);

g.drawArc(25,50,50,50,0,180);

or the following:

g.setColor(Color.GREEN); g.fillOval(0,0,300,300); g.setColor(Color.black ); g.fillOval(80,75,30,30); g.fillOval(190,75,30,30); g.setColor(Color.black); g.drawArc(75,150,150,150,0,180);

Drawing A gif file

Use the following code for drawing a file called myfile.gif.

Also see the notes below:

//import java.net.*;

//create an ImageIcon for the file called myfile.gif

//See note 1 below for finding a gif file.

//See note 2 below for learning where to copy the gif file.

//See note 3 below for another way of creating ImageIcon

URL url = getClass( ).getResource ("myfile.gif");

ImageIcon imageIcon = new ImageIcon (url);

//create an Image object for the ImageIcon object

Image image = imageIcon.getImage( );

//Draw the image to span all the drawing surface.

//g is the graphics object passed to method paintComponent.

//See note 4 below for drawing an image with a fixed built in size.

g.drawImage (image,0,0,this.getWidth( ), this.getHeight ( ) , this);

image text in transcribed

Happy S Sad Picture

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago