Solved: Write a Java program that constructs an Applet

$30.00

Category:

Description

Solved: Write a Java program that constructs an Applet

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain JRadioButtons which a user can use to select a color for a circle (foreground color) and a color for its background, and also to specify whether the circle is filled or unfilled. Radio buttons are located at the top side of the applet.

(The size of the applet here is approximately 500 X 400).

The circle must be unfilled initially, and its initial color is red. The initial background color should be cyan. The initial diameter of the circle must be 100 pixels, and the coordinate of the upper left corner of the bounding rectangle of the circle must be (200, 100), i.e., x-coodinate is 200 and y-cooridnate is 100.
A user can choose a color of the circle using radio buttons and also choose its background color. The color choices for the circle are red, green, and blue. The color choices for the background are cyan, yellow, and pink.

A user can choose one of radio buttons for Unfilled or Filled. If a user chooses Filled Radio Button, the circle will be filled with the chosen color.

A user can choose the color of the circle and its background.

A user can also use arrow keys in the keyboard to move the location of the circle. Every time a user presses the right arrow key in the keyboard, the circle should move towards right by 5 pixels

Similarly, pressing the left arrow key moves the circle to left by 5 pixels every time, pressing the up arrow key moves the circle to up by 5 pixels every time, and pressing the down arrow key moves the circle to down by 5 pixels every time.

Now a user can also press the character B key in the keyboard to make the diameter of the circle larger by 6 pixels every time, and also press the character S key in the keyboard to make the diameter of the circle smaller by 6 pixels every time.
Important: the diameter of the circle should not become smaller than 10 pixels even if a user keeps pressing the character S key.

Click here to view the applet

Class description

CanvasPanel class

The CanvasPanel class extends JPanel defined in javax.swing package. This is where a circle is drawn. The default color of the circle is red and the default background color of this panel is cyan. It must contain the following method.

public void paintComponent(Graphics page)

Using the parameter, the Graphics object, this method will draw a circle with its selected color. The initial color of the circle must be red and the initial background color must be cyan. The initial diameter of the circle must be 100 pixels, and the coordinate of the upper left corner of the bounding rectangle of the circle must be (200, 100), i.e., x-coodinate is 200 and y-cooridnate is 100. Initially the circle should be unfilled.
Remember that this method need to call paintComponent method defined in its parent class. This class can be defined as nested class within the WholePanel class.

WholePanel class

The WholePanel class organizes all components in the applet. It extends JPanel defined in javax.swing package. It should contain at least the following instance variable:

Attribute name Attribute type Description
canvas CanvasPanel A panel where a circle is drawn with its selected color.

This class should have a constructor:

public WholePanel()

This is where all components are arranged. Add as many instance variable as you would like to this class, and initialize them in this constructor. One way to define this class is to contain an object of CanvasPanel, and objects of JRadioButton to choose colors.

FillListener class

The FillListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, depending on whether to fill or unfill the circle is chosen through their JRadioButtons, it needs to control the paintComponent method how the circle is drawn. One suggestion is to set up some boolean variable and set it to a certain value in this method so that it can be used in the paintComponent method. This listener is used together with JRadioButtons.

ColorListener class

The ColorListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, the color selected by using the JRadioButtons is assigned as a color to be used to draw the circle, or for its background color. This listener is used together with JRadioButtons.

DirectionListener class

The DirectionListener class implements KeyListener interface defined in java.awt.event package. It must implement the following method:

public void keyPressed(KeyEvent event)

In this method, if the up arrow key in the keyboard is pressed, then the circle should move upwards by 5 pixels. If the down arrow key is pressed, then the circle should move downwards by 5 pixels. If the left arrow key is pressed, then the circle should move towards left by 5 pixels. If the right arrow key is pressed, then the circle should move towards right by 5 pixels. When the circle moves, its shape should not change If the B key is pressed, then the diameter of the circle should become larger by 6 pixels. If the S key is pressed, then the diameter of the circle should become smaller by 6 pixels , but the diameter should not get smaller than 10 pixels.

PointListener class

The PointListener class implements MouseListener interface and MouseMotionListener interface. It must implement the following method:

Other methods from MouseListener can be left as blank.

