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

Write a GUI application that allows the user by using JSlider class to gather th

ID: 3701781 • Letter: W

Question

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 o event handler for showing JColorChooser pre-defined dialog (Fig 2) o 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 . (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. Using slider to drew-- × Change Color Radius Data: 50 100 150 200 250 Circle Information Radus: 79 ameter 158 Area 1980868 Circumterence: 498 37 Fig

Explanation / Answer

Answer :

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;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote