Java paintComponent()绘制jMenu和JToolBar

Java paintComponent()绘制jMenu和JToolBar,java,swing,paintcomponent,jmenu,jtoolbar,Java,Swing,Paintcomponent,Jmenu,Jtoolbar,我用JMenu、JToolBar和JPanel编程了一个小的绘画应用程序,问题是,当我开始在面板上绘制时,JMenu和JToolBar绘制在同一个面板上,有时面板的背景会变成灰色而不是白色,如下所示: 这是我的密码: JFrame代码: public class ArdoiseF extends JFrame { private JMenuBar menu = new JMenuBar(); private JToolBar toolbar = new JToolBar(); private

我用JMenu、JToolBar和JPanel编程了一个小的绘画应用程序,问题是,当我开始在面板上绘制时,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;
}


}
公共类PanneauF扩展了JPanel{
私有整数i=0;
私有int mousex=0,mousey=0;
私有布尔值clean=true;;
私有颜色=Color.black;
私有字符串shape=“rond”;
ArrayList points=新的ArrayList();
公共PanneauF(){
this.addMouseMotionListener(新的MouseMotionListener(){
公共无效鼠标标记(鼠标事件e){
添加(新点(e.getX()-7,e.getY()-7,15,颜色,形状));
重新油漆();
}
public void mouseMoved(MouseEvent arg0){
}       
});
this.addMouseListener(新的MouseListener(){
公共无效mouseClicked(MouseEvent e){
}
公共无效鼠标事件(鼠标事件e){
}
公共无效mouseExited(MouseEvent e){
}
公共无效鼠标按下(MouseEvent e){
添加(新点(e.getX()-7,e.getY()-7,15,颜色,形状));
重新油漆();
}
公共无效MouseEvent(MouseEvent e){
}
});
}
公共组件(图形g){
抽签(g);
如果(清洁){
g、 setColor(Color.white);
g、 fillRect(0,0,this.getWidth(),this.getHeight());
干净=假;
points=新的ArrayList();
}
}
私人空间绘制(图形g){
对于(p点:此点){
g、 setColor(p.getColor());
if(p.getType()=“rond”)
g、 fillOval(p.getPosx(),p.getPosy(),10,10);
否则{
g、 fillRect(p.getPosx(),p.getPosy(),10,10);
}
}
}
公共void setClean(布尔c){
这个.clean=c;
}
公共空间设置颜色(c色){
这个颜色=c;
}
公共无效设置形状(字符串S){
这个.shape=S;
}
}

我也有同样的问题。将JMenuBar放在JFrame中。仅在绘图时使用JPanel。
希望有帮助。

我也有同样的问题。将JMenuBar放在JFrame中。仅在绘图时使用JPanel。
希望有帮助。

在重新绘制面板时,你真的应该重新绘制整个画面。首先清除它,然后进行检查并将您的所有积分重新添加

现在看起来很奇怪,因为你画出了所有的分数,然后你有时会,但只是有时清除它


只要总是把它弄清楚,然后画出要点——否则你可能会有以前画过的窗户上的零散的东西,永远不会被写下来。只有当Swing组件将自身标记为非不透明时,才允许它们不绘制每个像素。

重新绘制面板时,您真的应该重新绘制整个图像。首先清除它,然后进行检查并将您的所有积分重新添加

现在看起来很奇怪,因为你画出了所有的分数,然后你有时会,但只是有时清除它


只要总是把它弄清楚,然后画出要点——否则你可能会有以前画过的窗户上的零散的东西,永远不会被写下来。只有当Swing组件将自身标记为不透明时,才允许它们不绘制每个像素。

只需添加
super.paintComponent(g)
窗格uf中的
public void paintComponent(Graphics g)
方法的开头


阅读。

只需添加
super.paintComponent(g)
窗格uf中的
public void paintComponent(Graphics g)
方法的开头


阅读相关内容。

为了更快获得更好的帮助,请发布一条消息。面板违反了其不透明度约定:在pointComponent中调用super或将其不透明度设置为false。有关详细信息,请参阅关于在swing中绘制的文章(在swing标记wiki中引用),以获得更好的帮助,发布一条。面板违反了其不透明度约定:在pointComponent中调用super或将其不透明度设置为false。有关详细信息,请参阅关于在swing中绘制的文章(在swing标记wiki中引用)谢谢!它工作了,但是当我画东西的时候背景还是变成灰色的。通常背景是白色的,当我在上面画点的时候背景变成灰色的,我按照Tim B的要求做了,它工作了:),我还有一个问题,为什么画得慢?比如,当我画一条线时,它显然是由远离彼此的点组成的,我需要慢慢画,使它看起来像一条真正的线,它不像窗户、photoshop或任何其他绘画工具的绘画谢谢!它工作了,但是当我画东西的时候背景还是变成灰色的。通常背景是白色的,当我在上面画点的时候背景变成灰色的,我按照Tim B的要求做了,它工作了:),我还有一个问题,为什么画得慢?比如,当我画一条线时,它是由远离彼此的点清楚地画出来的,我需要慢慢地画,使它看起来像一条真正的线,它不像是窗户、photoshop或任何其他绘画工具的绘画。问题解决了,我只需清理面板并重新绘制我的点,谢谢!问题解决了,我只需清除面板并重新绘制我的点,谢谢!