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 befo
Because you should be migrating to a plugin-free environment, consider a hybrid deployed via java-web-start. The example below displays your content in a JPanel, which is double buffered by default. In particular,
Invoke super.paintComponent() to avoid rendering artifacts.
Override getPreferredSize() to establish the initial size.
Instantiate Random with a constant seed for easier debugging.
The applet tag in a comment allows easy testing, as shown here.
Test:
$ javac ShadowApplet.java ; appletviewer ShadowApplet.java
Code:
//
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);
}
}
}