为什么repaint()没有到达paintComponent()方法?

时间:2014-02-07 01:30:02

标签: java eclipse swing

我一直在学校为AP Java做一个旋转线项目。所有支持课程都已完成(由我的合作伙伴完成),但由于我的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();

}
}

1 个答案:

答案 0 :(得分:2)

我的猜测是因为......

display.setLayout(null);

基本上painter的默认大小(和位置)为0x0。这意味着RepaintManager可能会查看并思考自己,这将浪费时间绘制,因为无论如何都不会显示任何重绘请求。

您应该使用适当的布局管理器,例如BorderLayout ......

查看Laying Out Components Within a Container