2014-02-07 27 views
0

我一直在爲AP Java在學校的旋轉線項目工作。所有的支持類都完成了(通過我的合作伙伴),但我還沒有完成我的顯示,因爲我的paintComponent()方法根本不工作。爲什麼repaint()沒有到達paintComponent()方法?

我已經尋求幫助,沒有人能夠找到錯誤。我所需要做的就是將這些多邊形畫在屏幕上,剩下的就可以完成。

這是我到目前爲止有:

imports... 

public class Display extends JFrame { 


protected int screenHeightD = 600; 
protected int screenWidthD = 700; 
private ArrayList <Rotator> planesOnBoard = new ArrayList(); 
private Timer timer; 
private boolean timerOn = true; 
private int timerDuration = 300; 
private int rotateDegValue = 5; 
PlaneFacilitator painter = new PlaneFacilitator(); 
JFrame display = new JFrame ("Rotating Line"); 



public class PlaneFacilitator extends JComponent { 

    // takes the double arrays sent by the getXArray/getYArray 
    public int [] convertXcrds (double [] xCrdsD) { 

     int [] xCrds = new int[xCrdsD.length]; 

     for (int i = 0 ; i < xCrdsD.length - 1; i++) { 
      double e = xCrdsD [i]; 
      xCrds [i] = (int) Math.round (e); 
     } 
     return xCrds; 
    } 
    public int [] convertYcrds (double [] yCrdsD) { 

     int [] yCrds = new int[yCrdsD.length]; 

     for (int i = 0 ; i < yCrdsD.length - 1; i++) { 
      double e = yCrdsD [i]; 
      yCrds [i] = (int) Math.round (e); 
     } 
     return yCrds; 
    } 

    // paints the entire set of planes 
    public void paintComponent (Graphics g){ 

     System.out.println("Painted"); 
     planesOnBoard.add(0, new Rotator (screenWidthD/2, screenHeightD/2, new Dimension (screenHeightD, screenWidthD))); 
     planesOnBoard.get(0).addPolygon(Color.black, 1, 0, true); 


     for (Rotator target : planesOnBoard) { 

      for (int polyIndex = 0; polyIndex < target.getNumPolygons(); polyIndex++) { 

       double[] xCrdsD = target.getXArray(polyIndex); 
       double[] yCrdsD = target.getYArray(polyIndex); 

       g.setColor(target.getColor(polyIndex)); 
       g.drawPolyline(convertXcrds(xCrdsD), convertYcrds(yCrdsD), xCrdsD.length); 

      } 

      g.setColor(Color.green); 
      g.drawRect(target.getXOrigin() - 2, target.getYOrigin() - 2, 5, 5); 

     } 

    } 

} 


// ctor for display, creates the frame... 
public Display() { 

    display.setLayout(null); 
    display.setSize(screenWidthD, screenHeightD); 
    display.setTitle("Rotating Line"); 

    display.add(painter); 
    System.out.println(screenWidthD + " " + screenHeightD); 

    JButton startB = new JButton ("Start"); 
    JButton pauseB = new JButton ("Pause"); 

    ActionListener pauseBL = new pauseB(); 
    pauseB.addActionListener(pauseBL); 
    ActionListener startBL = new startB(); 
    startB.addActionListener(startBL); 
    startB.setBounds(10, 10, 150, 20); 
    pauseB.setBounds(170, 10, 150, 20); 
    display.add(startB); 
    display.add(pauseB); 

    display.addMouseListener(new DropDownMenuListener()); 

    display.getContentPane().setBackground(Color.WHITE); 
    display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    display.setVisible(true); 
    display.setBounds(0, 0, screenWidthD, screenHeightD); 
    display.setResizable(false); 
    display.getContentPane().repaint(); 

} 

// creates a dropdown menu that appears whenever you click on the screen. 
class DropDownMenu extends JPopupMenu { 
    public DropDownMenu(){ 
     JMenuItem addPent = new JMenuItem ("Add a Pentagon"); 
     JMenuItem addQuad = new JMenuItem ("Add a Quadrilateral"); 
     JMenuItem addTri = new JMenuItem ("Add a Triangle"); 
     JMenuItem addLine = new JMenuItem ("Add a Line"); 
     JMenuItem reverseRot = new JMenuItem ("Reverse the Rotation of the plane"); 
     JMenuItem addNewPlane = new JMenuItem ("Add a new plane here"); 

     add(addPent); 
     add(addQuad); 
     add(addTri); 
     add(addLine); 
     add(reverseRot); 
     add(addNewPlane); 

     addNewPlane.addActionListener(new addPlaneListener()); 
     addLine.addActionListener(new addLineListener()); 
     addTri.addActionListener(new addTriListener()); 
     addQuad.addActionListener(new addQuadListener()); 
     addPent.addActionListener(new addPentListener()); 
     reverseRot.addActionListener(new addReverseRotateListener()); 

    } 
} 


// Lots an lots of listeners... 
int xCrdMenu; 
int yCrdMenu; 
class DropDownMenuListener extends MouseAdapter { 
    public void mousePressed(MouseEvent e){ 
     if (e.isPopupTrigger()) 
      xCrdMenu = e.getX(); 
      yCrdMenu = e.getY(); 
      doPop(e); 
    } 

