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

Help me with this :( \" Using eclipse \" Write code that uses a JList to hold th

ID: 3818072 • Letter: H

Question

Help me with this :(

" Using eclipse "

Write code that uses a JList to hold three shape names – Rectangle, Square, and Circle, as well as an open area to the left of the JList. When a user clicks on the GUI to the left of the JList, the code should draw a shape of the chosen type (from the list) in that position. The dimensions used should be random on each click, between 50 and 300 pixels wide. When a new shape is drawn, the old one should disappear. Below is an example of the GUI, but looking close enough is good enough:

Simple Drawing GUI Rectangle Square ircle

Explanation / Answer

bundle com.zetcode;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Surface broadens JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new Color(150, 150, 150));
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.fillRect(30, 20, 50, 50);
g2d.fillRect(120, 20, 90, 60);
g2d.fillRoundRect(250, 20, 70, 60, 25, 25);
g2d.fill(new Ellipse2D.Double(10, 100, 80, 100));
g2d.fillArc(120, 130, 110, 100, 5, 150);
g2d.fillOval(270, 130, 50, 50);
}
@Override
open void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
open class BasicShapesEx broadens JFrame {
open BasicShapesEx() {
initUI();
}
private void initUI() {
add(new Surface());
setTitle("Basic shapes");
setSize(350, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
open static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
open void run() {
BasicShapesEx ex = new BasicShapesEx();
ex.setVisible(true);
}
});
}
}