Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:10:55+00:00 2026-05-29T23:10:55+00:00

I need to preface this with we are not allowed to use IDE’s in

  • 0

I need to preface this with we are not allowed to use IDE’s in class. I am obviously missing something important. I am writing a program that should allow a user to click a radio button then change the “light” to that color. I thought I connected the listeners correctly but obviously I am forgetting something. Can someone help me with this?

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Lab4Frame extends JFrame {
    //public boolean red, yellow, green;
    Lab4Frame(){
        this.setLayout(new BorderLayout());
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();

        add(p, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
    }

    public static void main(String[] args){

            Lab4Frame frame = new Lab4Frame();
            frame.setTitle("Lab4 Application # 1");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
    }

}

class Lab4RadioButtonPanel extends JPanel {
        Lab4Panel canvas = new Lab4Panel();
        //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel() {
        boolean red, green, yellow;
        this.setLayout(new FlowLayout());
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");

        this.setBorder(BorderFactory.createLineBorder(Color.black));

        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);


        this.add(jrbRed);
        this.add(jrbYellow);
        this.add(jrbGreen);

        jrbRed.addActionListener(new RedListener(canvas));
        jrbYellow.addActionListener(new YellowListener(canvas));
        jrbGreen.addActionListener(new GreenListener(canvas));
    }
}

class Lab4Panel extends JPanel{

    public Lab4Panel(){}

    boolean red, green, yellow;
    int radius = 5;
    int x = -1;
    int y = -1;

    public void setRed(){
        red = true;
        repaint();
    }

    public void setYellow(){
        yellow = true;
        repaint();
    }

    public void setGreen(){
        green = true;
        repaint();
    }

    protected void paintComponent(Graphics g){
        if (x<0 || y<0) {
            x = getWidth() / 2 - radius;
            y = getHeight() / 2 - radius;
        }
        super.paintComponent(g);
        g.drawRect(x - 10,y - 90, 40, 120);
        g.drawRect(x - 5,y - 90, 40, 120);
        g.setColor(Color.RED);
        g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        g.setColor(Color.YELLOW);
        g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        g.setColor(Color.GREEN);
        g.drawOval(x,y, 4 * radius, 4 * radius);


        if(red){
            g.setColor(Color.RED);
            g.fillOval(x,y - 80, 4 * radius, 4 * radius);
            repaint();
        }

        else if (yellow){
            g.setColor(Color.YELLOW);
            g.fillOval(x,y - 40, 4 * radius, 4 * radius);
            repaint();
        }

        if(green){
            g.setColor(Color.GREEN);
            g.fillOval(x,y, 4 * radius, 4 * radius);
            repaint();
        }

    }


}

class RedListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    RedListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setRed();
    }
}

class YellowListener implements ActionListener{
    private Lab4Panel canvas;

    YellowListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setYellow();
    }
}

class GreenListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    GreenListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setGreen();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:10:58+00:00Added an answer on May 29, 2026 at 11:10 pm

    Change the code below:

    class Lab4RadioButtonPanel extends JPanel {
        Lab4Panel canvas = new Lab4Panel();
        //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
    
        public Lab4RadioButtonPanel() {
    

    to

    class Lab4RadioButtonPanel extends JPanel {
        Lab4Panel canvas;
        //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
    
        public Lab4RadioButtonPanel(Lab4Panel canvas) {
            this.canvas = canvas;
    

    and change the code below:

    Lab4Panel p = new Lab4Panel();
    Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
    

    to:

    Lab4Panel p = new Lab4Panel();
    Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p);
    

    I get the result like figure below, I think you’ll get what’s wrong with your program:
    enter image description here

    The bug is clear: the panel you display is constructed in main function, but the panel you change according to clicking a radio button is constructed in class Lab4RadioButtonPanel, they don’t point to the same object, this is the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to preface this with I am not allowed to use an IDE
I need to preface this with I am not allowed to use an IDE
I need to preface this with I am not allowed to use an IDE
I need to preface this with I am not allowed to use an IDE
I need to preface this with my instructor doesn't let us use IDE's. We
Preface: I'm not sure what resources are. I need this form (which is working
Let me preface this by saying this is not an actual situation of mine
Preface: This is the first real swing program I have done. I have a
I'd like to preface this by stating that this is for a class, so
Preface: This has become a quite a long post. While I'm not new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.