wood burning stoves 2.0*
The moose likes Java in General and the fly likes How do I change color on an Icon? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I change color on an Icon?" Watch "How do I change color on an Icon?" New topic
Author

How do I change color on an Icon?

kotoisin
Greenhorn

Joined: Nov 08, 2004
Posts: 5
I am to change color of an Icon, with 3 different buttons.
So far I have this, but the ColorIcon will not compile.
Compiler says: cannot resolve class ColorIcon.


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;


public class ColorIcon implements Icon
{
private int width;
private Color color;
private ChangeColor c;


public ColorIcon(int aWidth, ChangeColor c)
{
this.c = c;
width = aWidth;
color = Color.RED;
}

public int getIconWidth()
{
return width;
}

public int getIconHeight()
{
return width / 2;
}

public void setColor(Color c)
{
color = c;
}

public void paintIcon(Component c, Graphics g, int x, int y)
{

color = c.color;
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double ellipse = new Ellipse2D.Double(x,y, width, width);
g2.setColor( color );
g2.fill( ellipse );
}
}
---------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestColorIcon{

public java.awt.Color color = java.awt.Color.RED;

public Component createComponents()
{
JButton buttonRed = new JButton("R�d");
JButton buttonBlue = new JButton("Bl�");
JButton buttonGreen = new JButton("Gr�n");

final ColorIcon icon = new ColorIcon( 20);
final JLabel label = new JLabel( icon );

JPanel panel = new JPanel();
panel.setLayout( new GridLayout(0, 3) );
panel.add( buttonRed );
panel.add( buttonBlue );
panel.add( buttonGreen );
panel.add( label );

buttonRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.RED;
label.repaint();
}
});

buttonBlue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.BLUE;
label.repaint();
}
});

buttonGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.GREEN;
label.repaint();
}
});

return panel;
}


public static void main(String[] args)
{
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
}
catch (Exception e) {
}

JFrame frame = new JFrame("Change Color");
ChangeColor changeColor = new ChangeColor();
Component content = changeColor.createComponents();
frame.getContentPane().add( content );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
Thanks in advance....
kotoisin
Greenhorn

Joined: Nov 08, 2004
Posts: 5
No compiler says: cannot resolve symbol - class ChangeColor
I wonder how I change the color of the Icon then?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I change color on an Icon?
 
Similar Threads
Not to show the ellipse?
Movement is wacky, just increases.
Enter # in JTextField, draw = # ovals, use JBottons to color ovals???
painting custom components without extending jcomponent
Creating an image and Drawing to it.