Рисую круг, который должен рандомно кататься по полю. А он не отрисовывается, хотя уже всё проверил. В Swing пока мало опыта.
public void initUI() {
JFrame frame = new JFrame(MovingCircle.class.getSimpleName());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(null);
JPanel circle = new JPanel() {
public void paint(Graphics graphics) {
graphics.drawOval(200, 200, 150, 150);
graphics.setColor(Color.black);
}
};
frame.add(circle);
frame.setSize(WIDTH, HEIGHT);
xCircle = getNextSpeed() * (random.nextBoolean() ? 1 : -1);
yCircle = getNextSpeed() * (random.nextBoolean() ? 1 : -1);
Timer t = new Timer(1000 / STEPS_OF_IMAGE_PER_SECOND, (ActionEvent e) -> {
xcCircle += xCircle;
ycCircle += yCircle;
if (xcCircle + circle.getWidth() > frame.getContentPane().getWidth()) {
xcCircle = frame.getContentPane().getWidth() - circle.getWidth();
xCircle = -getNextSpeed();
} else if (xcCircle < 0) {
xcCircle = 0;
xCircle = getNextSpeed();
}
if (ycCircle + circle.getHeight() > frame.getContentPane().getHeight()) {
ycCircle = frame.getContentPane().getHeight() - circle.getHeight();
yCircle = -getNextSpeed();
} else if (ycCircle < 0) {
ycCircle = 0;
yCircle = getNextSpeed();
}
circle.setLocation((int) xcCircle, (int) ycCircle);
});
frame.setVisible(true);
t.start();
}
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости