I'm trying to create a program that shows for 1000 ms a transparent screen following by an image that is displaying for 20 ms. That should continue in a loop (1000m s -> 20 ms -> 1000 ms -> 20 ms -> ....).
The time for displaying the image is too long, so that you can see the image on the screen if it appears.
Is there a way to get a better performance?
Here is the code:
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class SubPrimingJava extends JFrame {
public SubPrimingJava() {
super("GradientTranslucentWindow");
setBackground(new Color(0,0,0,0));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width+20, screenSize.height+200);
//setLocationRelativeTo(null);
setLocation(-10, -100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
if (g instanceof Graphics2D) {
final int R = 240;
final int G = 240;
final int B = 240;
Paint p =
new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
0.0f, getHeight(), new Color(R, G, B, 0), true);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
};
setContentPane(panel);
setLayout(new GridBagLayout());
}
public static void main(String[] args) {
// Determine what the GraphicsDevice can support.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported =
gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
//If translucent windows aren't supported, exit.
if (!isPerPixelTranslucencySupported) {
System.out.println(
"Per-pixel translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SubPrimingJava gtw = new
SubPrimingJava();
// Display the window.
gtw.setVisible(true);
ImageIcon icon = new ImageIcon("test.png");
JLabel label = new JLabel( icon );
gtw.add(label);
gtw.setOpacity(0);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gtw.setOpacity(1);
Timer t2 = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gtw.setOpacity(0);
}
});
t2.start();
t2.setRepeats(false);
}
});
timer.setRepeats(true);
timer.start();
}
});
}
}
Don't know if this blinks any faster, but its all I could come up with:
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gtw.setOpacity(1);
label.paintImmediately(label.getBounds());
gtw.setOpacity(0);
label.paintImmediately(label.getBounds());
}
});
timer.setRepeats(true);
timer.start();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.