Solved

Creating custom color in Java

Posted on 2011-02-13
5
762 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 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

733 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