Solved

Creating custom color in Java

Posted on 2011-02-13
5
755 Views
Last Modified: 2013-11-24
How do we create a custom color? You'll see the g.custom color on line 49.  
import javax.swing.*;
import java.awt.*;
import java.text.*;

  
public class FrameOfPanels extends JFrame
{

    // Instance variables
    private JPanel primaryPanel;
    
    /**
     * Constructor for objects of class FrameOfPanels
     */
    public FrameOfPanels(String title )
    {
        super( title );        
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
        
        primaryPanel = new JPanel( );
        primaryPanel.setBackground( Color.black );
        
        addRunnerInfoPanelsAndLabel( );

        this.getContentPane().add( primaryPanel );
        
//        this.setBackground(Color.black);
        this.pack();
        this.setSize(700,300);
        this.setVisible(true);
    }
    
    private void addRunnerInfoPanelsAndLabel( )
    {
        RunnerInfoPanel panelForRunner1;
        RunnerInfoPanel panelForRunner2;
        RunnerInfoPanel panelForRunner3;
        
        
  

        panelForRunner1 = new RunnerInfoPanel( new ImageIcon("image3.jpg"),
                                               "Smith",
                                               "Andrea",
                                               "GVSU",
                                               5,
                                               1205,
                                               1120,
                                              g.customColor) ;
        panelForRunner2 = new RunnerInfoPanel( new ImageIcon("image2.jpg"),
                                               Color.gray );
        panelForRunner3 = new RunnerInfoPanel( new ImageIcon("image1.jpg"),
                                               Color.white );
      
        primaryPanel.add( panelForRunner1 );
        primaryPanel.add( panelForRunner2 );
        primaryPanel.add( panelForRunner3 );
        
        double averageTime = (double)(panelForRunner1.getRunner( ).getSeconds( ) +
                                      panelForRunner2.getRunner( ).getSeconds( ) +
                                      panelForRunner3.getRunner( ).getSeconds( ) ) / 3;

        DecimalFormat dfmt = new DecimalFormat( "###,###.##" );        
        primaryPanel.add( new JLabel( "Average time in seconds of all runners: " +
                                          dfmt.format( averageTime ) ) );       
    }

}

Open in new window

0
Comment
Question by:Darkejo1
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34885036
Color custom = new Color(r, g, b);
0
 

Author Comment

by:Darkejo1
ID: 34885048
Now it's saying it can't find variable g.
0
 
LVL 92

Expert Comment

by:objects
ID: 34885055
r, g, and b are the RGB values for the colour. You'll need to set them to whatever colour you need
0
 

Author Comment

by:Darkejo1
ID: 34885063
I understand that, I think it's referring to how i'm calling the custom color. Normally I would use Color.white. Right now I'm trying to use g.customColor. I'm assuming the g in customColor is what it's referring to. I've already modified the values for RGB. Thanks for your help!
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34885140
       int r = 10;
        int g = 100;
        int b = 35;
        Color custom = new Color(r, g, b);
        panelForRunner1 = new RunnerInfoPanel( new ImageIcon("image3.jpg"),
                                               "Smith",
                                               "Andrea",
                                               "GVSU",
                                               5,
                                               1205,
                                               1120,
                                              custom) ;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now