简体   繁体   English

我的重画功能没有调用我的paintComponent

[英]My repaint function is not calling my paintComponent

I have read multiple topics about why repaint does not call the paintComponent, yet I have not been able to combine those answers with my code to get it working. 我已经阅读了多个主题,有关为什么重新绘制为什么不调用paintComponent,但是我仍无法将这些答案与代码结合起来才能正常工作。 I have to do an assignment for school where in a GUI I give the X and Y coordinate for N trucks to be drawn, first one on X and Y and the rest randomly. 我必须做一个学校作业,在GUI中为要绘制的N辆卡车给出X和Y坐标,第一个在X和Y上绘制,其余的则是随机的。

public class TruckMaker extends JPanel {
    public int y1;
    public int x1;
    public static Double circle, circle2, circle3;
    public static java.awt.geom.Rectangle2D.Double rectangle, rectangle2;
    Random random = new Random();

    public TruckMaker() {
        circle = new Ellipse2D.Double();
        circle2 = new Ellipse2D.Double();
        circle3 = new Ellipse2D.Double();
        rectangle = new Rectangle2D.Double();
        rectangle2 = new Rectangle2D.Double();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLUE);
        g2.fill(circle);
        g2.fill(rectangle);
        g2.fill(rectangle2);
        g2.fill(circle2);
        g2.fill(circle3);
        System.out.println("Paint component is being called");
    }

    public void getTrucks(int x, int y, int n) {
        boolean firstTimer = true;
        while (n > 0) {
            if (firstTimer) {
                x1 = x;
                y1 = y;
                rectangle = new Rectangle2D.Double(x1, y1, 50, 50);
                circle = new Ellipse2D.Double(x1 + 25, (y1 + 65), 25, 25);
                rectangle2 = new Rectangle2D.Double(x1 + 65, y1 - 25, 150, 75);
                circle2 = new Ellipse2D.Double(x1 + 140, (y1 + 65), 25, 25);
                circle3 = new Ellipse2D.Double(x1 + 175, (y1 + 65), 25, 25);
                n--;
                firstTimer = false;
            } else {
                x1 = (random.nextInt(750) + 1);
                y1 = y;
                rectangle = new Rectangle2D.Double(x1, y1, 50, 50);
                circle = new Ellipse2D.Double(x1 + 25, (y1 + 65), 25, 25);
                rectangle2 = new Rectangle2D.Double(x1 + 65, y1 - 25, 150, 75);
                circle2 = new Ellipse2D.Double(x1 + 140, (y1 + 65), 25, 25);
                circle3 = new Ellipse2D.Double(x1 + 175, (y1 + 65), 25, 25);
                n--;
            }
        }
        repaint();
    }
}

and here is where I call the TruckMaker 这是我称TruckMaker

public class TruckMain {

    private TruckMaker truck;
    private TruckMain truckmain;
    private final static int FRAME_WIDTH = 750;
    private final static int FRAME_HEIGHT  = 750;
    public final static JButton startButton = new JButton("Start");
    private final static BorderLayout border = new BorderLayout();
    public final static JLabel x = new JLabel("x: ");

    public final static JLabel y = new JLabel("y: ");

    public final static JLabel n = new JLabel("n: ");
    private static JPanel controlPanel = new JPanel();

    private static JTextField textField1 = new JTextField(5);
    private static JTextField textField2 = new JTextField(5);
    private static JTextField textField3 = new JTextField(5);

    public static void main(String[] args){
        TruckMaker truck = new TruckMaker();
        TruckMain truckMain = new TruckMain();
        JFrame frame = new JFrame();
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.add(truck);
        frame.setLayout(border);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        controlPanel.add(x);
        controlPanel.add(textField1);
        controlPanel.add(y);
        controlPanel.add(textField2);
        controlPanel.add(n);
        controlPanel.add(textField3);
        controlPanel.add(startButton);
        startButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                int x1 = Integer.parseInt(textField1.getText());
                int y1 = Integer.parseInt(textField2.getText());
                int n1 = Integer.parseInt(textField3.getText());
                truck.getTrucks(x1,y1,n1);
                }
                });
        frame.add(controlPanel,BorderLayout.SOUTH);
    }
}    

You repaint method seems to work fine; repaint方法似乎可以正常工作; the problem is in your main method and how you construct your frame. 问题出在您的main方法以及如何构造框架上。 It seems like your TruckMaker component is not visible at all, so repaint has nothing to do. 看来您的TruckMaker组件根本不可见,因此repaint无任何事。 Maybe this is just a mistake in your posted code, since I can not even see your controls panel, but instead of adding the components directly to frame , you have to add them to the frame's contentPane : 或许这只是在您发布的代码错误,因为我甚至不能看到你的控制面板,但不是直接添加组件frame ,你必须将它们添加到框架的contentPane

frame.getContentPane().add(truck, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel,BorderLayout.SOUTH);

After this fix, both the control panel and the TruckMaker component are visible and the paintComponent method is being called. 修复之后,控制面板和TruckMaker组件均可见,并且paintComponent方法被调用。


There's also an (unrelated) issue with the types of circle , circle2 , etc. I guess this should be circlecircle2等类型相关的还有一个(不相关的)问题。我想这应该是

public static Ellipse2D.Double circle, circle2, circle3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM