2013-03-01 54 views
0

我正在制作音乐/绘图应用程序,您可以使用鼠标自由绘制线条,并且TrackBar可以水平穿过它并读取线条并使用x和y坐标修改音乐。在面板上添加面板,两者都可见

  • 我遇到的问题是,TrackBar类是一个JPanel并DrawBoard类是一个JPanel,我在一个框架中添加。但是哪一个是最顶层的就是要显示的那个,我想要的是让他们都显示出来,并且TrackBar在绘制的线条之上传递。

下面是这3类的源代码,并拿出一些unncesesary代码:

主机类,我将它们添加(当前设置的跟踪条犯规出现,但出现了绘图板,因为它重叠它):

public class MainFrame extends JFrame { 

public static ColourToolbar colourBar; 
public static TrackBar tb; 

public MainFrame(){ 

    super("VIPE by Prestige WorldWide"); 


    // Top colour toolbar for tones 
    colourBar = new ColourToolbar(); 
    this.getContentPane().add(colourBar, BorderLayout.NORTH); 

    // This class ImagePanel is a JPanel which I overite the paintCOmponent to have background 
    ImagePanel bg = new ImagePanel(); 
     bg.setLayout(new BorderLayout()); 
     Dimension size = getPreferredSize(); 
     size.setSize(1024,800); //w, h 
     bg.setPreferredSize(size); 
    this.getContentPane().add(bg, BorderLayout.CENTER); 



    // I add the TrackBar to the ImagePanel since its the Background 
    tb = new TrackBar(); 
    bg.add(tb, BorderLayout.CENTER); 

    // I add the drawing board but It will overlap the Trackbar 
    DrawBoard dboard = new DrawBoard(); 
    bg.add(dboard, BorderLayout.CENTER); 




    // The control toolbar where the settings and control buttons are. 
    Toolbar toolBar = new Toolbar(); 
    this.getContentPane().add(toolBar, BorderLayout.SOUTH); 
} 

public static void main(String[] args){ 

    MainFrame frame = new MainFrame(); 

    frame.setBackground(Color.WHITE); 
    frame.setSize(1024,768); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    //frame.pack(); 
    frame.setVisible(true); 

} 


} 

现在的TrackBar类:

public class TrackBar extends JPanel{ 

private TrackBarAction tba = new TrackBarAction(this); 
public static int TIME = 9; 

public Timer t = new Timer(TIME, tba); 
public static double x = 0, y = 0, velX = 1.0, velY = 0; 

private int SKIP = 120; 


public TrackBar(){ 

    this.setOpaque(false); 
} 

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

      Graphics2D g2d = (Graphics2D)g; 
      //g2d.drawRect((int)x, (int)y, 10, 800); 

      Rectangle2D r2d = new Rectangle2D.Double(x, y, 8.0, 800.0); // x,x, w, h 
      g2d.setPaint(Color.DARK_GRAY); 
      g2d.fill(r2d); 
      g2d.draw(r2d); 
} 

public void reset(){ 

    t.stop(); 
    x = 0; 
    y = 0; 
    velX = 0.5; 
    velY = 0; 
    this.repaint(); 
} 

public void skipForward(){ 

    if(x+SKIP <= 1024){ 
    x += SKIP; 
    } else { 
     x = 1024 - x; 
    } 

    this.repaint(); 
} 

public void skipBackwards(){ 

    if(x-SKIP >= 0){ 
    x -= SKIP; 
    } else { 
     x = 0; 
    } 

    this.repaint(); 
} 

} 

而现在的DrawBoard类:

