2013-01-24 4 views
0

저는 현재 Lego Mindstorm NXT와 상호 작용할 수있는 간단한 GUI 인터페이스를 구현하고 있습니다. 현재 내 문제는 내 인터페이스의 페인트 문제입니다. 내 MainUI가로드 될 때 GridPanel을 설정하는 GirdPanel()이라는 메서드를 호출합니다. JFrame을 확장 한 MainUI가이 패널을 JFrame 호출에 추가합니다. 다음은이 문제와 관련된 MainUI의 전체 코드입니다.paint가 호출 된 후 JPanel이 JFrame에 표시되지 않습니다.

public MainUI(){ 
    setSize(700, 600); 

    PathPanel pathPanel = new PathPanel(controller); 
    add(pathPanel, BorderLayout.WEST); 
    CurrentPath.getInstance().addPathDataListener(pathPanel); 
    CurrentPath.getInstance().addPointSelectionListener(pathPanel); 

    gridPanel(); 
    add(gridPanel, BorderLayout.CENTER); 


    robotControlBar(); 
    add(robotControls, BorderLayout.NORTH); 

    setJMenuBar(menuPanel()); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setVisible(true); 
} 

public void gridPanel(){ 
    gridPanel = new JPanel(new BorderLayout()); 
    WGraph graph = new WGraph(); 

    gridPanel.add(graph, BorderLayout.CENTER); 

} 

WGraph는 JPanel을 확장하고이 프로그램의 그래프 표시를 제어하는 ​​클래스입니다.

public class WGraph extends JPanel{ 

public WGraph(){ 
    //WGraph Panel property setup 
    setLayout(new BorderLayout()); 
    //Variable creation 
    points = new ArrayList<Dot>(); 
    //Label to display coordinates of selected point 
    pointDisplay = new JLabel("Selected point at: None Chosen"); 

    //testPoints(); //Comment Out when test dots not needed. 

    //Create Graph Panel 
    panel = new JPanel(); 
    panel.setBackground(PANEL_COLOR); 

    //Mouse Listeners for Panel 
    MouseEventHandler mouseListener = new MouseEventHandler(); 
    panel.addMouseListener(mouseListener); 
    panel.addMouseMotionListener(mouseListener); 


    //Adding components to the WGraph panel 
    add(pointDisplay, BorderLayout.NORTH); 
    add(panel, BorderLayout.CENTER); 

    repaint(); 
} 
public void paintComponent(Graphics g){ 
    // invokes default painting for JFrame; must have this! 
    super.paintComponent(g); 

    // paint on the canvas rather than the JFrame 
    Graphics pg = panel.getGraphics(); 
    System.out.println("*"); //Print out to see when repaint has been called. for testing only 
    int width = panel.getWidth(); 
    int height = panel.getHeight(); 
    pg.setColor(GRID_COLOR); 

    for (int i = 50; i < width; i+=50) { 
     pg.drawLine(i, 0, i, height); 
    } 

    for (int i = 50; i < width; i+=50) { 
     pg.drawLine(0, i, width, i); 
    } 

    Dot previousPoint = null; 

    for (int i = 0; i < points.size(); i++) { 
     Dot currentPoint = points.get(i); 
     currentPoint.draw(pg); 

     if (previousPoint != null) { 
      pg.setColor(Dot.DESELECTED_COLOR); 
      pg.drawLine(new Float(previousPoint.getCenter().x).intValue(), 
        new Float(previousPoint.getCenter().y).intValue(), 
        new Float(currentPoint.getCenter().x).intValue(), 
        new Float(currentPoint.getCenter().y).intValue()); 
     } 

     previousPoint = currentPoint; 
    } 
} 

결국 내 문제를 설명 할 수 있습니다. 문제는 그래프 패널에 예상대로 표시되지 않는 문제입니다. 이유를 확인하려고합니다. 현재 프로그램이로드 될 때 다음과 같이 나타납니다. LINK1 그래프가 전혀 표시되지 않지만 JComboBox를 드롭 다운하면 나타납니다. LINK2 JComboBox에 항목이 선택되어 닫히면 다시 시작됩니다. LINK3 그러나 대화를 시도 할 때 다시 사라집니다. LINK4 댓글

내 JFrame 또는 JPanel 구성에서 눈에 보이는 오류가 있습니까? 무슨 일이 일어나는지 알아내는 데 도움이되는 제안이 있으십니까?

사이드 노트 : 프레임이 처음로드 될 때 페인트 기능이 세 번 호출됩니다. JComboBox가 열릴 때 한 번 더. JComboBox가 닫힐 때 한 번 더. 그리고 마침내 클릭 할 때 그래프를 사용하지 않을 때가 많습니다.

+0

LINK1 : http://imgur.com/Z3MNx5k – ErichNova

+0

LINK2 : HTTP : //imgur.com/z9Dy6jK – ErichNova

+0

LINK3 : HTTP : // imgur. com/UeJM5EK – ErichNova

답변

2

왜이 라인을 Graphics pg = panel.getGraphics();으로 사용하고 패널에 점을 그리는 데 pg 개체를 사용하고 있습니까? JPanel을 확장하는 다른 클래스를 만들고 paintComponent 메서드를 재정 의하여 필요한 모든 점을 그리는 대신 WGraph 패널에 해당하는 Jpanel 클래스의 객체를 추가하는 것이 좋습니다. 코드를 고려 예를 들어

아래 제공 :

import java.awt.Container; 
import java.awt.BorderLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JComboBox; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

class MyFrame extends JFrame implements ActionListener 
{ 
    private JComboBox jcbShape; 
    private WGraph jpGraph; 
    public MyFrame() 
    { 
     super("GridFrame"); 
    } 
    public void prepareGUI() 
    { 
     Object[] items= {"Line","Rectangle","Circle"}; 
     jcbShape = new JComboBox(items); 
     jpGraph = new WGraph(); 
     Container container = getContentPane(); 
     container.add(jpGraph); 
     container.add(jcbShape,BorderLayout.NORTH); 
     jcbShape.addActionListener(this); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(300,400); 
    } 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     String sShape = (String)jcbShape.getSelectedItem(); 
     jpGraph.setShape(sShape); 
    } 
    public static void main(String[] st) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       MyFrame myFrame = new MyFrame(); 
       myFrame.prepareGUI(); 
       myFrame.setVisible(true); 
      } 
     }); 
    } 
} 
class WGraph extends JPanel 
{ 
    private String sShape = "Line"; 
    public void setShape(String shape) 
    { 
     sShape = shape; 
     repaint(); 
    } 
    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     if ("Line".equalsIgnoreCase(sShape)) 
     { 
      g.drawLine(10, 20, 100, 200); 
     } 
     else if ("Circle".equalsIgnoreCase(sShape)) 
     { 
      g.drawOval(50, 100 , 200, 200); 
     } 
     else if ("Rectangle".equalsIgnoreCase(sShape)) 
     { 
      g.drawRect(10, 20, 150, 200); 
     } 
    } 
} 
+0

그래픽 pg = panel.getGraphics() ; 내 문제가 거짓말을 한 곳이 정확히 어디에 있었습니까. 나는 이것을 필요로하지 않았고 슈퍼 콜에서 p를 사용 했어야했다. – ErichNova