我已经阅读了多个主题,内容为何为何重新绘制不调用paintComponent,但是我仍无法将这些答案与代码结合起来才能正常工作。我必须做一个学校作业,在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();
}
}
这是我称之为
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);
}
}
最佳答案
您的repaint
方法似乎可以正常工作;问题出在您的main
方法以及如何构造框架中。看来您的TruckMaker
组件根本不可见,因此repaint
无关。也许这只是您发布的代码中的一个错误,因为我什至看不到您的控件面板,而是必须将它们直接添加到框架的frame
中,而不是直接将它们添加到contentPane
中:
frame.getContentPane().add(truck, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel,BorderLayout.SOUTH);
修复之后,控制面板和
TruckMaker
组件均可见,并且正在调用paintComponent
方法。circle
,circle2
等类型也存在一个(不相关的)问题。我想这应该是public static Ellipse2D.Double circle, circle2, circle3;
关于java - 我的重画功能没有调用我的paintComponent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27382773/