public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{ 


(..) 

public Color currentColor; 

public static boolean eraser = false; 

private int xX1, yY1; 

public DrawBoard(){ 



    Graphics2D g2d = bImage.createGraphics(); 
    g2d.dispose(); 

    Dimension size = getPreferredSize(); 
    size.setSize(1024,800); //w, h 
    setPreferredSize(size); 
    //status = new JLabel("default"); 
    //add(status, BorderLayout.SOUTH); 
    addMouseListener(this); 
    addMouseMotionListener(this); 


    imgLabel = new JLabel(new ImageIcon(bImage)) { 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     paintInLabel(g); 
    } 
    }; 
    imgLabel.setOpaque(false); 
    setOpaque(false); 
     add(imgLabel, BorderLayout.CENTER); 

} 

private void paintInLabel(Graphics g) { 

    if(!eraser){ 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setColor(getColor()); // this colour is when mouse is pressed 
     g2d.setStroke(new BasicStroke(STROKESIZE)); 
     if (points.size() < 1) { 
      return; 
     } 
     for (int i = 1; i < points.size(); i++) { 
      int x1 = points.get(i - 1).x; 
      int y1 = points.get(i - 1).y; 
      int x2 = points.get(i).x; 
      int y2 = points.get(i).y; 
      g2d.drawLine(x1, y1, x2, y2); 
     } 
    } 

} 


// Where the drawing happens 
@Override 
public void mousePressed(MouseEvent e) { 
    //status.setText("you pressed down the mouse"); 
    if(!eraser){ 
     xX1 = e.getX(); 
     yY1 = e.getY(); 
     points.add(e.getPoint()); 
    } 
} 

@Override 
public void mouseDragged(MouseEvent e) { 
    //status.setText("you draged the mouse"); 
    Graphics2D g2d = bImage.createGraphics(); 

    // this is the eraser code 
    if(eraser){ 
     //Graphics2D g2d = bImage.createGraphics(); 
     System.out.println("eraser = true"); 
     System.out.println("Stroke = " + STROKESIZE); 
     g2d.setComposite(AlphaComposite.Clear); 
     g2d.setColor(new Color(0, 0, 0, 0)); 
     g2d.drawRect(e.getX(), e.getY(), STROKESIZE, STROKESIZE); 
     g2d.fillRect(e.getX(), e.getY(), STROKESIZE, STROKESIZE); 
     // g2d.setColor(c); 

     imgLabel.repaint(); 
    }else { 
     //g2d.setComposite(); 
     points.add(e.getPoint()); 
     System.out.println("point = " + e.getPoint()); 
     imgLabel.repaint(); 
    } 

} 

@Override 
public void mouseReleased(MouseEvent e) { 
    //status.setText("you release the mouse click"); 
    if(!eraser){ 
     Graphics2D g2d = bImage.createGraphics(); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setColor(getColor()); // this is the final colour 
     g2d.setStroke(new BasicStroke(STROKESIZE)); 


     if (points.size() >= 2) { 
      for (int i = 1; i < points.size(); i++) { 
       int x1 = points.get(i - 1).x; 
       int y1 = points.get(i - 1).y; 
       int x2 = points.get(i).x; 
       int y2 = points.get(i).y; 
       g2d.drawLine(x1, y1, x2, y2); 
      } 
     } 
    g2d.dispose(); 

    points.clear(); 
    imgLabel.repaint(); 
    } 
    //imgLabel.repaint(); 

} 
// End of where the drawing happens 

public void clearDrawBoard() { 

} 

private Color getColor() { 

    return ColourToolbar.selectedColor; 
} 

private void setColor(Color col){ 

    this.currentColor = col; 
} 

@Override 
public void mouseClicked(MouseEvent e) { 
    //throw new UnsupportedOperationException("Not supported yet."); 
} 

} 

回答

4

BorderLayout只能有一个组件添加到它们的每个5个位置。通过将DrawPanel添加到中心位置,您可以有效地移除TrackPanel。

相反,将TrackBar的布局管理器设置为BorderLayout并将DrawPanel添加到它。

您也可以使用JLayeredPane的,但管理变得更复杂

+0

权............. – mKorbel 2013-03-01 20:24:16

+0

是的,这做的工作!谢谢! – nullwriter 2013-03-02 15:55:09