Note that these four listener classes and CanvasPanel class can be defined as nested classes inside of the WholePanel class.

How to get started:

Download the following files and use them as a base of your program:

Assignment7.java
WholePanel.java

Step 1: Complete the constructor or WholePanel and also CanvasPanel by following their descriptions.
Step 2: Create FillListener class and ColorListener class that implements ActionListener by following their description.
Step 3: Create DirectionListener class that implements KeyListener so that the circle can move using up, down, left, or right arrow key, and also get larger or smaller using B or S key.
More steps: These are just skeletons. You need to adjust your code to make it work cleanly.

Assignment7.java (NOT TO BE CAHNGED)

// Assignment #: 7

//         Name: Your name

//    StudentID: Your id

//      Lecture: Your lecture

//  Description: it needs to be filled

 

import javax.swing.*;

 

public class Assignment7 extends JApplet

{

 

public void init()

{

// create a WholePanel object and add it to the applet

WholePanel wholePanel = new WholePanel();

getContentPane().add(wholePanel);

 

//set applet size to 500 X 400

setSize (500, 400);

}

 

}

WholePanel.java (NEEDS TO BE CHANGED)

// Assignment #: 7

//         Name: your name

//    StudentID: your id

//      Lecture: your lecture days/time

//  Arizona State University CSE205

//  Description: It needs to be filled.

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;  // to use listener interfaces

 

public class WholePanel extends JPanel

{

private Color foregroundColor, backgroundColor;

private int currentDiameter, x1, y1;

private CanvasPanel canvas;

private JPanel buttonPanel;

 

private JRadioButton filledRadio, unfilledRadio;

 

public WholePanel()

{

backgroundColor = Color.CYAN;

foregroundColor = Color.RED;

 

currentDiameter = 100;

x1 = 200; y1 = 100;

 

unfilledRadio = new JRadioButton(“Unfilled”, true);

buttonPanel = new JPanel();

buttonPanel.add(unfilledRadio);

 

//TO BE COMPLETED

 

 

canvas = new CanvasPanel();

 

JSplitPane sPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, buttonPanel, canvas);

 

setLayout(new BorderLayout());

add(sPane, BorderLayout.CENTER);

}

 

//Its description should be completed

private class ColorListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

//needs to be filled

}

} // end of ColorListener

 

 

//Its description should be completed

private class FillListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

//needs to be filled

}

}

 

//CanvasPanel is the panel where a circle is drawn

private class CanvasPanel extends JPanel

{

//Constructor to initialize the canvas panel

public CanvasPanel( )

{

// make this canvas panel listen to keys

addKeyListener (new DirectionListener());

// make this canvas panel listen to mouse

addMouseListener(new PointListener());

 

setBackground(backgroundColor);

 

//This method needs to be called for this panel to listen to keys

//When panel listens to other things, and go back to listen

//to keys, this method needs to be called again.

requestFocus();

}

 

 

//this method draws all characters pressed by a user so far

public void paintComponent(Graphics page)

{

super.paintComponent(page);

setBackground(backgroundColor);

 

//to be filled

}

 

/** This method is overriden to enable keyboard focus */

public boolean isFocusable()

{

return true;

}

 

// listener class to listen to keyboard keys

private class DirectionListener implements KeyListener

{

public void keyReleased(KeyEvent e) {}

 

public void keyTyped(KeyEvent e) {}

 

// in case that a key is pressed, the following will be executed.

public void keyPressed(KeyEvent e)

{

//TO BE COMPLETED

}

} // end of DirectionListener

 

 

// listener class that listens to the mouse

// This class is already completed. No adjustment is needed.

public class PointListener implements MouseListener

{

//in case that a user presses using a mouse,

//it gains the focus of the keyboard keys

public void mousePressed (MouseEvent event)

{

canvas.requestFocus();

}

 

public void mouseClicked (MouseEvent event) {}

public void mouseReleased (MouseEvent event) {}

public void mouseEntered (MouseEvent event) {}

public void mouseExited (MouseEvent event) {}

 

} // end of PointListener

 

} // end of Canvas Panel Class

 

} // end of Whole Panel Class


error: Content is protected !!