Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8895993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:55:42+00:00 2026-06-14T23:55:42+00:00

I have sliders that will create RGB values and then use that to create

  • 0

I have sliders that will create RGB values and then use that to create a box and fill in its color. When I initialize my program it does not immediate draw the square. It will draw the square and begin filling it in after moving the slider though.

The method used to draw the rectangle and fill it with the color is called during the constructor but doesnt do anything, no errors, just doesnt draw the square. The slider calls the SAME method on state change and it will then draw the square. It makes no sense.

Im sure this is something that others may experience with lack of some foundations.

Here is my code you can copy paste and run in jGrasp or whatever to see what I mean. Thanks!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Colors extends javax.swing.JFrame {

    public Colors() {
        initComponents();
        addSliderListeners();
        addSliderLabels();
        this.setTitle("Color Mixer");
        sliderRed.setValue(150);
        sliderGreen.setValue(125);
        sliderBlue.setValue(215);
        updateImage();
    }

    private void addSliderListeners(){
        sliderRed.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                updateImage();
            }
        });
        sliderGreen.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                updateImage();
            }
        });
        sliderBlue.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                updateImage();
            }
        });
    }

    private void updateImage(){
        lblRedDec.setText(""+sliderRed.getValue());
        lblRedDec.setForeground(new Color(sliderRed.getValue(),0,0));
        lblRedHex.setText(Integer.toHexString(sliderRed.getValue()));
        lblRedHex.setForeground(lblRedDec.getForeground());

        lblGreenDec.setText(""+sliderGreen.getValue());
        lblGreenDec.setForeground(new Color(0,sliderGreen.getValue(),0));
        lblGreenHex.setText(Integer.toHexString(sliderGreen.getValue()));
        lblGreenHex.setForeground(lblGreenDec.getForeground());

        lblBlueDec.setText(""+sliderBlue.getValue());
        lblBlueHex.setText(Integer.toHexString(sliderBlue.getValue()));
        lblBlueDec.setForeground(new Color(0,0,sliderBlue.getValue()));
        lblBlueHex.setForeground(lblBlueDec.getForeground());

        Graphics2D g2 = (Graphics2D)this.getGraphics();
        g2.setColor(new Color(sliderRed.getValue(),sliderGreen.getValue(),sliderBlue.getValue()));
        g2.fill(new Rectangle(25,125,250,250));
    }

    private void addSliderLabels() {
        sliderRed.setPaintLabels(true);
        sliderGreen.setPaintLabels(true);
        sliderBlue.setPaintLabels(true);
    }
