我寫了兩個類FullScreen
,其中包含邏輯截圖和FullScreenGUI
,其中包含用於模擬照片點擊效果的邏輯。Java:獲取照片點擊後效果截圖
照片點擊效果基本上是在短時間內閃爍屏幕,比如截圖後50ms。它是通過用不透明度爲1%的JFrame
覆蓋整個屏幕而生產的。背景變成白色,然後不透明度從1%變爲100%,保持爲50ms,然後回到1%。
的FullScreen
構造採用兩個參數,一個用於的次取數截圖,而另一個用於在之間的持續時間。
的FullScreenGUI
構造一個JFrame
,它最大化,設置背景爲白色。當調用fire()
方法時,會根據需要更改不透明度以生成效果。
問題:
使用下面的代碼,我能夠生產首次截圖拍攝的效果,但對隨後的點擊。假設,FullScreen
構造函數被調用與所述參數(4,2)
(即每次取以2秒的持續時間4次點擊),則該效果是很好的產生爲第一次點擊而不是剩餘的3次點擊。 FullScreenGUI
的JFrame
似乎沒有出現,所以效果不明顯。我曾嘗試JFrame.setAlwaysOnTop(true)
,JFrame.toFront()
但他們似乎並沒有帶來JFrame
頂端。
有采取截圖沒有問題,而是與效果。你有任何其他想法來製作它嗎?
這裏是代碼: FullScreen.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.Timer;
import javax.swing.filechooser.FileSystemView;
class FullScreen
{
int times, duration;
Timer t;
Robot r;
BufferedImage bi;
FullScreenGUI fg;
FullScreen(int tim, int duration)
{
fg = new FullScreenGUI();
fg.setVisible(true);
this.times = tim;
this.duration = duration;
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
System.out.println("Inside constructor");
t = new Timer(duration*1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Inside action");
if(times>0)
{
System.out.println("times: "+times);
//Get the screenshot
bi = capture();
//Gives the path of the home directory. For windows, it'll go to your desktop
FileSystemView filesys = FileSystemView.getFileSystemView();
File fl = filesys.getHomeDirectory();
saveImage(bi, fl);
//Produces the "clicking" effect
fg.setAlwaysOnTop(true);
fg.toFront();
fg.fire();
times--;
}
else
t.stop();
}
});
}
public void fire()
{
t.start();
}
public void saveImage(BufferedImage source, File destination)
{
System.out.println("Inside saveimage");
if(destination.isDirectory())
{
System.out.println("destination: "+destination.getAbsolutePath());
String tmp = destination.getAbsolutePath() + File.separator + "Screenshot";
String str;
int i=1;
for(str=tmp; (new File(str+".png").exists()); i++)
{
str = tmp + "_" + String.valueOf(i);
System.out.println("trying: "+str);
}
try {
ImageIO.write(source, "png", new File(str+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public BufferedImage capture()
{
System.out.println("Captured");
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
return r.createScreenCapture(new Rectangle(d));
}
public static void main(String arg[])
{
//click 4 times each at an interval of 2 seconds
FullScreen f = new FullScreen(4,2);
f.fire();
while(f.t.isRunning());
}
}
FullScreenGUI.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FullScreenGUI extends JFrame {
FullScreenGUI()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setResizable(false);
setOpacity(0.01f);
setAlwaysOnTop(true);
setLayout(new BorderLayout());
JLabel jl = new JLabel();
jl.setBackground(Color.white);
add(jl);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void fire()
{
System.out.println("click");
setVisible(true);
try{
setOpacity(1f);
Thread.sleep(50);
setOpacity(0.1f);
}catch(Exception e) { e.printStackTrace(); }
setVisible(false);
}
}
.-),我確定EDT必須凍結,請在我刪除的帖子中看到鏈接的帖子,Swing Timer是徒勞無益的,Merry Christmast是您世界上最簡單的角落 – mKorbel
是的工作!謝謝。祝你們倆聖誕快樂! :-) – Divyanshu
明天不是聖誕節的傢伙? :P現在是2012年12月24日下午04:44 CAT。 –