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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:45:03+00:00 2026-05-22T22:45:03+00:00

I am trying to get the console to print when the mouse is pressed

  • 0

I am trying to get the console to print when the mouse is pressed within an object of class RenderCanvas which extends JPanel. However, I am getting no feedback when I press the mouse down in the window. Any suggestions as to what I might be able to change to make the MouseListener work?

Here is my code:

RenderCanvas Class:

import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;


public class RenderCanvas extends JPanel {
    private List<Rect> rectangles = new ArrayList<Rect>();
    private List<Line> lines = new ArrayList<Line>();

    public void renderCanvas() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                addRect(0, 0, 200, 200, Color.ORANGE);
                System.out.println(e);
            }
        });
    }

    public void paintComponent(Graphics g) {
        for (Rect rectangle : rectangles) {
            rectangle.draw(g);
        }
        for (Line line : lines) {
            line.draw(g);
        }
    }

    public void addRect(int x, int y, int width, int height, Color color) {
        Rect rectangle = new Rect(x, y, width, height, color);
        this.rectangles.add(rectangle);
    }

    public void addLine(int y, int width, Color color) {
        Line line = new Line(y, width, color);
        this.lines.add(line);
    }
}

Main Class:

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

public class Main {
    public static void main(String[] args) {
        JFrame window = new JFrame("Window");
        RenderCanvas canvas = new RenderCanvas();
        window.setContentPane(canvas);
        window.setSize(640, 360);
        window.setLocation(640, 360);
        window.setVisible(true);
    }
}

Thanks in advance for any help!

  • 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-22T22:45:04+00:00Added an answer on May 22, 2026 at 10:45 pm

    public void renderCanvas() is NOT a constructor. Change

    public void renderCanvas()
    

    to

    public RenderCanvas()
    

    Notice the upper-case “R” and the absence of the “void” return type

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

Sidebar

Related Questions

I'm trying to get console input in my Clojure program, but when it gives
I'm currently trying to get the output of an executable console-app into an other
I get this error when trying ruby script/console Rails requires RubyGems >= . Please
I'm trying to get php debug output to the console window in Eclipse but
I'm trying to print out an object called Contact (an extension of NSObject I
I'm trying to get input from user using array of structs and then print
I'm trying to get useful information from the exception object (passed to the a
I'm trying to get sockets working with Chrome 16 which is using the latest
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {

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.