2

我编写了一个小绘图应用程序,有一个 JMenu、JToolBar 和一个 JPanel,问题是,当我开始在面板上绘图时,JMenu 和 JToolBar 是在同一个面板上绘制的,有时面板的背景会变成灰色白色的,这是它的样子:java错误

这是我的代码: 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;
}


}
4

3 回答 3

3

只需在方法super.paintComponent(g);的开头添加.public void paintComponent(Graphics g)PanneauF

阅读customPaintings

于 2013-12-09T13:56:26.003 回答
1

重绘面板时,您真的应该重绘整个画面。首先清除它,然后再重新添加所有积分。

现在看起来很奇怪,因为你画了所有的点,然后你有时但有时只清除它?

只需始终清除它然后绘制点 - 否则您可能会从以前的窗口油漆中获得永远不会被覆盖的杂散东西。仅当 Swing 组件将自己标记为不透明时,才允许不绘制到每个像素。

于 2013-12-09T13:49:08.693 回答
0

我有同样的问题。将 JMenuBar 放在您的 JFrame 中。仅将 JPanel 用于绘图。希望能帮助到你。

于 2013-12-09T13:43:34.500 回答