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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:14:38+00:00 2026-06-15T11:14:38+00:00

Hello I have a class that draws a star in Java what works like

  • 0

Hello I have a class that draws a star in Java what works like a charm. After this I have extended the Star class to create another star with extendes possibilities ( in this case the color has to be different )

for some reason in my panel when I call the classes and give the parameters with the constructor only the child class color seems to work.

here is my code

    public class Star {

    protected int radius;
    protected int xmiddelpunt;
    protected int ymiddelpunt;
    protected static Color color;

    public Star(int radius, int x, int y, Color color) {
        xmiddelpunt = x;
        ymiddelpunt = y;
        this.radius = radius;

        this.color = color;
    }

}

and the extended class

    public class StarRed extends Star {

    protected int x, y;
    protected static Color color;

    Random red = new Random();

    public StarRed(int radius, int x, int y, Color color) {
        super(radius, x, y, color);

        this.radius = radius;
        this.x = x;
        this.y = y;
        this.color = color;
    }
}

the constructor of my panel class is as follows:

    ArrayList<Star> stars = new ArrayList<Star>();
ArrayList<StarRed> rs = new ArrayList<StarRed>();

public HeavenPanel() {

    setBackground(Color.blue); // geef het paneel een blauwe kleur


    this.addMouseWheelListener(this); // set de mouselistener


    for(int i = 0; i < 10; i++) {
        stars.add(new Star (r.nextInt(30 + 50), r.nextInt(10 + 701), r.nextInt(10 + 701), Color.yellow));
    }

    for(int k = 0; k < 10; k++) {
        rs.add(new StarRed(40, r.nextInt(30 + 50), r.nextInt(30 + 50), Color.red));
    }

}
  • 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-15T11:14:39+00:00Added an answer on June 15, 2026 at 11:14 am

    First problem:

    protected static Color color;
    

    That means that field (which you’ve got two of…) is shared across the whole type. I would have expected this to be an instance field, so different stars can be different colours. Instead, all stars are the same colour, unless you’ve got some code in StarRed which uses the color field, in which case you may have two colours of stars… but it’s still not right.

    Second problem: your StarRed class declares its own fields for x, y, and color, despite them also being declared in the superclass. You’re then setting the value of the superclass’s radius field despite that already having been set in the superclass constructor.

    Basically it’s all a bit confused at the moment. You should work out what information is associated with the type rather than any specific instance (in which case that should be a static field) and what information is associated with individual instances (in which case those should be instance fields). You should almost never use the same field name in a subclass and a superclass – and personally I’d suggest making all fields private (except possibly for constants).

    Finally, why would the StarRed constructor want to take a Color at all? Shouldn’t it always be red?

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

Sidebar

Related Questions

Hello I have like this 2 tables class User public int UserId{get;set;} { ....
I have a class that looks something like this: public class MyClass { string
I have a class that resembles this: However, after the initial constructor, the copy
Hello friends i have this class that is getting values from the database: import
Hello I have this code class Triplets { public: int nVal1; int nVal2; NodeT
say I have: class Test { public static int Hello = 5; } This
Hello all I have this problem that I can't seem to fix. I've been
So my problem is this. I have a class called Globals that contains all
Hello I have next css class that working in IE fine but not working
Hello I have a regular WinForm that calls this: private void childDirectoryToolStripMenuItem_Click(object sender, EventArgs

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.