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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:07:25+00:00 2026-06-15T22:07:25+00:00

Hello I am creating a application ( exercise ) in Java where I have

  • 0

Hello I am creating a application ( exercise ) in Java where I have to change the color of a variable in my drawing class. when the application starts and I run a sysout on the color variable it says null but when I press my right mouse button for example it changes color in the controller class but not in my drawing class.. Can someone take a look and tell me what i am doing wrong?

here is a piece of code

this is the relevant part of the drawing class

    private Color color;
private ArrayList<Point> p = new ArrayList<Point>();

public Drawing(Color color) {
    this.color = color;
    System.out.println("color  " + color);
}

public void draw(Graphics g) {
    for(int i = 0; i < p.size(); i++) {
        g.setColor(color);
        g.fillRect(p.get(i).x, p.get(i).y, 10, 10);
    }
}

and this is the relevant code of my controller.

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}

I hope someone can figure out what i’m doing wrong..

  • 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-15T22:07:26+00:00Added an answer on June 15, 2026 at 10:07 pm

    Add a setter method to your class Drawing and pass the actual color after it has been calculated on a right-mouse-button click:

    public void setColor(Color color) {
       this.color = color;
    }
    

    And in the controller:

    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
        draw.setColor(color);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have been tasked with creating a fairly complex web application in php,
Hello guys I'm creating an application that have a widget the application and the
Hello all i have created a empty application instead of creating single page application
I am creating Sample Spring MVC application. In my Controller class I have define
Hello I am creating a Windows application (WPF) that is going to be running
Hello I'm creating a game and I have a few ways to make trainers
Does anyone have a hello world sample or tutorial for creating an Eclipse plugin
Hello I'm creating an os x application for which I try to add a
i'm creating a very simple (hello World quality) web application using spring mvc 3.0.
Hello I am creating a windows application that will be installed in 10 computers

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.