Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
package de.tutorials;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class EllipticalOrbitExample extends JFrame {
volatile BufferStrategy bufferStrategy;
Runnable renderLoop = new Runnable() {
public void run() {
Point center = new Point(200, 150);
int radiusA = 170;
int radiusB = 65;
Image earth = null;
try {
earth = ImageIO.read(new URL("http://www.planeten.ch/images/plch_objekte/Erde.gif"))
.getScaledInstance(45, 45, BufferedImage.SCALE_SMOOTH);
} catch (Exception e) {
e.printStackTrace();
}
double angle = 0;
double step = 0.1;
while (true) {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
int x = (int) (radiusA * Math.sin(angle)) + center.x;
int y = (int) (radiusB * Math.cos(angle)) + center.y;
g.fillOval(center.x, center.y, 5, 5);
g.drawImage(earth, x - 45/2, y - 45/2, null);
bufferStrategy.show();
g.dispose();
try {
TimeUnit.MILLISECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
angle += step;
angle %= 360;
}
}
};
public EllipticalOrbitExample() {
super("EllipticalOrbitExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
createBufferStrategy(2);
this.bufferStrategy = getBufferStrategy();
Executors.newSingleThreadExecutor().execute(renderLoop);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new EllipticalOrbitExample();
}
});
}
}