2013-02-01 1 views
0

JPanel을 사용하여이 미디어 재생 응용 프로그램의 타임 라인을 디자인하려고합니다. 타임 라인과 관련하여 현재 비디오 프레임의 표시를 제공하려면이 TimeLinePanel 클래스의 특정 x 좌표에 세로선을 그려야 JPanel을 확장 할 수 있어야합니다. 지금까지 paint() 함수를 사용하거나 paintComponent()을 재정의하는 데 많은 행운이 없었습니다. NewJApplet (이 클래스는 JApplet까지 확장됩니다) 클래스의 paint() 함수를 사용하려고 시도했을 때 세로선이 그려 지지만 타임 라인으로 사용하고있는 JPanel이 사라집니다 (테두리가 더 이상 보이지 않음). 또한 paint() 메서드를 사용하는 것이 좋지 않다고 들었습니다. 결과적으로 JPanel에서 paintComponent() 메서드를 재정의하려고 시도 했으므로 NewJApplet에서 paintComponent 메서드를 호출 해 보았습니다. 그러나 수직선으로 timeLinePanel1을 업데이트하지 않습니다. JPanel에서 특정 위치에 수직선을 그리는 방법은 무엇입니까?

어떤 도움

주시면 감사하겠습니다. 내가 애플릿을 실행하려고하면

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package helloworld; 

import java.awt.GridLayout; 
import javax.swing.border.EmptyBorder; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/** 
* 
* @author Justin 
*/ 
public class NewJApplet extends javax.swing.JApplet { 

    /** 
    * Initializes the applet NewJApplet 
    */ 
    @Override 
    public void init() { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 

     try { 
      /* 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
      * */ 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 

     //</editor-fold> 

     /* Create and display the applet */ 
     try { 
      java.awt.EventQueue.invokeAndWait(new Runnable() { 
       public void run() { 
        initComponents(); 
        timeLinePanel1.paintComponent(timeLinePanel1.getGraphics()); 
        timeLinePanel1.repaint(); 
       } 
      }); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    /** 
    * This method is called from within the init() method to initialize the 
    * form. WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     myPanel = new javax.swing.JPanel(); 
     timeLinePanel1 = new helloworld.TimeLinePanel(); 

     myPanel.setBackground(new java.awt.Color(51, 51, 51)); 

     javax.swing.GroupLayout myPanelLayout = new javax.swing.GroupLayout(myPanel); 
     myPanel.setLayout(myPanelLayout); 
     myPanelLayout.setHorizontalGroup(
      myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(myPanelLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(timeLinePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     myPanelLayout.setVerticalGroup(
      myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, myPanelLayout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(timeLinePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(myPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGap(0, 0, 0)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(myPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
     ); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    private javax.swing.JPanel myPanel; 
    private helloworld.TimeLinePanel timeLinePanel1; 
    // End of variables declaration 
} 


/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package helloworld; 

import java.awt.GridLayout; 
import javax.swing.border.EmptyBorder; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/** 
* 
* @author Justin 
*/ 
public class TimeLinePanel extends javax.swing.JPanel { 

    /** 
    * Creates new form TimeLinePanel 
    */ 
    public TimeLinePanel() { 
     initComponents(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     Rectangle r = new Rectangle(3, 0, 1, 25); 
     g.fillRect(25, 0, 1, 25); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 

     jPanel1.setBackground(new java.awt.Color(255, 255, 255)); 
     jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 200, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 25, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration 
} 

이 내가 무엇을 얻을 수 있습니다 :

다음

내가 지금까지 가지고있는 코드입니다. 이상적으로, (x, y) 흰색 상자 ( timeLinePanel1)의 좌표 (예 : 코드에 표시된대로)에 수직선을 그려서 내가 어떻게 할 수 있는지 이해하고 싶습니다. JPanel 내에서 세로선을 그립니다.

enter image description here

+0

다른 구성 요소가 타임 라인에 나타 납니까? 그렇지 않은 경우 캔버스로'BufferedImage'를 사용하여 레이블에 표시하는 것이 더 간단합니다. 나는 당신이 선호하는 크기와 레이아웃과 관련된 많은 문제를 추측 할 수 있습니다. 이미지를 사용하면 이러한 문제를 해결할 수 있습니다. –

답변

1

당신은 당신의 패널에있는 모든 페인트하기 위해 super.paintComponent를 호출 할 필요가 : 나는 그것이 정답이 있다고, 잘 모르겠어요

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.fillRect(25, 0, 1, 25); 
} 
0

하지만 아마 당신은 색상을 설정하는 것을 잊지 ?

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(Color.red); 
    Rectangle r = new Rectangle(3, 0, 1, 25); 
    g.fillRect(25, 0, 1, 25); 
}