私はJavaでプログラムを書きたいと思っています...ウィンドウの形状(JFrame)を一連のPNG画像(透明な背景を持つ)に設定したいです。(実際には、ウィンドウの形状を連続的に変化させ、アニメーションのように見せたいです!)そして、ファイルから画像を読み取り、それらを配列に保存してから、クラス GeneralPath を使用してアニメーションの領域を取得します文字 (png 画像内)、areaArray に保存します。
すべての作業が完了したら、ペイント スレッドを開始します。それはうまく機能します...しかし、時々ウィンドウが点滅しました(ああ...つまり、ちらつきが発生したということですが、点滅したときに見た背景色は透明で、デスクトップの壁紙を見ることができました!)。
ちらつき/フラッシュを二度と見たくないのですが、誰か助けてくれますか? ありがとう!
PS : 下手な英語でごめんなさい!
public class JCustomFrame extends JFrame implements Runnable
{
private final int max_frame=18; //Here is the max numbers of my png images
private BufferedImage[] BufImageArray; //The array to save all the png images
private BufferedImage nowImage; //Save the image to be drawn in this turn
private int counter; //Indicate which png image to be drawn
private Area[] areaArray; //Save the shapes of the animated character in each png image
public void run()// a thread function to paint the frame continual
{
while(true){
if(counter==max_frame)counter=0;
nowImage=BufImageArray[counter];
setShape(areaArray[counter]);
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){
System.out.println("Thread.sleep error!");
}
counter++;
}
}
public JCustomFrame()
{
super();
setUndecorated(true);
setBackground(new Color(0,0,0,0));
counter= 0;
//...some codes here
new Thread(this).start();
}
public void paint(Graphics graphic)
{
graphic.drawImage(nowImage,0,0,this);
}
}
以下は、プログラムを実行するためのサンプル コードです。
import javax.swing.*;
public class MainFrame
{
public static void main(String[] args)
{
JCustomFrame myFrame = new JCustomFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,400);
myFrame.setVisible(true);
return ;
}
}
上記の「pngファイル名」と「max_frame」の2行を変更して、新しい画像ファイルを適用しました。同じプログラムを OpenSuse ではなく Windows に配置すると、(ちらつきなしで) 非常にうまく動作することがわかりました。ここでは、ソースのすべてのパックをアップロードします (画像ファイルを含めます)。 ここに私のコードがあります。
再度、感謝します。
================================================== ================================
提案してくれたアンドリュー・トンプソンに感謝します。
今回は、問題に関係のないコードを削除して、状況を示す gif を貼り付けてみます。上記のコードは実行できませんが、リンクのソース コードはうまく機能します。PSちらつき/フラッシュはランダムなフレームで発生し、gifが示すものと完全に同じではありません. (なぜなら、透明なパネルを gif 画像に固定シーケンスでしか追加できなかったからです)
ありがとう!!