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 6102195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:35:11+00:00 2026-05-23T13:35:11+00:00

My aim is to make a paint type application. Thank you for your suggestions..

  • 0

My aim is to make a paint type application.

Thank you for your suggestions..
but there is still error in the code.

In netbeans IDE error : ‘method addActionListener in class cannot be applied to given types’ occurs in the line button1.addActionListener(panel);

so i used it suggestion to cast it to ActionListener. so the line is button1.addActionListener((ActionListener) panel);
but still in the below ‘if statement‘ error occurs : cannot find symbol variable ‘button1’

Thanks again for your help.
here is changed code. i changed the variable names and used adapter class to make code simpler.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class r extends JPanel
{

public int x1,x2,y1,y2;

public static double SWITCH;

public r()

{

    setBackground(Color.WHITE);

    addMouseListener(new MouseAdapter()

    {

         public void mousePressed(MouseEvent m)

         {

         x1=m.getX();

         y1=m.getY();

         repaint();

         }

         public void mouseReleased(MouseEvent m)

         {

         x1=x2=y1=y2=0;

         repaint();

         }

    });

    addMouseMotionListener(new MouseMotionAdapter()

    {

        public void mouseDragged(MouseEvent m)

        {

           x2=m.getX();

           y2=m.getY();

           repaint();

        }

    });

  }

  public void paintComponent(Graphics g)

  {

    super.paintComponent(g);

    if(SWITCH == 2)

    {

        g.drawRect(x1, y1, x2, y2);

    }

    else if (SWITCH == 3)

    {

        g.drawOval(x1,y1,x2,y2);

    }

    else

    {

        g.drawString("qwe", x1, y1);

    }

 }

 }

 public class q extends JFrame implements ActionListener
 {
 public static void main(String[] args)

 {

    q window = new q();

    window.setVisible(true);

    window.setSize(1024, 800);

    window.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container cont = window.getContentPane();

    cont.setLayout(new GridLayout(2,2));

    r panel = new r();

    JPanel BPanel = new JPanel();

    cont.add(panel);

    cont.add(BPanel);

    BPanel.setBackground(Color.blue);

    JButton button1,button2;

    button1 = new JButton("Rect");

    button2 = new JButton("Oval");

    BPanel.add(button1);

    BPanel.add(button2);

    button1.addActionListener((ActionListener) panel);

    button2.addActionListener((ActionListener) panel);

}

public void actionPerformed(ActionEvent a)

{

    Object obj;

    obj=a.getSource();

    if (obj== button1)

    {

        SWITCH = 2;

        repaint();

    }

    else

    {

       SWITCH = 3;

       repaint();

    }

}

}
  • 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-23T13:35:12+00:00Added an answer on May 23, 2026 at 1:35 pm

    I think your basic Java concepts are lacking.

    There is no “this” reference in any static context, i.e. static void main.

    Replace “this” with your instance of “r”, “z3”.

    b1.addActionListener(z3);
    b2.addActionListener(z3);
    

    Other than that, your UI code is really all over the place, but that’s a discussion for another question.

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

Sidebar

Related Questions

I am a very, very mediocre programmer, but I still aim to use the
I try to make my evoque templates color-code a bit, but the html I
We have developed an application in c# .net and aim to make it deployable
Im trying to make some text non-copyable, my aim isn't to stop people from
I have following script which is aim to make a war file. def ant
Good day everybody, I want to make an if conditional with the following aim:
I aim to make an app that would store sms messages with certain words
so my aim is to make a IF statement determine what the value of
Aim: To have the if statement execute a onclick event through code. I currently
I want to make an application in Qt where the tabs in the QTabBar

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.