2013-12-09 48 views
2

我用一個JMenu,JToolBar和JPanel編寫了一個小繪畫應用程序,問題是,當我開始繪製面板時,JMenu和JToolBar繪製在同一個面板上,有時面板的背景變成灰色,而不是白色的,這裏是什麼樣子:java errorpaintComponent()繪製jMenu和JToolBar

這裏是我的代碼: 的JFrame代碼:

public class ArdoiseF extends JFrame { 

private JMenuBar menu = new JMenuBar(); 
private JToolBar toolbar = new JToolBar(); 
private JMenu file = new JMenu("Fichier"); 
private JMenu edit = new JMenu("Edition"); 
private JMenu about = new JMenu("About"); 
private JMenu shape = new JMenu("Forme du curseur"); 
private JMenu color = new JMenu("Couleur du curseur"); 
private JMenuItem clear = new JMenuItem("Effacer"); 
private JMenuItem quit = new JMenuItem("Quitter"); 
private JMenuItem rond = new JMenuItem("Rond"); 
private JMenuItem carre = new JMenuItem("Carre"); 
private JMenuItem rouge = new JMenuItem("Rouge"); 
private JMenuItem bleu = new JMenuItem("Bleu"); 
private JMenuItem noir = new JMenuItem("Noir"); 

private JButton rougeButton = new JButton(new ImageIcon("rouge.jpg")); 
private JButton bleuButton = new JButton(new ImageIcon("bleu.jpg")); 
private JButton noirButton = new JButton(new ImageIcon("noir.jpg")); 
private JButton formecarreeButton = new JButton(new ImageIcon("formecarree.png")); 
private JButton formerondeButton = new JButton(new ImageIcon("formeronde.png")); 

private JPanel container = new JPanel(); 
private PanneauF pan = new PanneauF(); 
private ColorListener cListener = new ColorListener(); 
private ShapeListener shapeListener = new ShapeListener(); 


public ArdoiseF(){ 

    this.setTitle("Paint -_-"); 
    this.setSize(700,500); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    initComposants(); 
    this.setVisible(true); 
} 

private void initComposants(){ 
    file.add(clear); 
    file.addSeparator(); 
    file.add(quit); 
    file.setMnemonic('F'); 
     clear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK)); 
     quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_DOWN_MASK)); 

    shape.add(rond); 
    shape.add(carre); 
    color.add(rouge); 
    color.add(bleu); 
    color.add(noir); 

    edit.add(shape); 
    edit.add(color); 
    edit.setMnemonic('E'); 

    menu.add(file); 
    menu.add(edit); 


    toolbar.add(formecarreeButton); 
    toolbar.add(formerondeButton); 
    toolbar.addSeparator(); 
    toolbar.add(noirButton); 
    toolbar.add(rougeButton); 
    toolbar.add(bleuButton); 

    clear.addActionListener(new ClearListener()); 
    rougeButton.addActionListener(cListener); 
    bleuButton.addActionListener(cListener); 
    noirButton.addActionListener(cListener); 
    rouge.addActionListener(cListener); 
    bleu.addActionListener(cListener); 
    noir.addActionListener(cListener); 
    formecarreeButton.addActionListener(shapeListener); 
    formerondeButton.addActionListener(shapeListener); 
    carre.addActionListener(shapeListener); 
    rond.addActionListener(shapeListener); 

    container.setLayout(new BorderLayout()); 
    container.add(toolbar,BorderLayout.NORTH); 
    container.add(pan,BorderLayout.CENTER); 
    this.setContentPane(container); 

    this.setJMenuBar(menu); 

} 


class ClearListener implements ActionListener{ 

