2016-12-13 7 views
0

私はいくつかの変化する色と動く形を持つアニメーションスクリーンセーバーを作ろうとしています。また、一種のトレイル効果を持たせたいと思います(設定しないと、あなたの背景色)。これまでに一度この効果を達成しましたが、どうやってそれをやったのか分かりません。現在のところ、結果は灰色の背景を横切って色が変化する動く四角形です。 ScreenSaver.javaJPanelの背景を更新しないようにするにはどうすればよいですか?

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JPanel; 
import javax.swing.Timer; 

public class ScreenSaver extends JPanel 
{ 
    public static Component sc; 
    public int delay = 1000/2; 
    public int state = 0; 
    public int re = 255; 
    public int gr = 0; 
    public int bl = 0; 
    public int d = 1; 
    ScreenSaver() 
    { 
     ActionListener counter = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       updateUI(); 
       repaint(); 
      } 
     }; 
     new Timer(delay, counter).start(); 
     System.out.println("this is the secret screensaver that appears after 30 seconds of no mouse activity");//i have this line here so it doesn't print every time the screen updates 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     this.setBackground(null); 
     if (state == 0) 
     { 
      gr++; 
      if(gr == 255) 
       state = 1; 
     } 
     if (state == 1) 
     { 
      re--; 
      if(re == 0) 
       state = 2; 
     } 
     if (state == 2) 
     { 
      bl++; 
      if(bl == 255) 
       state = 3; 
     } 
     if (state == 3) 
     { 
      gr--; 
      if(gr == 0) 
       state = 4; 
     } 
     if (state == 4) 
     { 
      re++; 
      if(re == 255) 
       state = 5; 
     } 
     if (state == 5) 
     { 
      bl--; 
      if(bl == 0) 
       state = 0; 
     } 
     g.setColor(new Color(re, gr, bl)); 
     d++; 
     g.fillRect(d, 50, 50, 50); 
    } 
    public static void main(String[] args) 
    { 
     //ScreenSaver sc = new ScreenSaver(); 
    } 
} 

Main.java:(ScreenSaver.javaは、このファイルを使用してオブジェクトと呼ばれている)

import java.awt.Color; 
import java.awt.Graphics; 
//import java.util.Random; 

//import javax.swing.AbstractAction; 
//import javax.swing.Action; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionListener; 

import javax.swing.Timer; 

public class FinalProject extends JPanel implements MouseMotionListener 
{ 
    public int delay = 1000/2; 
    public boolean screenActive = true; 
    public int why = 1; 
    FinalProject() 
    { 
     ActionListener counter = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       updateUI(); 
       repaint(); 
      } 
     }; 
     new Timer(delay, counter).start(); 
    } 
    static JFrame jf = new JFrame(); 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     this.setBackground(Color.BLACK); 
     if (screenActive) 
     { 
      why++; 
     } 
     if (why == 6) 
     { 
      why = 0; 
      System.out.println("inactivity detected"); 
      screenActive = false; 
      ScreenSaver sc = new ScreenSaver(); 
      jf.add(sc); 
     } 
    } 

    public static void main(String[] args) throws InterruptedException 
    { 
     System.out.println("Welcome to Computer Simulator 0.1"); 
     System.out.println("this is the main screen"); 
     FinalProject e = new FinalProject(); 
     //ScreenSaver sc = new ScreenSaver(); 
     jf.setTitle("game"); 
     jf.setSize(500,500); 
     //jf.setUndecorated(true); 
     //jf.setBackground(new Color(1.0f,1.0f,1.0f,0.5f)); 
     jf.setVisible(true); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //jf.add(new ScreenSaver()); 
     jf.add(e); 
    } 

    @Override 
    public void mouseDragged(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseMoved(MouseEvent MOUSE_MOVED) 
    { 

    } 
} 
+0

'repaint'を空のメソッドに置き換え、' update'を直接呼び出すと、あなたが望む効果を得られますか? –

+0

@DanArmstrong、update()を直接呼び出さないでください。 Swingには、オーバーライドすべきではない明確な画層があります。 1つの方法として、このアプローチはダブルバッファリングを除去する。 – camickr

+0

まさに私がそれを提案した理由。彼らは非標準的な効果を得るためにSwingをハックすることを望んでいるようです。しかし、ハックや虐待はさておき、以下の答えは、BufferedImagesに行ったり、ペイント上のアイテムを再描画する方法です。 –

答えて

2

(のupdateUIを起動しないでください)。コンポーネントには、コンポーネント自体を再描画させるために必要なのはrepaint()です。

BufferedImageに描画を行い、BufferedImageを使用してJLabelに追加するImageIconを作成する方法もあります。

もう1つの方法は、塗りつぶしたいオブジェクトのリストを保持し、コンポーネントが再描画されるたびにリストを反復することです。

作業の例と、どちらの方法を使用するかについての分析については、Custom Painting Approchesを参照してください。

関連する問題