Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Color Problem

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Color Problem

    Ok, so I'm reading and writing excel files for work using a program I created to do my work for me and I'm having 1 formatting issue. I cant get java to produce the correct color based on the RGB values I got from an excel template we have at work.

    I dont know the exact RGB value b/c I'm not at work, but I believe it was something around
    R: 197, G: 217, B: 241

    However this was just giving me white, while I needed a light blueish green color. If a tweaked the RGB a bit, it would either turn into medium blue or white (depending on which way I tweaked). I also tried some colors with RGB values I got from a color table I found online, but the colors didnt quite match up there either. If it was supposed to be a medium-dark color, it would appear darker than intended. If it was supposed to be a light-medium color, it would appear practically white or extremely light.

    My boss said I can just find a different similar color, but its REALLY pissing me off. Is there something special I should know about Java's Color class? I never have been able to really get the colors right.


    BTW, I use SmartXLS to read/write xls files, the API sucks but the email support and the power and ease of use of it is incredible.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Color Problem

    The color you specified should be a light bluish color, it should not be white unless your monitor is completely uncalibrated or you are creating the Color incorrectly. How are you creating a new color based upon these values?
    For what its worth, here is a small class that I wrote that has helped me to play around with colors while in my IDE: it allows you view the color based upon RGB values (click on the RGB tab - this does not take into account alpha values obviously):
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class ColorTest extends JFrame{
    	public ColorTest(){
    		super();
     
    		JPanel main = new JPanel();
    		 chooser = new JColorChooser();
    		main.add(chooser);
    		JButton button = new JButton("Print New Color");
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				Color c = chooser.getColor();
    				System.out.print("new Color(");
    				System.out.print(c.getRed());
    				System.out.print(", ");
    				System.out.print(c.getGreen());
    				System.out.print(", ");
    				System.out.print(c.getBlue());
    				System.out.println(");");
     
    			}
    		});
    		main.add(button);
    		getContentPane().add(main);
    		pack();
    		setVisible(true);
    	}
    	JColorChooser chooser;
    	public static void main(String[] args){
    		new ColorTest();
    	}
    }
    Last edited by copeg; July 8th, 2010 at 09:07 PM.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Color Problem

    Well I think the problem may be that for SmartXLS, I need to send Excel the color as a RGB int.

    For instance, here is the line where I tell the Excel Cell to be set the color:

    RangeStyle.setPatternFG(new Color(197,217,241).RGB());


    Now, there is the possibility that I just messed it up entirely, but I got a different cell to turn near purple like I wanted. Here is the API code for this method:
    API:
    setPatternFG

    public void setPatternFG(int fg)

    Sets the color used to display the pattern foreground.

    Parameters:
    fg - an integer describing the font color as a four-byte integer in the format 0x00RRGGBB.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Color Problem

    Ok, so I used a method to get the color I needed.

    getPatternFG
    public int getPatternFG()Returns the color used to display the pattern foreground.

    Returns:
    an integer representing the color. The color is returned as a four-byte integer in the format 0x00RRGGBB.
    This returned 27.

    So I tried setPatternFG(27) and got black...

    Any thoughts from here?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Color Problem

    Foreground would be the color of the letters. Black seems about right.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Color Problem

    Thats what I thought a few weeks ago when I started this. For some reason, the API has Foreground set as the Background and I have no idea what background is. The text color is set by using the following method:
    setFontColor
    public void setFontColor(int color)Sets the color used to display the font.

    Parameters:
    color - integer describing the font color as a four-byte integer in the format 0x00RRGGBB.

Similar Threads

  1. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  2. changing the visited links color in JEditorpane
    By nasi in forum AWT / Java Swing
    Replies: 5
    Last Post: April 18th, 2010, 08:44 AM
  3. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  4. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 10:57 AM
  5. How prograssbarColor with differentThe color indicates
    By NARs in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2009, 10:04 PM