Solved

Creating custom color in Java

Posted on 2011-02-13
5
754 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 76
countAbc challenge 9 49
What is the latest versions eclipse neon 2 117
ArrayIndexOutOfBoundException 9 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

17 Experts available now in Live!

Get 1:1 Help Now