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

  • Home
  • SEARCH
  • 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 9255489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:43:45+00:00 2026-06-18T11:43:45+00:00

I am working on a homework assignment where a ball is moved around the

  • 0

I am working on a homework assignment where a ball is moved around the screen using left, right, up and down buttons. In addition, the ball’s color is changed using green and red buttons. For some reason, my listeners are giving me the error “Btns.RightListener cannot be resolved to a type”. I am not sure why.

Any help would be appreciated.

This is what I have so far…

Main Class:

import javax.swing.*;

@SuppressWarnings("serial")
public class Lab2Main extends JFrame {


        Lab2Main()
    {
        setTitle("Lab 2 - Move The Ball");
        Lab2 p = new Lab2();
        add(p);
    }

    public static void main (String[] args) { 

        Lab2 frame = new Lab2();
        frame.setTitle("Lab 2 - Move The Ball");
        frame.setSize(450, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }   

}

Class holding listeners:

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

@SuppressWarnings("serial")
public class Lab2 extends JFrame { 

    Btns canvas = new Btns();
    JPanel panel = new JPanel();

    JButton jbtL = new JButton("Left");
    JButton jbtR = new JButton("Right");
    JButton jbtU = new JButton("Up");
    JButton jbtD = new JButton("Down");
    JButton jbtRd = new JButton("Red");
    JButton jbtG = new JButton("Green");

    Lab2()
        {
        setLayout(new BorderLayout());

        panel.add(jbtR);
        panel.add(jbtL);
        panel.add(jbtU);
        panel.add(jbtD);
        panel.add(jbtRd);
        panel.add(jbtG);

        this.add(canvas, BorderLayout.CENTER);
        this.add(panel, BorderLayout.SOUTH);

        jbtL.addActionListener(new Btns.LeftListener(canvas));
        jbtR.addActionListener(new Btns.RightListener(canvas));
        jbtU.addActionListener(new Btns.UpListener(canvas));
        jbtD.addActionListener(new Btns.DownListener(canvas));
        jbtRd.addActionListener(new Btns.RdColorChangeListener(canvas));
        jbtG.addActionListener(new Btns.GColorChangeListener(canvas));
        }
}   

Class holding the buttons:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class  Btns extends JPanel
{
    int radius = 5;
    int x = -1;
    int y = -1;

        protected void paintComponent(Graphics g) {
            if (x < 0 || y < 0)
            {
                x = getWidth() / 2 -radius;
                y = getHeight() / 2 -radius;
            }
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillOval(5,10,25,25);
    } 
    public void moveLeft()
    { 

             x -= 5; 
             this.repaint(); 
        } 

        public void moveRight()
        { 

            x += 5; 
            this.repaint(); 
        } 
        public void moveUp()
        { 

            y -= 5; 
            this.repaint(); 
        } 

        public void moveDown()
        { 
            y += 5; 
            this.repaint(); 
        } 


        public class LeftListener implements ActionListener
        { 
            private Btns canvas; 

            LeftListener(Btns canvas) 
            { 
              this.canvas = canvas; 
            } 

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


        public class RightListener implements ActionListener
        { 
            private Btns canvas; 

            RightListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

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


         class UpListener implements ActionListener
         { 
            private Btns canvas; 

            UpListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

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



        class DownListener implements ActionListener
        { 
            private Btns canvas; 

            DownListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

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

        class RdColorChangeListener implements ActionListener 
            { 
            private Btns canvas; 

            RdColorChangeListener(Btns canvas) { 
                this.canvas = canvas; 
            } 
            public void actionPerformed(ActionEvent e){ 
                canvas.setColor(Color.RED); 
                repaint();
            } 

        class GColorChangeListener implements ActionListener 
            { 
            private Btns canvas; 

            GColorChangeListener(Btns canvas) { 
                this.canvas = canvas; 
            } 
            public void actionPerformed(ActionEvent e){ 
                canvas.setColor(Color.GREEN); 
                repaint();
            } 
       }
    }
  }


        public void setColor(Color red) {
        // TODO Auto-generated method stub

        }
}
  • 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-06-18T11:43:46+00:00Added an answer on June 18, 2026 at 11:43 am

    The class Btns.RightListener (and, in fact, all the inner classes of Btns) need to be declared as static classes in order to create new instances like you are doing. Just add the static modifier to each class declaration. Otherwise, you would need to do something like this:

    jbtL.addActionListener(canvas.new Btns.LeftListener(canvas));
    

    Oh, and as @antlersoft points out, you need to correct the class nesting.

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

Sidebar

Related Questions

I'm working on a homework assignment to modify code given by my professor using
I'm working on a homework assignment in Perl CGI using the CGI.pm module. In
Ok, so I am working on a homework assignment, and I am using SWING
I am working on a homework assignment that calculates GPA's and course averages using
I'm a novice working on a homework assignment introducing us to using files in
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
I'm working on a homework assignment right now, and I'm given this information: $s6
I am working on a homework assignment that deals with binary search trees and
I'm working on a homework assignment in which I'm required to use char arrays
I'm working on a homework assignment (a project), for which one criterion is that

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.