Моя программа представляет собой анимированный GUI, который рандомизирует начальную позицию и скорость из 3 изображений. Как только изображение достигает края кадра, скорость меняется на противоположную.
Эта часть работает, но иногда не все 3 изображения появляются в пределах рамки для их начальной позиции. Как мне убедиться, что они всегда начинаются в границах кадра?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*; // for jar file
/**
* AnimationExampleLameAndBuggy.java
* @author Suha N.
* Description:
* Date: April 14, 2020
* @version 1.00 2015/11/7
*/
public class AnimationUI extends JFrame implements ActionListener {
// instance variable for components
private JPanel drawingPanel, controlPanel;
private JButton btnStart;
// instance variables for picture arrays, speed and positions
private TextPicture myTitle;
private ImagePicture pic1, pic2, pic3;
private int xPos1, yPos1, xPos2, yPos2, xPos3, yPos3;
private int xVel1, yVel1, xVel2, yVel2, xVel3, yVel3;
private Timer timer; // animation timer
private int width, height; // height and width of frame
private Die d1, d2, d3; // dice to randomize location and speeds
/**
* Creates a new instance of AnimationExample.
*/
public AnimationUI(){
super("My Simple Animation Example"); // title for the frame
// initialize the width of the frame
width = 400;
height = 600;
// create the dice for the positions and speed
d1 = new Die(width);
d2 = new Die(height);
d3 = new Die();
setLayout(null); // layout for my frame
controlPanel = new JPanel(); // panel for buttons
drawingPanel = new JPanel(); // used only to set the boundaries to draw
btnStart = new JButton("Let's Move"); // create button
// set the size and position of the drawing panel
drawingPanel.setBounds(0, 0, width, height - 100);
drawingPanel.setLayout(null); // set layout of drawing panel
// add the drawing panel to the frame
add(drawingPanel);
// set the size and position of the control panel
controlPanel.setBounds(0, height - 100, width, 100);
// add the drawing panel to the frame
add(controlPanel);
// add components to panels
controlPanel.add(btnStart); //button to control panel
// for each picture object, add the image
// randomize its initial position using the Die class
// and place the object on the drawingPanel
// PICTURE 1
pic1 = new ImagePicture (new ImageIcon("minion.png")); // create the first image
pic1.setBounds(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight()); // set its boundaries
drawingPanel.add(pic1); // add the image to the panel
// roll the dice for the first image
d1.rollDie();
d2.rollDie();
d3.rollDie();
// place the image based on the first two dice
xPos1 = d1.getValue();
yPos1 = d2.getValue();
pic1.setxPos(xPos1);
pic1.setyPos(yPos1);
// set the speed based on the value to the third die
xVel1 = d3.getValue();
yVel1 = d3.getValue();
// PICTURE 2
pic2 = new ImagePicture (new ImageIcon("minion.png"));
pic2.setBounds(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight());
drawingPanel.add(pic2);
// roll the dice for the second image
d1.rollDie();
d2.rollDie();
d3.rollDie();
xPos2 = d1.getValue();
yPos2 = d2.getValue();
pic2.setxPos(xPos2);
pic2.setyPos(yPos2);
xVel2 = d3.getValue();
yVel2 = d3.getValue();
// PICTURE 3
pic3 = new ImagePicture (new ImageIcon("minion.png"));
pic3.setBounds(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight());
drawingPanel.add(pic3); // add the image to the panel
// roll the dice for the third image
d1.rollDie();
d2.rollDie();
d3.rollDie();
xPos3 = d1.getValue();
yPos3 = d2.getValue();
pic3.setxPos(xPos3);
pic3.setyPos(yPos3);
xVel3 = d3.getValue();
yVel3 = d3.getValue();
timer = new Timer(20, this); //creates a timer and this class as the listener. set to fire every 20ms
timer.setInitialDelay(5); //set the initial delay to 5 ms before it starts
// add button as a listener in this frame
btnStart.addActionListener(this);
timer.addActionListener(this);
// set size and location of frame
setSize(width, height);
setLocation(100, 100);
// make frame visible
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// method for actions events
public void actionPerformed (ActionEvent e) {
if (e.getSource()==btnStart) {
// if btn is clicked start timer
timer.start();
}
else if (e.getSource()==timer) {
// if the timer fires, go through each of the
// pictures and set its new position
// PICTURE 1
xPos1 += xVel1;
yPos1 += yVel1;
// change the position of the first image
pic1.setxPos(xPos1);
pic1.setyPos(yPos1);
//check for the boundaries to reverse direction
if (xPos1 > (drawingPanel.getWidth() - pic1.getMyWidth())) {
xVel1*=-1; // reverse speed
}
else if (xPos1 < 0 ) {
xVel1*=-1; // reverse speed
}// if x pos
if (yPos1 > (drawingPanel.getHeight() - pic1.getMyHeight())) {
yVel1*=-1; // reverse speed
}
else if (yPos1 < 0) {
yVel1*=-1; // reverse speed
}// if ypos
// PICTURE 2
xPos2 += xVel2;
yPos2 += yVel2;
// change the position of the second image
pic2.setxPos(xPos2);
pic2.setyPos(yPos2);
if (xPos2 > (drawingPanel.getWidth() - pic2.getMyWidth())) {
xVel2 *=-1;
}
else if (xPos2 < 0 ) {
xVel2*=-1;
}
if (yPos2 > (drawingPanel.getHeight() - pic2.getMyHeight())) {
yVel2*=-1;
}
else if (yPos2 < 0) {
yVel2*=-1;
}
// PICTURE 3
xPos3 += xVel3;
yPos3 += yVel3;
// change the position of the third image
pic3.setxPos(xPos3);
pic3.setyPos(yPos3);
if (xPos3 > (drawingPanel.getWidth() - pic3.getMyWidth())) {
xVel3*=-1;
}
else if (xPos3 < 0 ) {
xVel3*=-1;
}
if (yPos3 > (drawingPanel.getHeight() - pic3.getMyHeight())) {
yVel3*=-1;
}
else if (yPos3 < 0) {
yVel3*=-1;
}
}// if timer
}// actionPerformed
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AnimationUI animation = new AnimationUI();
}
}