2016-06-21 66 views
0

我知道這已被問過很多次,但每次訪問問題線程並優先考慮解決方案時,它仍然不適用於我。在JApplet中閃爍的圖形

所以在我製造任何混淆之前,我的問題是我畫的畫面不斷閃爍,這是因爲每次畫屏時,我都會用完全白色的矩形清除屏幕,我畫的東西在矩形上方會閃爍。

這裏是我的代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.geom.*; 
import javax.swing.Timer.*; 

/** 
    * 
    * beschrijving 
    * 
    * @version 1.0 van 16-6-2016 
    * @author 
    */ 

public class shadows3 extends JApplet implements ActionListener{ 
    // Begin variabelen 
    int[] loc = new int[2]; 
    int[][] wall = new int[10][8]; 
    // Einde variabelen 

    public void init() { 
    Container cp = getContentPane(); 
    cp.setLayout(null); 
    cp.setBounds(0, 0, 600, 600); 
    // Begin componenten 
    for (int a=0; a<10; a++) { 
     int tempx = (int) (Math.random()*20)*30; 
     int tempy = (int) (Math.random()*20)*30; 
     wall[a][0] = tempx; 
     wall[a][1] = tempy; 
     wall[a][2] = tempx+30; 
     wall[a][3] = tempy; 
     wall[a][4] = tempx+30; 
     wall[a][5] = tempy+30; 
     wall[a][6] = tempx; 
     wall[a][7] = tempy+30; 
    } // end of for 

    loc[0] = 300; 
    loc[1] = 300; 

    Timer step = new Timer(20, this); 
    step.start(); 
    // Einde componenten 

    } // end of init 
    private int length(int x1, int y1, int x2, int y2) { 
    double distance = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 
    return (int) distance; 
    } 

    // Begin eventmethoden 

    public void paint (Graphics g){ 
    g.setColor(Color.WHITE); 
    g.fillRect(0,0,600,600); 

    int[] xpoints = new int[8]; 
    int[] ypoints = new int[8]; 
    int[] list = new int[3]; 
    for (int a=0; a<5; a++) { 
     for (int b=0; b<4; b++) { 
     if (length(wall[a][b*2],wall[a][b*2+1],loc[0],loc[1])==Math.max(Math.max(length(wall[a][0],wall[a][1],loc[0],loc[1]), 
     length(wall[a][2],wall[a][3],loc[0],loc[1])), 
     Math.max(length(wall[a][4],wall[a][5],loc[0],loc[1]), 
     length(wall[a][6],wall[a][7],loc[0],loc[1])))) { 
      int temp = b; 
      for (int c=0; c<3; c++) { 
      temp += 1; 
      if (temp == 4) { 
       temp = 0; 
      } // end of if 
      list[c] = temp; 
      } // end of for 
     } // end of if 
     } // end of for 
     xpoints[0] = wall[a][list[0]*2 ]; 
     ypoints[0] = wall[a][list[0]*2+1]; 

     xpoints[1] = wall[a][list[1]*2 ]; 
     ypoints[1] = wall[a][list[1]*2+1]; 

     xpoints[2] = wall[a][list[2]*2 ]; 
     ypoints[2] = wall[a][list[2]*2+1]; 

     xpoints[3] = wall[a][list[2]*2 ]+(wall[a][list[2]*2 ]-loc[0])*10000; 
     ypoints[3] = wall[a][list[2]*2+1]+(wall[a][list[2]*2+1]-loc[1])*10000; 

     xpoints[4] = wall[a][list[0]*2 ]+(wall[a][list[0]*2 ]-loc[0])*10000; 
     ypoints[4] = wall[a][list[0]*2+1]+(wall[a][list[0]*2+1]-loc[1])*10000; 

     g.setColor(Color.BLACK); 
     g.fillPolygon(xpoints, ypoints, 5); 
     //g.fillRect(wall[a][0],wall[a][1],30,30); 
    } // end of for 
    } 

    //@Override 
    public void actionPerformed(ActionEvent e){ 
    loc[0] += 4; 
    loc[1] += 2; 

    repaint(); 
    } 
    // Einde eventmethoden 

} // end of class shadows3 

如果你想知道什麼,我試圖創建,它是某種實時陰影引擎,這是相當緩慢的,但它只是一個有趣的項目中,雖然對於我的許多項目來說,屏幕閃爍是一個真正的問題。

非常感謝提前!

+1

1)爲什麼代碼的小程序?如果是由於老師指定它,請將它們轉介給[爲什麼CS教師應該**停止**教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop教學-java的小應用程序/)。 2)請參閱[不支持Java插件的支持](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/)和[轉移到無插件Web]( https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free)。 –

回答

1

