简体   繁体   English

paintComponent方法混合了不同的JPanel组件

[英]paintComponent method mixes different JPanel components

I'm developing a software which paints 2 different JPanel for my GUI: a score and a mast guitar. 我正在开发一个为我的GUI绘制2种不同的JPanel的软件:乐谱和桅杆吉他。 The score is a class which extends JPanel and has paintComponent() method like this: 分数是扩展JPanel的类,并具有如下的paintComponent()方法:

public class PanelPartitura extends JPanel implements MouseListener{
  public void paintComponent(Graphics comp){
    super.paintComponent(comp);
    comp2D = (Graphics2D)comp;
    comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

    paintBackground();
    paintStave();
    paintNotes();

    [...]
  }
}

The mast guitar is a class as well: 桅杆吉他也是一门课:

public class PanelGuitarra extends JPanel implements MouseListener
  public void paintComponent(Graphics comp){
    super.paintComponent(comp);
    comp2D = (Graphics2D)comp;
    comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

    //Then I call secondary methods to paint it:
    paintBackground();              
    PaintPoints();
  }

  [...]
}

It still works fine. 它仍然可以正常工作。 I add the class PanelPartitura to a JScrollPane in order to scroll when it's playing: 我将类PanelPartitura添加到JScrollPane以便在播放时滚动:

partitura = new PanelPartitura();
JScrollPartitura = new JScrollPane(partitura, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

Both JPanel s mix each others painted components when the software is playing and scrolling. 在播放和滚动软件时,两个JPanel将彼此绘制的组件混合在一起。 I would like to ask, if somebody has a clue about what on earth is going on? 我想问一下,是否有人对地球上正在发生的事情有任何了解? In my opinion: 在我看来:

  1. It could be because I separated the painting methods as we've seen above: 可能是因为我分开了上面所看到的绘画方法:

     paintBackground(); paintStave(); paintNotes(); 

    then, when the software starts to paint, it paints some parts of the first JPanel ( paintBackground() for example) and then some parts of the mast guitar ( paintBackground() ), then it changes again and the result is a mixture of both. 然后,当软件开始绘制时,它将绘制第一个JPanel某些部分paintBackground()例如paintBackground() ),然后paintBackground()桅杆吉他的某些部分( paintBackground() ),然后再次更改,结果是两者的混合。

    I think this is because it mixes different parts every time, I mean it doesn't behave in the same way every time it plays. 我认为这是因为它每次都混合不同的部分,我的意思是每次播放时行为都不尽相同。

    I really don't want this to be happening, so let me ask you: how can I make atomic methods to be sure this wouldn't be the problem? 我真的不希望这种情况发生,所以让我问你:我如何制作原子方法以确保这不是问题?

  2. I missunderstood the scroll method. 我误解了滚动方法。 I scroll on this way: 我以这种方式滚动:

     //the note playing currently position is saved in positionBar positionBar = 150 + 50*PGuitarra.composicion.getAcordeSeleccionado().getPosicionXAcorde(); //horizontalScrollBar is moved to this position PGuitarra.JScrollPartitura.getHorizontalScrollBar().setValue(positionBar); 

I see that your paint methods are not using the same Graphics object (at the JPanel scope). 我看到您的绘制方法没有使用相同的Graphics对象(在JPanel范围内)。 Could that be the reason? 那可能是原因吗? And if it is, try passing comp (the Graphics object) as a parameter to paintBackground, paintStave and paintNotes. 如果是这样,请尝试将comp(图形对象)作为参数传递给paintBackground,paintStave和paintNotes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM