2012-11-16 115 views
1

任何人都可以解釋一個框架上繪製圖形的正確方法嗎?我看了其他例子,這應該工作,但它沒有。請參閱「畫圖」方法。我試圖畫一個正方形(矩形),它不出現在我的框架上。Java GUI矩形圖形

問候

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(); 
    } 
    public void Paint(Graphics g){ 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setColor(Color.BLUE); 
     g2.draw(new Rectangle(25,25,200,200)); 
    } 
    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()); 
//Color textColor = new Color( 255-lblRedDec.getForeground().getRed(), 
//        255-lblRedDec.getForeground().getGreen(), 
//        255-lblRedDec.getForeground().getBlue() 
//       ); 
//jLabel1.setBackground(textColor); 

     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()); 

    } 

    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(); 

     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.setSnapToTicks(true); 
     sliderRed.setToolTipText(""); 
     sliderRed.setValue(212); 
     sliderRed.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
     sliderRed.setName(""); // NOI18N 

     lblRed.setText("Red"); 

     lblRedHex.setText("hex"); 

     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.setToolTipText(""); 
     sliderGreen.setValue(35); 
     sliderGreen.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
     sliderGreen.setName(""); // NOI18N 

     lblGreen.setText("Green"); 

     lblGreenHex.setText("hex"); 

     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.setToolTipText(""); 
     sliderBlue.setValue(218); 
     sliderBlue.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
     sliderBlue.setName(""); // NOI18N 

     lblBlue.setText("Blue"); 

     lblBlueHex.setText("hex"); 

     lblBlueDec.setText("dec"); 

     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() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(layout.createSequentialGroup() 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(lblRedHex) 
          .addComponent(lblRed) 
          .addComponent(lblRedDec)) 
         .addGap(25, 25, 25)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(26, 279, Short.MAX_VALUE) 
         .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.UNRELATED) 
       .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))) 
       .addGap(6, 6, 6)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .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))) 
       .addContainerGap()) 
     ); 

     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 javax.swing.JSlider sliderBlue; 
    private javax.swing.JSlider sliderGreen; 
    private javax.swing.JSlider sliderRed; 
    // End of variables declaration 

} 
+2

將'public void Paint(Graphics g)'更改爲'public void paint(Graphics g)'並且在此方法之前添加註釋'@ Override' – CAMOBAP

+0

好吧嗯,是的,作品謝謝 – Kairan

+1

作爲一般建議。不要繪製成像JFrame這樣的頂級容器。而是將'JPanel'中的'@ Override' paintComponent(Graphics)'添加到框架中,或者交替地在添加到框架(或面板等)的'BufferedImage'中繪製。 –

回答

1

這是塗料(以小寫的 'p')

0

爲CAMOBAB在評論

變化public void Paint(Graphics g)public void paint(Graphics g)並添加註釋@Override這種方法

之前說的