你需要創建一個單獨的圖像上繪製所有的東西:

im=createImage(getWidth(), getHeight()); 
在初始化

Graphics g2=im.getGraphics(); 
g2.setColor(Color.WHITE); 
g2.fillRect(0,0,600,600); 

,並繼續與G2取代所有G指令:

g2.setColor(Color.BLACK); 
g2.fillPolygon(xpoints, ypoints, 5); 

,那麼你在圖像上畫出G:

g.drawImage(im, 0, 0, this); 

在您的油漆

然後你有更新方法:

public void update(Graphics g) { paint(g); } 
+0

非常感謝,我也會試試這個 – Lolslayer

2

因爲您應該將migrating設置爲無插件環境,請考慮通過部署的hybrid。以下示例顯示您的內容爲JPanel,默認情況下爲雙緩衝。特別是,

image

測試:

$ javac ShadowApplet.java ; appletviewer ShadowApplet.java 

代碼:

//<applet code="ShadowApplet.class" width=600 height=600></applet> 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 
import javax.swing.JApplet; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

/** 
* @see https://stackoverflow.com/a/37958844/230513 
*/ 
public class ShadowApplet extends JApplet { 

    private static final Random R = new Random(42); 

    @Override 
    public void init() { 
     EventQueue.invokeLater(() -> { 
      add(new ShadowPanel()); 
     }); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new ShadowApplet()::display); 
    } 

    private void display() { 
     JFrame f = new JFrame("Test"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new ShadowPanel()); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class ShadowPanel extends JPanel implements ActionListener { 

     int[] loc = new int[2]; 
     int[][] wall = new int[10][8]; 

     public ShadowPanel() { 
      this.setLayout(null); 
      this.setBackground(Color.WHITE); 
      for (int a = 0; a < 10; a++) { 
       int tempx = R.nextInt(20) * 30; 
       int tempy = R.nextInt(20) * 30; 
       wall[a][0] = tempx; 
       wall[a][1] = tempy; 
       wall[a][2] = tempx + 30; 
       wall[a][3] = tempy; 
       wall[a][4] = tempx + 30; 
       wall[a][5] = tempy + 30; 
       wall[a][6] = tempx; 
       wall[a][7] = tempy + 30; 
      } 
      loc[0] = 300; 
      loc[1] = 300; 
      Timer step = new Timer(20, this); 
      step.start(); 
     } 

     private int length(int x1, int y1, int x2, int y2) { 
      double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); 
      return (int) distance; 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      int[] xpoints = new int[8]; 
      int[] ypoints = new int[8]; 
      int[] list = new int[3]; 
      for (int a = 0; a < 5; a++) { 
       for (int b = 0; b < 4; b++) { 
        if (length(wall[a][b * 2], wall[a][b * 2 + 1], loc[0], 
         loc[1]) == Math.max(Math.max(length(wall[a][0], wall[a][1], loc[0], loc[1]), 
          length(wall[a][2], wall[a][3], loc[0], loc[1])), 
          Math.max(length(wall[a][4], wall[a][5], loc[0], loc[1]), 
           length(wall[a][6], wall[a][7], loc[0], loc[1])))) { 
         int temp = b; 
         for (int c = 0; c < 3; c++) { 
          temp += 1; 
          if (temp == 4) { 
           temp = 0; 
          } 
          list[c] = temp; 
         } 
        } 
       } 
       xpoints[0] = wall[a][list[0] * 2]; 
       ypoints[0] = wall[a][list[0] * 2 + 1]; 

       xpoints[1] = wall[a][list[1] * 2]; 
       ypoints[1] = wall[a][list[1] * 2 + 1]; 

       xpoints[2] = wall[a][list[2] * 2]; 
       ypoints[2] = wall[a][list[2] * 2 + 1]; 

       xpoints[3] = wall[a][list[2] * 2] + (wall[a][list[2] * 2] - loc[0]) * 10000; 
       ypoints[3] = wall[a][list[2] * 2 + 1] + (wall[a][list[2] * 2 + 1] - loc[1]) * 10000; 

       xpoints[4] = wall[a][list[0] * 2] + (wall[a][list[0] * 2] - loc[0]) * 10000; 
       ypoints[4] = wall[a][list[0] * 2 + 1] + (wall[a][list[0] * 2 + 1] - loc[1]) * 10000; 

       g.setColor(Color.BLACK); 
       g.fillPolygon(xpoints, ypoints, 5); 
       g.fillRect(wall[a][0], wall[a][1], 30, 30); 
      } 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      loc[0] += 4; 
      loc[1] += 2; 
      repaint(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(600, 600); 
     } 
    } 
} 
+0

謝謝,我一定會看看 – Lolslayer