4 Replies - 1002 Views - Last Post: 03 January 2012 - 06:47 AM Rate Topic: -----

#1 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

extending the java.awt.color class

Posted 01 January 2012 - 09:01 PM

I'm supposed to be able to define a class called LabeledColor by extending the java.awt.color class and use this class with the line:

colorChooser.addItem(new LabeledColor(Color.RED, "Red"));



Here is my LabeledColor class:

import java.awt.*;

public class LabeledColor extends Color {

	private static final long serialVersionUID = 1L;

	String colorName;
	
	public LabeledColor(Color color, String name) {
		super(color);
		colorName = name;	
	}
	
	public String toString() {
		return colorName;
	}
}



And here is where I'm using it:

import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawStarMap extends GraphicsProgram {

	//Set up a window with a canvas to click on to add stars and the control bar to choose the size and color of the stars
	public void init() {
		setBackground(Color.GRAY);
		add(new JButton("Clear"), SOUTH);
		sizeSlider = new JSlider(MIN_SIZE, MAX_SIZE, INITIAL_SIZE);
		add(new JLabel("  Small"), SOUTH);
		add(sizeSlider, SOUTH);
		add(new JLabel("Large  "), SOUTH);
		initColorChooser();
		add(colorChooser, SOUTH);
		addMouseListeners();
		addActionListeners();
	}
	
	//add the options to choose a color
	private void initColorChooser() {
		colorChooser = new JComboBox();
		colorChooser.addItem(new LabeledColor(Color.WHITE, "White"));
		colorChooser.addItem(new LabeledColor(Color.RED, "Red"));
		colorChooser.setEditable(false);
		colorChooser.setSelectedItem(Color.WHITE);
	}
		
	// get the size of the star from the slider
	private double getCurrentSize() {
		return sizeSlider.getValue();
	}
	
	//when the mouse is clicked add a star to the canvas per chosen size and color
	public void mouseClicked(MouseEvent e) {
		GStar star = new GStar(getCurrentSize());
		star.setFilled(true);
		star.setColor((Color) colorChooser.getSelectedItem());
		add(star, e.getX(), e.getY());
	}
	
	//if the clear button is clicked, clear the canvas of any stars
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Clear")) {
			removeAll();
		}
	}
	
	private static final int MIN_SIZE = 1;
	private static final int MAX_SIZE = 50;
	private static final int INITIAL_SIZE = 16;
	
	private JSlider sizeSlider;
	private JComboBox colorChooser;
}



The problem I'm having is in passing the color to the LabeledColor constructor. According to the assignment I'm supposed to pass a color field. If I try to pass Color.WHITE (or any color), I get the error "The constructor Color(Color) is undefined." If I change the LabeledColor constructor to expecting an int like the parent constructor then I get the error, "The constructor LabeledColor(Color, String) is undefined." I can't win. How do I make both happy?

Thanks,
Mickey

Is This A Good Question/Topic? 0
  • +

Replies To: extending the java.awt.color class

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,135
  • Joined: 05-April 11

Re: extending the java.awt.color class

Posted 01 January 2012 - 09:14 PM

Color does not have a constructor that takes another Color.
So you cannot call this super constructor
super(color);


Was This Post Helpful? 0
  • +
  • -

#3 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: extending the java.awt.color class

Posted 01 January 2012 - 09:20 PM

Right. Then what do I do? According to the text I'm supposed to be able to call it using the color:

colorChooser.addItem(new LabeledColor(Color.RED, "Red"));


I found this - http://monet.ncsa.ui...abeledColor(int[], java.lang.String, boolean) - which is essentially the class I'm supposed to be creating. I'm thinking the solution lies somewhere in the method getColors() that they've defined but now sure how to go about defining or using it. Ideas?

Thanks,
Mickey
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: extending the java.awt.color class

Posted 02 January 2012 - 03:21 PM

You will have to call a constructor that exists something like

    public LabeledColor(Color color, String name) {
        super(color.getRGB());
        colorName = name;   
    }


or
    public LabeledColor(Color color, String name) {
        super(color.getRed(), color.getGreen(), color.getBlue());
        colorName = name;   
    }


Was This Post Helpful? 1
  • +
  • -

#5 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: extending the java.awt.color class

Posted 03 January 2012 - 06:47 AM

Aww geez. I knew it had to be something simple I was missing. I kept thinking just super(getRGB()) should work. Derr. Thanks so much for your help!

Mickey
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1