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.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author Tom
*
*/
public class PrintComponentToImageExample extends JFrame {
JPanel panel = new JPanel() {
List<Point> points = new ArrayList<Point>();
{
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
}
});
setPreferredSize(new Dimension(640,480));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Point lastPoint = null;
for (Point point : points) {
if (lastPoint != null) {
g.drawLine(lastPoint.x, lastPoint.y, point.x, point.y);
}
lastPoint = point;
}
this.repaint();
}
};
JButton btn = new JButton("print to image");
public PrintComponentToImageExample() {
super("PrintComponentToImageExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedImage bufferedImage = new BufferedImage(panel
.getWidth(), panel.getHeight(),
BufferedImage.TYPE_INT_RGB);
panel.paint(bufferedImage.getGraphics());
try {
ImageIO
.write(bufferedImage, "JPEG",
new File("c:/out.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
add(panel, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new PrintComponentToImageExample();
}
}