Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSC255 - Programming IlI Class Activity 4 (Ch. 13 Graphics and Java 2D) Total: 3

ID: 3700008 • Letter: C

Question

CSC255 - Programming IlI Class Activity 4 (Ch. 13 Graphics and Java 2D) Total: 30 pts Write a GUI application that allows the user by using JSlider class to gather the value of radius (radius is 15, by default) to draw a solid (Light Gray, at the default) color of circle as well as display all information about the circle with black color Need to include (2 pts) title bar: "Using slider to draw a solid circle (Fig 1) 10 pts) Change Color button at the top of the control Panel event handler for showing JColorChooser pre-defined dialog (Fig 2) if the user chooses the different color from the dialog, then the shape will automatically update that color if the user did not choose any color or press Cancel button, the color will be set back to Light Gray the textarea information's textcolor also be changed by that color (by default, the text color is black) o o o o (5 pts) Radius data: The radius value will be changed by the object of JSlider (the number between 0 to 255, set up the default value to 15, the mark label by 50) (10 pts) If the user changes the slider's radius value, the circle also be changed by that radius as well as the information at the text area (3 pts) Display the circle's area and circumference with two decimal places. Uaing aliderto draw-X Change Golor Radius Data: 50 100 150 200 250 Radius: 79 Diameter 158 ra:13605 58 Croumference: 496.37 Fig 1 lg

Explanation / Answer

Class HW2

import javax.swing.JFrame;

public class HW2 {
     
public static void main( String[] args ) throws Exception {


   TheWindow w= new TheWindow();
   w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   w.setSize(550,800);
   w.setVisible(true);
}
}

Class TheWindow

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TheWindow extends JFrame{

private JSlider mySlider;
private DrawOval myPanel;

public TheWindow() {
super("Use Slider to draw a solid circle");

myPanel = new DrawOval();

CircleInformation panelForInfo=new CircleInformation();

JTextArea area=new JTextArea(myPanel.getCircleInformation());
TitledBorder sliderBorder= BorderFactory.createTitledBorder("Circle Information");
panelForInfo.setBorder(sliderBorder);
area.setBounds(panelForInfo.getBounds());
area.setEditable(false);
panelForInfo.setLayout(new BorderLayout());
panelForInfo.add(area);


mySlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
mySlider.setMajorTickSpacing(50);
mySlider.setMinorTickSpacing(10);
mySlider.setPaintTicks(true);
mySlider.setPaintLabels(true);
mySlider.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
myPanel.setD(mySlider.getValue()*2);
area.setText(myPanel.getCircleInformation());
}
}
);
  
TitledBorder radius= BorderFactory.createTitledBorder("Radius");
mySlider.setBorder(radius);
JPanel btnPanel=new JPanel();

JButton btn1 = new JButton( "Choose Color");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color newColor = JColorChooser.showDialog(
       myPanel,
"Choose Oval Color",
myPanel.getBackground());
if(newColor != null){
   myPanel.setColor(newColor);
   area.setForeground(newColor);
}
else{
   myPanel.setColor(Color.LIGHT_GRAY);
   area.setForeground(Color.BLACK);
  
}
}
});
  
btnPanel.add(btn1);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(myPanel,this);
add(btnPanel,this);
add(mySlider,this);
add(panelForInfo,this);
  

}

}

Class DrawOval

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawOval extends JPanel{


private int d = 30;
private Color color=Color.LIGHT_GRAY;

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillOval( 0, 0, d, d);
}

public void setD(int newD) {
d = (newD >=0 ? newD : 10);
repaint();
}
public void setColor(Color c) {
color=c;
repaint();
}

public Dimension getPreferredSize() {
return new Dimension(510, 510);
}

public Dimension getMinimumSize() {
return getPreferredSize();
}

   public String getCircleInformation() {

       String radius="Radius="+(d/2)+" ";
       String dia="Diameter="+d+" ";
       String a= "Area="+Math.round(Math.PI*(d/2)*(d/2))/100.0+" ";
       String c= "Circumference="+Math.round(2*Math.PI*(d/2))/100.0+" ";
      
       return radius+dia+a+c;
   }
}

CIRCLEINFORMATION CLASS

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class CircleInformation extends JPanel {

private Color color;
  
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
  
}


   public void setColor(Color color) {
       this.color = color;
   }
}

Please upvote if you find the answer satisfactory :)