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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:31:42+00:00 2026-05-27T06:31:42+00:00

I am not sure why my code is not working but it is driving

  • 0

I am not sure why my code is not working but it is driving me crazy.

This is my class that should draw the button and creates the area to draw on:

The second set of code is the driver program I need to make 4 canvas’s that when I make two mouse clicks it draws a line between the two clicks and then repaints if I try and click on a canvas again.

I get the following errors:
error: invalid method declaration; return type required
addMouseListener(this);
^
error: illegal start of type
addMouseListener(this);
^

public class Scribble extends JPanel implements MouseListener
{
addMouseListener(this);
int x, x1, y, y1;
boolean flag = false;

protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawLine(x, y, x1, y1);
}


public void mouseReleased(MouseEvent e)
{

    if (!flag){
        x = e.getX();
        y = e.getY();
        flag = true;
    }
    else{
        x1 = e.getX();
        y1 = e.getY();
        flag = false;
        repaint();
}
}



public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}

}



public class Lab8Draw extends JFrame{

public Lab8Draw(){
    Color c = new Color(100, 10, 200);
    setTitle("Lab 8 - Application #1");
    setLayout(new GridLayout(2,2));
    Scribble s = new Scribble();
    s.setBackground(Color.WHITE);
    add(s);
    Scribble s1 = new Scribble();
    s1.setBackground(Color.RED);
    add(s1);
    Scribble s2 = new Scribble();
    s2.setBackground(c);
    add(s2);
    Scribble s3 = new Scribble();
    s.setBackground(Color.BLUE);
    add(s3);

}


    public static void main (String[] args){


        Lab8Draw frame = new Lab8Draw();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 800);
        frame.setVisible(true);
    }
}
  • 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-27T06:31:43+00:00Added an answer on May 27, 2026 at 6:31 am

    You will have to implement MouseListener

    public class Scribble extends JPanel implements MouseListener
    

    and register it

    Scribble s = new Scribble();
    s.addMouseListener(s);
    

    The Java tutorials provide a useful guide to writing a MouseListener.

    Alternatively, you could define an inner class that extends MouseInputAdapter as it already has empty implementations of the MouseListener methods.

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

Sidebar

Related Questions

I'm not sure why this code isn't working, so any help would be appreciated.
I'm not sure why my code is not working.. I have code in vba
I am working with old ASP code and I am not sure about semantics
I have the following Clojure code and I'm not sure why it's not working:
Not sure why my .plist looks like this when viwed as source code, and
Not sure how to do this, here is my code Dim oWeb As New
not sure why I find this so difficult. Here is the html code: <tr>
I am doing a procedure and it's working but I'm not sure if it'll
I have this code I been working on but I'm having a hard time
I'm not quite sure what's happening, but all signs point me in this direction..

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.