ОБНОВЛЕНИЕ: полукомплексная анимация + свинг таймер = крушение поезда. Конечным источником проблем был таймер Java, либо версия свинга, либо версия утилиты. Они ненадежны, особенно если сравнивать производительность в разных операционных системах. Внедряя заурядный поток, программа работает очень гладко на всех системах. http://zetcode.com/tutorials/javagamestutorial/animation/. Также заметно помогает добавление Toolkit.getDefaultToolkit (). Sync () в метод paintComponent ().
Я написал некоторый код, который плавно анимировался в awt.Applet (но мерцал), а затем я реорганизовал его в java swing. Теперь он не мерцает, но выглядит прерывистым. Я перепутался с таймером, но это не работает. Будем весьма благодарны за любые советы или предложения по плавной анимации компонентов качелей.
import java.util.Random;
import java.util.ArrayList;
import java.awt.event.<em>;
import java.awt.</em>;
import javax.swing.*;
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////</p>
<p>public class Ball extends JApplet{</p>
<pre><code>public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("And so the ball rolls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initContainer(frame);
frame.pack();
frame.setVisible(true);
}
});
}
public static void initContainer(Container container){
GraphicsPanel graphicsPanel = new GraphicsPanel();
MainPanel mainPanel = new MainPanel(graphicsPanel);
container.add(mainPanel);
graphicsPanel.startTimer();
}
@Override
public void init(){
initContainer(this);
}
}
////////////////////////////////////////////////// /////////////////////
////////////////////////////////////////////////// ////////////////////
класс MainPanel расширяет JPanel {
JLabel label = new JLabel ("Частицы");
GraphicsPanel gPanel;
public MainPanel(GraphicsPanel gPanel){
this.gPanel = gPanel;
add(gPanel);
add(label);
}
}
////////////////////////////////////////////////// /////////////////////
////////////////////////////////////////////////// ////////////////////
Класс GraphicsPanel расширяет JPanel, реализует MouseListener {
private ArrayList<Particle> ballArr = new ArrayList<Particle>();
private String state="s"; //"s"=spiral, "p"=particle
private int speed=10; //~20 Hz
private Timer timer;
public GraphicsPanel(){
System.out.println("echo from gpanel");
setPreferredSize(new Dimension(500,500));
timer = new Timer(speed, new TimerListener());
addMouseListener(this);
}
public void startTimer(){
timer.start();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
for (Particle b: ballArr){
g.setColor(b.getColor());
g.fillOval(b.getXCoor(),b.getYCoor(),
b.getTheSize(),b.getTheSize());
}
}
public void mousePressed(MouseEvent e) {
ballArr.add(new Particle(e.getX(), e.getY(), state));
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
for (Particle b: ballArr)
b.move();
setBackground(Color.WHITE);
repaint();
}
}
}
/////////////////////////////////////////////// ///////////////////////////
////////////////////////////////////////////////// ////////////////////////
класс частиц
{
private static int instanceCount; {{СчетчикЭкземпляров ++;}}
private int z = 11, t = 1, u = 1;
private int [] RGB = new int [3];
private int [] randomizeColor = new int [3];
частный двойной радиус, тета;
private int x, y, centerX, centerY, размер, spiralDirection = 1,
ballSizeLowerBound, ballSizeUpperBound,
radiusLowerBound, radiusUpperBound,
mouseInputX, mouseInputY,
radiusXMultiplier, radiusYMultiplier;
Цветной цвет
частное состояние String;
частный Случайный случайный = новый Случайный ();
////////////////////////////////////////////////// /////////////////////////
общественная частица (int x, int y, int centerX, int centerY, int radius,
int theta, int size, Color color) {
this.x = х; this.y = Y; this.centerX = CenterX; this.centerY = centerY;
this.radius = радиус; this.theta = тета; this.size = размер; this.color = цвет;
}
public Particle(int mouseInputX, int mouseInputY, String state){
this.mouseInputX=mouseInputX;
this.mouseInputY=mouseInputY;
this.state=state;
//randomize color
RGB[0] = random.nextInt(252);
RGB[1] = random.nextInt(252);
RGB[2] = random.nextInt(252);
randomizeColor[0] = 1+random.nextInt(3);
randomizeColor[0] = 1+random.nextInt(3);
randomizeColor[0] = 1+random.nextInt(3);
centerX=mouseInputX;
centerY=mouseInputY;
if (state.equals("s")){ //setup spiral state
ballSizeLowerBound=5;
ballSizeUpperBound=18;
radiusLowerBound=0;
radiusUpperBound=50;
radiusXMultiplier=1;
radiusYMultiplier=1;
}
if (state.equals("p")){ //setup particle state
ballSizeLowerBound = 15;
ballSizeUpperBound =20 + random.nextInt(15);
radiusLowerBound = 5;
radiusUpperBound = 15+ random.nextInt(34);
radiusXMultiplier=1 + random.nextInt(3);
radiusYMultiplier=1 + random.nextInt(3);
}
size = ballSizeUpperBound-1; //ball size
radius = radiusUpperBound-1;
if (instanceCount %2 == 0) // alternate spiral direction
spiralDirection=-spiralDirection;
}
///////////////////////////////////////////////////////////////////////////
public int getXCoor(){return centerX+x*spiralDirection;}
public int getYCoor(){return centerY+y;}
public int getTheSize(){return size;}
public Color getColor(){return color;}
//////////////////////////////////////////////////////////////////////////
void move(){
//spiral: dr/dt changes at bounds
if (radius > radiusUpperBound || radius < radiusLowerBound)
u = -u;
//spiral shape formula: parametric equation for the
//polar equation radius = theta
x = (int) (radius * radiusXMultiplier * Math.cos(theta));
y = (int) (radius * radiusYMultiplier * Math.sin(theta));
radius += .1*u;
theta += .1;
//ball size formula
if (size == ballSizeUpperBound || size == ballSizeLowerBound)
t = -t;
size += t;
//ball colors change
for (int i = 0; i < RGB.length; i++)
if (RGB[i] >= 250 || RGB[i] <= 4)
randomizeColor[i] = -randomizeColor[i];
RGB[0]+= randomizeColor[0];
RGB[1]+= randomizeColor[1];
RGB[2]+= randomizeColor[2];
color = new Color(RGB[0],RGB[1],RGB[2]);
}
}