?
Solved

Creating custom color in Java

Posted on 2011-02-13
5
Medium Priority
?
769 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 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