让我说我有一个图像。我把图像放在一个JPanel中,并在JFrame中添加JPanel。图像从框架底部移动到框架顶部,同时使用AffineTransform
缩小其大小。该变量使用线程更改。通过线程缩放图像 - JAVA
所以这里的下面的代码:
public class SplashScreen extends JFrame{
Image img1;
int w=1,h=1;
int x=0,y=0;
Thread th = new Thread(new Runnable() {
@Override
public void run() {
while(true){
w-=0.05;
h-=0.05;
y-=2;
x+=1;
if(y==-100){
new MainMenu_BlueJay().setVisible(true);
dispose();
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
JPanel p = new JPanel();
public SplashScreen(){
setLayout(new BorderLayout());
p.setPreferredSize(new Dimension(900,600));
p.setBackground(Color.black);
p.setLayout(new GridLayout());
add(p);
setTitle("BlueJay");
setSize(900,600);
getContentPane().setBackground(Color.black);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
th.start();
requestFocus();
setFocusable(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
img1 = new ImageIcon("images/Intro/BJ Production 2013.png").getImage();
AffineTransform at = new AffineTransform();
at.scale(w,h);
g2d.setTransform(at);
g2d.drawImage(img1, x, y, p);
}
public static void main(String[] args) {
new SplashScreen();
}
但是我从代码获得以上只是黑屏。怎么了?无论如何,如果我不使用AffineTransform
功能(只是从下往上移动),图像会显示并移动,但是框架会快速闪烁(闪烁)。
任何想法来解决这个问题,所以我可以移动图像,同时减小其大小,也解决了闪烁/快速闪烁帧?
首先,你的变量线程访问应该是volatile的。其次,线程内部的paint调用必须与SwingUtilities.invokeLater() – morpheus05
一起围绕。那么如果不使用线程,如何减少变量的值? – noobprogrammer