2015-04-01 51 views
1

在我的圖形分配中,我無法設置面板的背景顏色。如果我將ColorPanel放到另一個面板中,面板背景的顏色會改變,但是如果我改變面板的尺寸,則圓形不會移動。在我的任務,要求,如果我改變幀的大小,圓應在框架要設置面板​​在畫面的中心無法設置面板的背景顏色

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 

    public class ColorMenuFrame extends JFrame { 
     private JMenu colorMenu = new JMenu("Colors"); 
     private ColorPanel p = new ColorPanel(); 

     public ColorMenuFrame(){ 
     p.setPreferredSize(new Dimension(100, 100)); 
     this.add(p); 
     } 

     // Main Method 
     public static void main(String[] args) { 
     ColorMenuFrame frame = new ColorMenuFrame(); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     } 

     //ColoRpanel class 

     public class ColorPanel extends JPanel { 
     private int diameter; 

     public ColorPanel() { 

     // Creating Colors JMEnuBar 
     JMenuBar menuBar = new JMenuBar(); 
     setJMenuBar(menuBar); 

     JMenu colorMenu = new JMenu("Colors"); 
     menuBar.add(colorMenu); 

     // Creating Background and Foreground menus 
     JMenu backGroundMenu = new JMenu("BackGround"); 
     JMenu foreGroundMenu = new JMenu("ForeGround"); 

     // Items inside the Background and Foreground menus 
     JMenuItem redItem = new JMenuItem("Red"); 
     JMenuItem greenItem = new JMenuItem("Green"); 
     JMenuItem blueItem = new JMenuItem("Blue"); 

     JMenuItem redItem1 = new JMenuItem("Red"); 
     JMenuItem greenItem1 = new JMenuItem("Green"); 
     JMenuItem blueItem1 = new JMenuItem("Blue"); 

     // Adding Red, Green and Blue Items to backGround menu  
     backGroundMenu.add(redItem); 
     backGroundMenu.add(greenItem); 
     backGroundMenu.add(blueItem); 

     // Adding Red, Green and Blue Items to ForeGround menu 
     foreGroundMenu.add(redItem1); 
     foreGroundMenu.add(greenItem1); 
     foreGroundMenu.add(blueItem1); 

     // Adding backGround and foreground sub menus to main Menu Bar Colors 
     colorMenu.add(backGroundMenu); 
     colorMenu.add(foreGroundMenu); 

     //Calling ActionListeners 

     // Calling ActionListner after clicking on Red button 

     redItem.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
      setBackground(Color.RED); 
      repaint(); 
     } 
     }); 

     // Calling ActionListner after clicking on Green button 
     greenItem.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      setBackground(Color.GREEN); 
      repaint(); 
     } 
     }); 

     // Calling ActionListner after clicking on blue button 

     blueItem.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      setBackground(Color.BLUE); 
      repaint(); 
     } 
     }); 

     // Calling ActionListner after clicking on Red button 
     redItem1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      setForeground(Color.RED); 
      repaint(); 
     } 
     }); 

     // Calling ActionListner after clicking on Green button 
     greenItem1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     setForeground(Color.GREEN); 
     repaint(); 
     } 
     }); 

     // Calling ActionListner after clicking on Green button 
     blueItem1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      setForeground(Color.BLUE); 
     repaint(); 
     } 
     }); 

    } 
    //Paint Component Method, to create circle at the center of panel  
    protected void paintComponent(Graphics g){ 
     int width = getSize().width; 
     int height =getSize().height; 

     if (width <= height){ 
     diameter = width/2; 
     } 
     else if (height <= width){ 
     diameter = height/2; 
     } 
     int r = diameter/2; 
     int x = (width/2) - r; 
     int y = (height/2)- r; 

     //Drawing Circle 
     g.fillOval(x, y, diameter, diameter); 
     } 
     } 
     } 

回答

1

不要忘記在你的paintComponent方法覆蓋內調用super.paintComponent(g)。在第一行上做。

@Override 
    protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    // .... 

這將告訴JPanel做自己的家務繪畫。

另外,如果你想順利出你圈的邊界,設置圖形渲染提示做抗鋸齒:

 // do this: 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

    // before calling this: 
    g.fillOval(x, y, diameter, diameter); 

請注意,你的圈子似乎中心本身就好了,當我運行代碼。

+0

非常感謝。我加了super.paintComponent(g);在paintComponent方法中,它工作 – Minal 2015-04-01 16:03:53

0

工作的中心?你可以用panel.setLocationRelativeTo(frame)來做到這一點。 (當然你應該在面板類上有一個框架實例)