Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 791
  • Last Modified:

Creating custom color in Java

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
Darkejo1
Asked:
Darkejo1
  • 3
  • 2
1 Solution
 
objectsCommented:
Color custom = new Color(r, g, b);
0
 
Darkejo1Author Commented:
Now it's saying it can't find variable g.
0
 
objectsCommented:
r, g, and b are the RGB values for the colour. You'll need to set them to whatever colour you need
0
 
Darkejo1Author Commented:
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
 
objectsCommented:
       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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now