/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        sliderRed = new javax.swing.JSlider();
        lblRed = new javax.swing.JLabel();
        lblRedHex = new javax.swing.JLabel();
        lblRedDec = new javax.swing.JLabel();
        sliderGreen = new javax.swing.JSlider();
        lblGreen = new javax.swing.JLabel();
        lblGreenHex = new javax.swing.JLabel();
        lblGreenDec = new javax.swing.JLabel();
        sliderBlue = new javax.swing.JSlider();
        lblBlue = new javax.swing.JLabel();
        lblBlueHex = new javax.swing.JLabel();
        lblBlueDec = new javax.swing.JLabel();
        lblTitle = new java.awt.Label();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        sliderRed.setMajorTickSpacing(15);
        sliderRed.setMaximum(255);
        sliderRed.setMinorTickSpacing(5);
        sliderRed.setOrientation(javax.swing.JSlider.VERTICAL);
        sliderRed.setPaintTicks(true);
        sliderRed.setValue(212);
        sliderRed.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        sliderRed.setName(""); // NOI18N

        lblRed.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblRed.setText("Red");

        lblRedHex.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRedHex.setText("hex");

        lblRedDec.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRedDec.setText("dec");

        sliderGreen.setMajorTickSpacing(15);
        sliderGreen.setMaximum(255);
        sliderGreen.setMinorTickSpacing(5);
        sliderGreen.setOrientation(javax.swing.JSlider.VERTICAL);
        sliderGreen.setPaintTicks(true);
        sliderGreen.setSnapToTicks(true);
        sliderGreen.setValue(35);
        sliderGreen.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        sliderGreen.setName(""); // NOI18N

        lblGreen.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblGreen.setText("Green");

        lblGreenHex.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblGreenHex.setText("hex");

        lblGreenDec.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblGreenDec.setText("dec");

        sliderBlue.setMajorTickSpacing(15);
        sliderBlue.setMaximum(255);
        sliderBlue.setMinorTickSpacing(5);
        sliderBlue.setOrientation(javax.swing.JSlider.VERTICAL);
        sliderBlue.setPaintTicks(true);
        sliderBlue.setSnapToTicks(true);
        sliderBlue.setValue(218);
        sliderBlue.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        sliderBlue.setName(""); // NOI18N

        lblBlue.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblBlue.setText("Blue");

        lblBlueHex.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblBlueHex.setText("hex");

        lblBlueDec.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblBlueDec.setText("dec");

        lblTitle.setFont(new java.awt.Font("Dialog", 0, 36)); // NOI18N
        lblTitle.setText("Color Mixer");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRedHex)
                            .addComponent(lblRed)
                            .addComponent(lblRedDec))
                        .addGap(25, 25, 25))
                    .addComponent(sliderRed, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sliderGreen, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblGreenHex)
                            .addComponent(lblGreen)
                            .addComponent(lblGreenDec))
                        .addGap(25, 25, 25)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sliderBlue, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblBlueHex)
                            .addComponent(lblBlue)
                            .addComponent(lblBlueDec))
                        .addGap(25, 25, 25))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblBlueDec)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblBlueHex)
                            .addGap(18, 18, 18)
                            .addComponent(lblBlue)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(sliderBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblGreenDec)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblGreenHex)
                            .addGap(18, 18, 18)
                            .addComponent(lblGreen)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(sliderGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblRedDec)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblRedHex)
                            .addGap(16, 16, 16)
                            .addComponent(lblRed)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(sliderRed, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );

        lblTitle.getAccessibleContext().setAccessibleName("");
        lblTitle.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>                        


    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Colors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Colors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Colors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Colors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Colors().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel lblBlue;
    private javax.swing.JLabel lblBlueDec;
    private javax.swing.JLabel lblBlueHex;
    private javax.swing.JLabel lblGreen;
    private javax.swing.JLabel lblGreenDec;
    private javax.swing.JLabel lblGreenHex;
    private javax.swing.JLabel lblRed;
    private javax.swing.JLabel lblRedDec;
    private javax.swing.JLabel lblRedHex;
    private java.awt.Label lblTitle;
    private javax.swing.JSlider sliderBlue;
    private javax.swing.JSlider sliderGreen;
    private javax.swing.JSlider sliderRed;
    // End of variables declaration                   

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:55:43+00:00Added an answer on June 14, 2026 at 11:55 pm

    The reason that the color square doesn’t show up initially is due to the fact that GroupLayout causes a repaint of all of its child components which effectively ‘paints over’ the square.

    To get your square to show up you could either:

    • Make a custom JComponent color square & add to the GroupLayout.
    • Extract all current components to a new JComponent and override the paintComponent method so that all GroupLayout component are drawn first. This option would allow you not to add the color square to the GroupLayout.

    Code:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        updateImage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a slider that returns values from 0.0f to 1.0f. I want to
Right now I have a div that slides right to left and then vice
Im trying to create a fragment shader that will do the same job as
I'm trying to create a section on my page that will allow users to
I have found a MooTools version of Nivoo Slider that (in theory) will work
I'm trying to create a footer that will slide from the bottom of the
I'm trying to create a jQuery content slider that will eventually look like this
I want to create an informative app that will display a list of content.
I have a news slider for my site that I'd like to use but
I am trying to create a jQuery content slider that will stop when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.