我如何才能理顺动漫,仍然动作快?
一种技术是如果每步移动较少的像素,将沿着它将采取的路径绘制半透明版本的移动对象。
E.G.一个10像素的举动,战平10%不透明度版本移动的第一像素,20%为第二等
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovingBlock {
MovingBlock() {
final JPanel gui = new JPanel() {
private static final long serialVersionUID = 1L;
int x = 0;
int step = 60;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
x+=10;
Color fg = getForeground();
for (int ii=x-step; ii<x; ii+=4) {
double transparency = (double)(x-ii)/(double)step;
Color now = new Color(
fg.getRed(),
fg.getGreen(),
fg.getBlue(),
(int)(255*(1-transparency)));
g2.setColor(now);
g2.fillRect(ii, 3, 5, 10);
}
if (x>getWidth()) {
x=0;
}
}
};
gui.setBackground(Color.BLACK);
gui.setForeground(Color.GREEN.darker());
gui.setPreferredSize(new Dimension(400,16));
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
gui.repaint();
}
};
Timer timer = new Timer(20, listener);
timer.start();
JFrame f = new JFrame("Moving Block");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(gui);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MovingBlock();
}
});
}
}
为了更好的帮助,尽快发布[SSCCE](http://sscce.org/) – mKorbel