    public void actionPerformed(ActionEvent e) { 
     pan.setClean(true); 
     pan.repaint(); 
    } 

} 
class ColorListener implements ActionListener{ 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == rougeButton || e.getSource() == rouge) 
      pan.setColor(Color.red); 
     if(e.getSource() == bleuButton || e.getSource() == bleu) 
      pan.setColor(Color.blue); 
     if(e.getSource() == noirButton || e.getSource() == noir) 
      pan.setColor(Color.black); 
    } 
} 
class ShapeListener implements ActionListener{ 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == formecarreeButton || e.getSource()== carre) 
      pan.setShape("carre"); 
     if(e.getSource() == formerondeButton || e.getSource()== rond) 
      pan.setShape("rond"); 
    } 

} 

} 

的JPanel代碼:

public class PanneauF extends JPanel{ 
private int i=0; 
private int mousex=0,mousey=0; 
private boolean clean=true;; 
private Color color= Color.black; 
private String shape = "rond"; 
ArrayList<Point> points = new ArrayList<Point>(); 


public PanneauF(){ 
    this.addMouseMotionListener(new MouseMotionListener(){ 

     public void mouseDragged(MouseEvent e) { 
      points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape)); 
      repaint(); 

     } 

     public void mouseMoved(MouseEvent arg0) { 

     }  
    }); 
    this.addMouseListener(new MouseListener(){ 

     public void mouseClicked(MouseEvent e) { 

     } 

     public void mouseEntered(MouseEvent e) { 

     } 

     public void mouseExited(MouseEvent e) { 

     } 

     public void mousePressed(MouseEvent e) { 
      points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape)); 
      repaint(); 

     } 

     public void mouseReleased(MouseEvent e) { 

     } 
    }); 
} 
public void paintComponent(Graphics g){ 
    draw(g); 
    if(clean){ 
     g.setColor(Color.white); 
     g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     clean=false; 
     points = new ArrayList<Point>(); 
    } 
} 

private void draw(Graphics g){ 
    for (Point p: this.points){ 
     g.setColor(p.getColor()); 
     if(p.getType()=="rond") 
      g.fillOval(p.getPosx(), p.getPosy(), 10, 10); 

     else{ 
       g.fillRect(p.getPosx(), p.getPosy(), 10, 10); 
     } 
    } 
} 
public void setClean(boolean c){ 
    this.clean = c; 
} 
public void setColor(Color c){ 
    this.color = c; 
} 
public void setShape(String S){ 
    this.shape=S; 
} 


} 
+0

爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

該面板違反了其不透明合同:或者在pointComponent中調用super或將其不透明度設置爲false。有關詳細信息,請參閱關於在swing中繪畫的文章(在swing標籤wiki中引用) – kleopatra

回答

1

重新繪製面板時,您應該重繪整個事物。開始清除它,然後通過並將所有點加回去。

現在看起來很奇怪,因爲您繪製了所有的點,然後有時您有時但只是有時清除它?

只要始終清除它,然後繪製點 - 否則你可能會從以前的窗口塗料流失的東西,永遠不會寫。如果Swing組件標記自己不是不透明的,則只允許Swing組件不能繪製到每個像素。

+0

問題解決了,我只需要清除面板並重繪我的觀點,謝謝! – FrankelStein

0

我有同樣的問題。將JMenuBar放入JFrame中。僅使用JPanel進行繪製。 希望它有幫助。

3

只需在PanneauFpublic void paintComponent(Graphics g)方法開始處加上super.paintComponent(g);即可。

閱讀關於customPaintings

+0

謝謝!它正在工作,但當我畫畫時,背景仍然變得灰暗。 – FrankelStein

+0

不客氣,我無法解決灰色問題(。 – alex2410

+0

通常情況下,背景是白色的,當我畫點時它會變成灰色,我做了Tim B要求的東西,它正在工作:),我有一個更多的問題,爲什麼繪圖緩慢?比如,當我畫出一條線時,它顯然是由彼此遠離的點組成的,我需要慢慢繪製,使其看起來像一條真正的線條,它不像繪製窗戶,Photoshop或任何其他繪畫工具 – FrankelStein