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

I\'m having trouble applying the java repaint method to my program. I need to ch

ID: 3654840 • Letter: I

Question

I'm having trouble applying the java repaint method to my program. I need to change the fill color of a Circle displayed according to the buttons on the screen. I think i have most the program done i just cant figure out how to apply the repaint method The original question is Write a program that draw a circle in a panel (define a panel class to display the circle). Implement three buttons that change the color of the circle. The buttons are named red, yellow and green. When click on the buttons, the fill color of the circle should change accordingly. here is my code so far import java.awt.*; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.Graphics; public class test3 extends JPanel { JRadioButton RED, YELLOW, GREEN; Color currentColor; public void paintComponent(Graphics g) { currentColor= Color.RED; g.setColor(currentColor); g.fillOval(50, 50, 100, 100); } public static void main(String[] args) { test3 frame = new test3(); frame.setSize(500,500); frame.setVisible(true); } public test3 (){ JPanel jpRadioButtons=new JPanel(); jpRadioButtons.setLayout(new GridLayout(1,1)); jpRadioButtons.add(RED=new JRadioButton("RED")); jpRadioButtons.add(GREEN=new JRadioButton("GREEN")); jpRadioButtons.add(YELLOW=new JRadioButton("YELLOW")); add(jpRadioButtons, BorderLayout.SOUTH); ButtonGroup group=new ButtonGroup(); group.add(RED); group.add(YELLOW); group.add(GREEN); GREEN.addActionListener(new ActionListener() { public void actionPerormed(ActionEvent e) { currentColor = Color.GREEN; repaint(); } }); } }

Explanation / Answer

import javax.swing.JFrame; public class TrafficLight { public static void main(String[] args){ JFrame frame = new JFrame("Traffic Light"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TrafficLightPanel panel = new TrafficLightPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true);

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