I am using paintComponent to make a fade-in opening. Though I can use transparent images to create this effect, I feel like drawing is both space conservative and efficient, but when I had tried to make code for it which is provided below
Graphics2D painter = (Graphics2D)g;
int paint = 0;
if (paint!=255) {
painter.setColor(new Color(0, 0, 0, paint));
paint+=17;
painter.drawImage(frm1,0,-16,768,576,null);
painter.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The window starts with a white screen, later showing frm1 (the image that I want the opacity to overlay)
In the Frame's code, I tried typing the constructor (which contains the start to the game loop) after the frame.setVisible(true); line of code, this affected the code in no way whatsoever. Even though I can use transparent images, I am trying to make the game more lightweight, therefore I would prefer paintComponent.
The code for the panel is provided below
package studios.masterpiece.pts.display;
//AWT
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
//IO
import java.io.IOException;
//ImageIO
import javax.imageio.ImageIO;
//Swing
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameScene extends JPanel implements Runnable{
private static final long serialVersionUID = -1892599432299801189L;
Timer timer;
Graphics painter;
BufferedImage frm1;
Thread repeat;
//References
static int tileSize = 48;
static int rows = 16;
static int columns = 12;
public static int SCREEN_WIDTH = tileSize*rows; //768
public static int SCREEN_HEIGHT = tileSize*columns;
//GameScene Properties
public GameScene() {
this.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.setDoubleBuffered(true);
startRepeat();
//Getting the image I want the opacity to overlay
try {
frm1 = ImageIO.read(getClass().getResourceAsStream("/studios/masterpiece/pts/animations/intro/Intro1.png"));
}
catch (IOException e) {e.printStackTrace();}
}
public void startRepeat() {
repeat = new Thread(this);
repeat.start();
//CURRENTLY DOES NOT WORK
timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Timer timer = (Timer) event.getSource();
int opacity = this.getOpacity();
opacity += 15;
if (opacity < 255) {
this.setOpacity(opacity);
} else {
opacity = 255;
this.setOpacity(opacity);
timer.stop();
}
this.repaint();
}
});
timer.start();
//UNTIL HERE
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D painter = (Graphics2D)g;
this.painter=painter;
}
@Override
public void run() {
while (repeat!=null) {
repaint();
}
}
}
Here's an example of a fade-in GUI.
Here's the initial GUI:
Here's the GUI after the image is faded in:
So, How did I do this?
I created a
JFrameand a drawingJPanel. I drew the background and the text usingColorinstances with a specified opacity or alpha component.I used a Swing
Timerto adjust the opacity every 250 milliseconds and repaint the drawingJPanel.Here's the complete runnable code. I made the additional class an inner class so I could post the code as one block.