    public void mouseReleased(MouseEvent e){ 
     if (e.isPopupTrigger()) 
      doPop(e); 
    } 

    private void doPop(MouseEvent e){ 
     DropDownMenu menu = new DropDownMenu(); 
     menu.show(e.getComponent(), e.getX(), e.getY()); 
    } 
} 

// defines the behavior of the startB 
// starts the timer...rotates x degrees each time 
class startB implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     timer = new Timer(); 
     timerOn = true; 

     (timer).scheduleAtFixedRate(new TimerTask() 
     { 
      public void run() { 

       System.out.println("Time"); 

       for (Rotator target : planesOnBoard) { 

        for (int polyIndex = 0; polyIndex < target.getNumPolygons(); polyIndex++) { 

         target.rotate (rotateDegValue, polyIndex); 

        } 

       } painter.repaint(); 

      } 
     } 
     , (long) timerDuration, (long) timerDuration); 
    } 

} 

// defines the behavior for the pause button 
class pauseB implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     if(timerOn == true){ 
      System.out.println("Pause"); 
      timer.cancel();; 
      timerOn = false; 
     } 
    } 
} 

class addPlaneListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     //JFrame source = (JFrame) e.getSource(); 
     Plane added = new Plane (getContentPane().getY(), getContentPane().getX(),getContentPane().getY(),getContentPane().getX()); 
     planesOnBoard.add(new Rotator (added)); 
     added.setOrigin(xCrdMenu, yCrdMenu); 

     painter.repaint(); 
     System.out.println("Plane"); 
    } 
} 

class addLineListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     planesOnBoard.get(0).addPolygon(new Polygon (Color.black, 1, 180, true, getContentPane().getY(), getContentPane().getX(),getContentPane().getY(),getContentPane().getX())); 
     painter.repaint(); 
     System.out.println("Line"); 
    } 
} 

class addTriListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     planesOnBoard.get(0).addPolygon(Color.black, 3, 60, true); 
     painter.repaint(); 
    } 
} 

class addQuadListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     planesOnBoard.get(0).addPolygon(Color.black, 4, 90, true); 
     painter.repaint(); 
    } 
} 

class addPentListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     planesOnBoard.get(0).addPolygon(Color.black, 5, 108, true); 
     painter.repaint(); 
    } 
} 

class addReverseRotateListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     rotateDegValue *= -1; 
    } 
} 


public static void main (String [] args) { 

    Display experiment = new Display(); 

} 
} 
+0

它可能不是改正錯誤,但作爲良好做法的情況下,加覆蓋方法的頂部有'@ Override'註解(比如'paintComponent')。此外,使用空佈局幾乎不是一個好主意...... – Sinkingpoint

回答

2

我的猜測是因爲這個...

display.setLayout(null); 

基本上painter0x0默認大小(和位置)。這意味着RepaintManager可能會查看和思考自己,這將浪費時間繪製,因爲無論如何都不會顯示,並丟棄任何重繪請求。

你應該使用合適的佈局管理器,像BorderLayout例如...

看看Laying Out Components Within a Container

+0

對不起,回覆遲,謝謝你的幫助! – Ian

相關問題