2016-06-21 12 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 

당신은 내가 만들려고 해요 궁금하고 있다면, 그것은 실시간 그림자 엔진의 어떤 종류, 그것은 상당히 느린하지만 그냥 재미 프로젝트의의 화면 깜박임은 내 프로젝트의 많은 부분에있어 실질적인 문제입니다.

미리 감사드립니다. 초기화에서

im=createImage(getWidth(), getHeight()); 

:

+1

1) 당사 코드 애플릿?교사가 지정했기 때문에 [CS 교사가 ** Java 애플릿 교육 **을 중단해야하는 이유] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should?hl=ko)를 참조하십시오. -stop-teaching-java-applets /)를 사용합니다. 2) [Java Plugin support deprecated] (http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) 및 [Plugin-Free Web로 이동] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). –

답변

1

당신은 모든 일을 그릴 별도의 이미지를 만들어야합니다.

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으로 표시되며 기본적으로 이중 버퍼링됩니다. 특히,

  • 인위적 렌더링을 피하기 위해 super.paintComponent()을 호출하십시오.

  • 초기 크기를 설정하려면 getPreferredSize()을 덮어 씁니다.

  • 더 쉬운 디버깅을 위해 상수 시드로 Random을 인스턴스화하십시오.

  • 설명의 applet 태그는 here과 같이 쉽게 테스트 할 수 있습니다.

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