2012-11-17 41 views
1

我有滑块可以创建RGB值,然后用它创建一个框并填充它的颜色。当我初始化我的程序时,它不会立即画出正方形。它将绘制正方形,并在移动滑块后开始填充它。表格加载时的Java GUI绘制和填充矩形

用于绘制矩形并填充颜色的方法在构造函数中被调用,但不做任何事情,没有错误,只是不绘制正方形。滑块在状态改变时调用SAME方法,然后它将绘制正方形。这个不成立。

我确定这是其他人可能会遇到的缺乏某些基础。

这里是我的代码,你可以复制粘贴并运行在jGrasp或任何看到我的意思。谢谢!

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     

} 
+0

只是猜测这里。也许现在在构造函数中改变颜色还为时过早。你应该测试看看'this.getGraphics()'返回什么。还要注意这一点:'new Colors()。setVisible(true)'。你实例化'JFrame',然后你才叫'setVisible'。 –

+0

@JamesPoulson:该代码是由NetBeans自动生成的。但是,我不确定这会如何影响显示。 – Kairan

+0

是的,我注意到底部的变量声明。我的想法是,当调用'setVisible'时,JFrame使用的画布可能会重置或刷新。你应该检查它是如何工作的。 –

回答

2

的原因,颜色方块最初不露面是因为这样的事实:GroupLayout导致所有的子组件的重绘其有效地“描绘了”广场。

为了让您的广场上显示你既可以:

  • 创建自定义JComponent颜色方块&添加到GroupLayout
  • 将所有当前组件提取到新的JComponent并覆盖paintComponent方法,以便首先绘制所有GroupLayout组件。该选项允许您不要将颜色方块添加到GroupLayout

代码:

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    updateImage(); 
} 
+0

“颜色方块最初没有出现的原因是由于GroupLayout会重新绘制其所有有效地”覆盖“方块的子组件。” 你是怎么弄出来的:p? –

+1

将应用程序和提取出来的'paintComponent'运行完美。在知道它的行为之前,先使用这个布局。 – Reimeus

+0

@Reimeus:我插入它没有结果,你能告诉我代码的目的地是在哪里? – Kairan