图形在JApplet中闪烁 [英] Graphics flashing in JApplet

查看:122
本文介绍了图形在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实现ActionListener {
//开始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);
//开始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;
} //结尾为

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

定时器步骤=新定时器(20,this);
step.start();
// Einde componenten

} //结束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;
}

//开始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],壁[A] [b * 2 + 1],在上述[0],在上述[1])== Math.max(Math.max(长度(壁并[a] [0],壁并[a] [1],loc [0],loc [1]),
length(wall [a] [2],wall [a] [3],loc [0],loc [1])),
Math.max(长度(墙[a] [4],墙[a] [5],loc [0],loc [1]),
长度(墙[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;
} //结束if
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);
} //结束
}

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

repaint();
}
// Einde eventmethoden

} //课程结束shadow3

如果你想知道我想要创建什么,它是某种实时阴影引擎,它相当慢但它只是一个有趣的项目,屏幕闪烁是一个真正的问题,虽然对许多人来说我的项目。



非常感谢提前!

解决方案

因为你应该



测试:

  $ javac ShadowApplet.java; appletviewer ShadowApplet.java 

代码:

  //< applet code =ShadowApplet.classwidth = 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
* /
公共类ShadowApplet扩展JApplet {

私有静态最终随机R =新随机(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);
}

私有静态类ShadowPanel扩展JPanel实现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;
定时器步骤=新定时器(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],墙[a] [b * 2 + 1],loc [0],
loc [1])== Math.max(Math.max(长度(墙[a] [0 ],墙[a] [1],loc [0],loc [1]),
长度(墙[a] [2],墙[a] [3],loc [0],loc [ 1])),
Math.max(长度(墙[a] [4],墙[a] [5],loc [0],loc [1]),
长度(墙[ 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);
}
}
}


I know that this has been asked many times before, but every time I visit the question thread and excecute the posed solution, it still doesn't work for me.

So before I create any confusion, the problem I have is that the screen I draw is constantly flashing, this is because every time I draw the screen, I clear the screen with a completely white filled rectangle, the things I draw over the rectangle will flash.

Here is my code:

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

If you're wondering what I'm trying to create, it's some kind of real-time shadows engine, it's fairly slow but it's just a fun project, the screen flashing is a real problem though for many of my projects.

Many thanks in advance!

解决方案

Because you should be migrating to a plugin-free environment, consider a hybrid deployed via . The example below displays your content in a JPanel, which is double buffered by default. In particular,

Test:

$ javac ShadowApplet.java ; appletviewer ShadowApplet.java

Code:

//<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);
        }
    }
}

这篇关于图形在JApplet中闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