I am a hobbyist attempting to implement some classics in Java 2D. I have problems getting some background images to scroll properly. Main problem seems to be the occurence of what I believe is called Tearing, ie old parts of "frames" are displayed giving a flickering effect in some parts of the image. Google tells me that it is probably related to missing VSYNC in windowed Java applications, and that it is impossible/very difficult to fix completely. However before giving up I thought to try asking here.
I have assembeled the code below as a bare bone example to illustrate the problem.
Any help of improving this code to reduce the Tearing/flickering would be much appreciated.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class ScrollTest {
private static int WIDTH = 320, HEIGHT = 240;
public static void main(String[] args) {
//System.setProperty("sun.java2d.opengl","True"); //Does not seem to make any difference
new ScrollTest().run(WIDTH, HEIGHT, 4, 1, "ScrollTest");
}
private boolean isRunning;
protected JFrame window;
protected Canvas canvas;
private BufferStrategy bs;
private int FPS = 100;
private long targetTime = 1000 / FPS;
protected int width;
protected int height;
protected float scale;
protected float aspect;
protected String title;
protected BufferedImage screenImage;
private BufferedImage bgImage;
private int bgTop;
private float offsetX;
public void run(int width, int height, float scale, float aspect, String title) {
this.width = width;
this.height = height;
this.scale = scale;
this.aspect = aspect;
this.title = title;
isRunning = false;
try {
init();
isRunning = true;
gameLoop();
}
finally {
lazilyExit();
}
}
public void init() {
canvas = new Canvas();
canvas.setPreferredSize(new Dimension((int)(width*scale), (int)(height*scale*aspect)));
window = new JFrame();
window.getContentPane().add(canvas);
window.setResizable(false);
window.setTitle(title);
window.pack();
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
canvas.createBufferStrategy(3);
bs = canvas.getBufferStrategy();
screenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//bgImage = loadBImage("res/res_DoubleDragon/Area 1_C_crop.png");
try {
bgImage = toCompatibleImage(ImageIO.read(new URL("https://spritedatabase.net/files/arcade/603/Background/Area1.png")));
} catch (Exception ex) {
ex.printStackTrace();
}
bgTop = HEIGHT-bgImage.getHeight(null)+20;
}
public BufferedImage loadBImage(String filename)
{
try {
return toCompatibleImage(ImageIO.read(new File(filename)));
} catch (IOException e) {
System.out.println("Image file not found");
return null;
}
}
public void gameLoop() {
long timestamp;
long usedTime;
long sleepTime;
long elapsedTime=0;
while (isRunning)
{
timestamp = System.nanoTime();
update(elapsedTime);
System.out.println(elapsedTime);
Graphics2D g = (Graphics2D)screenImage.getGraphics();
draw(g);
g.dispose();
Graphics2D g2 = (Graphics2D) bs.getDrawGraphics();
g2.scale(scale,scale*aspect);
g2.drawImage(screenImage,0,0,null);
bs.show();
g2.dispose();
usedTime = System.nanoTime() - timestamp;
sleepTime = targetTime - usedTime / 1000000;
if(sleepTime < 0) sleepTime = 5;
try {
Thread.sleep(sleepTime);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
elapsedTime=(System.nanoTime()-timestamp) / 1000000;
}
}
public void update(long elapsedTime) {
offsetX -= .05f*elapsedTime;
offsetX = Math.min(offsetX, 0);
offsetX = Math.max(offsetX, WIDTH - bgImage.getWidth(null));
}
public void draw(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.drawImage(bgImage, Math.round(offsetX), bgTop, null);
}
public void stop() {
isRunning = false;
}
/**
Exits the VM from a daemon thread. The daemon thread waits
2 seconds then calls System.exit(0). Since the VM should
exit when only daemon threads are running, this makes sure
System.exit(0) is only called if neccesary. It's neccesary
if the Java Sound system is running.
*/
public void lazilyExit() {
Thread thread = new Thread() {
public void run() {
// first, wait for the VM exit on its own.
try {
Thread.sleep(2000);
}
catch (InterruptedException ex) { }
// system is still running, so force an exit
System.exit(0);
}
};
thread.setDaemon(true);
thread.start();
}
private static final GraphicsConfiguration GFX_CONFIG = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
public static BufferedImage toCompatibleImage(BufferedImage image) {
/*
* if image is already compatible and optimized for current system settings, simply return it
*/
if (image.getColorModel().equals(GFX_CONFIG.getColorModel())) {
return image;
}
System.out.println("incompatible image");
// image is not optimized, so create a new image that is
final BufferedImage new_image = GFX_CONFIG.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
// get the graphics context of the new image to draw the old image on
final Graphics2D g2d = (Graphics2D) new_image.getGraphics();
// actually draw the image and dispose of context no longer needed
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
// return the new optimized image
return new_image;
}
}
Related
My laptop's resolution is 1920x1080. I recently opened my game from another laptop with 1360x768. The JPanel changed size and half of the game was outside the screen. I understand that my game dimensions x 4 are bigger than the second laptop's resolution. But, is there a way to make it adjust to every screen? Any ideas?
package Main;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import GameState.GameStateManager;
import Handlers.Keys;
#SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener {
// dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 4;
// game thread
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000 / FPS;
// image
private BufferedImage image;
private Graphics2D g;
// game state manager
private GameStateManager gsm;
// other
private boolean recording = false;
private int recordingCount = 0;
private boolean screenshot;
public GamePanel() {
super();
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
public void addNotify() {
super.addNotify();
if (thread == null) {
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;
gsm = new GameStateManager();
}
public void run() {
init();
long start;
long elapsed;
long wait;
// game loop
while (running) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (wait < 0) wait = 5;
try {
Thread.sleep(wait);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void update() {
gsm.update();
Keys.update();
}
private void draw() {
gsm.draw(g);
}
private void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();
if (screenshot) {
screenshot = false;
try {
java.io.File out = new java.io.File("screenshot " + System.nanoTime() + ".gif");
javax.imageio.ImageIO.write(image, "gif", out);
} catch (Exception e) {
}
}
if (!recording)
return;
try {
java.io.File out = new java.io.File("C:\\out\\frame" + recordingCount + ".gif");
javax.imageio.ImageIO.write(image, "gif", out);
recordingCount++;
} catch (Exception e) { }
}
public void keyTyped(KeyEvent key) { }
public void keyPressed(KeyEvent key) {
if (key.isControlDown()) {
if (key.getKeyCode() == KeyEvent.VK_R) {
recording = !recording;
return;
}
if (key.getKeyCode() == KeyEvent.VK_S) {
screenshot = true;
return;
}
}
Keys.keySet(key.getKeyCode(), true);
}
public void keyReleased(KeyEvent key) {
Keys.keySet(key.getKeyCode(), false);
}
}
You should be basing the size of game on the resolution of the screen.
You can easily get the resolution by using:
System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
The next issue is are you using a full screen for your game or is you game displayed in a frame. If you are using a frame with decorations, then you also need to account for the size of the title bar and borders around the frame. This information can only be accessed after the frame has been packed.
frame.pack();
System.out.println("Frame Insets : " + frame.getInsets() );
So now the size of your game board would be the size of the screen less the insets of the frame decorations.
I'm trying to set the dimensions for this game i want to create. The error is at while(running); I went over it for an hour but i can't seem to catch the error. I'm using NetbeansIDE also setPreserredSize (new Dimension(WIDTH, HEIGHT)); gives me an error, it is asking to Create a method " setPrefferedSize(java.awt.Dimension)"in.com.francesc.game.Game
private void setPreserredSize(Dimension dimension) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
when i run it i get this error
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of type
at com.francescstudio.game.Game.<init>(Game.java:67)
at com.francescstudio.game.Start.main(Start.java:16)
Java Result: 1
heres all the code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* #author francesc
*/
public class Game extends JPanel implements KeyListener, Runnable {
public static final long SerialVersionUID = 1L;
public static final int WIDTH = 400;
public static final int HEIGHT = 630;
public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28);
private Thread game;
private boolean running;
private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private long startTime;
private long elapse;
private boolean set;
public Game() {
setFocusable(true);
setPreserredSize (new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
}
private void update() {
}
private void render() {
Graphics2D g = (Graphics2D) Image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
// render board
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(Image, 0, 0, null);
g2d.dispose();
}
#Override
public void run() {
}
int fps = 0, update = 0;
long fpsTimer = System.currentTimeMillis();
double nsPerUpdate = 1000000000.0 / 60;
// last update time in nano seconds
double then = System.nanoTime();
double unprocessed = 0;
while (running){
boolean shouldRender = false;
double now = System.nanoTime();
unprocessed += (now - then) / nsPerUpdate;
then = now;
// update queque
while (unprocessed >= 1) {
update++;
update();
unprocessed--;
shouldRender = true;
}
// render
if (shouldRender) {
fps++;
render();
shouldRender = false;
} else {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized start(){
if(running)return;
running = true;
game = new Thread (this, "game");
game.start();
}
public synchronized stop (){
if(!running) return;
running = false;
System.exit(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
Your problem is while (running) {} isn't inside of a method, you defined it in the class where it will never get called.
EDIT:
Your code should look something a little more like this
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* #author francesc
*/
public class Game extends JPanel implements KeyListener, Runnable {
public static final long SerialVersionUID = 1L;
public static final int WIDTH = 400;
public static final int HEIGHT = 630;
public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28);
private Thread game;
private boolean running;
private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private long startTime;
private long elapse;
private boolean set;
public Game() {
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
}
private void update() {}
private void render() {
Graphics2D g = (Graphics2D) Image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
// render board
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(Image, 0, 0, null);
g2d.dispose();
}
#Override
public void run() {
int fps = 0, update = 0;
long fpsTimer = System.currentTimeMillis();
double nsPerUpdate = 1000000000.0 / 60;
// last update time in nano seconds
double then = System.nanoTime();
double unprocessed = 0;
while (running) {
boolean shouldRender = false;
double now = System.nanoTime();
unprocessed += (now - then) / nsPerUpdate;
then = now;
// update queque
while (unprocessed >= 1) {
update++;
update();
unprocessed--;
shouldRender = true;
}
// render
if (shouldRender) {
fps++;
render();
shouldRender = false;
} else {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public synchronized start(){
if(running)return;
running = true;
game = new Thread (this, "game");
game.start();
}
public synchronized stop (){
if(!running) return;
running = false;
System.exit(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
Hello I am kind of new to java. I am working on a game. when the game starts a loading screen appears and the fades away that's it so far. My question is simple. Is there a way to change the image size to fit the size of the computer screen. I want it to fit all types of screen sizes and be a larger scale of the original image. Here is my Main class so far(there are other classes like player and title and play but they are irrelevant to the question:
package Main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Comp extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public boolean run = true;
public Image screen, img;
public static double dir = 5;
public static int pixelSize = 1;
public static Dimension size = new Dimension(680, 500);
public static Dimension pixel = new Dimension(size.width / pixelSize,
size.height / pixelSize);
public static int mx;
public static manager m;
public static int my;
public static int fps;
public static boolean mr;
public static boolean ml;
JFrame frame = new JFrame();
public Thread t;
public boolean splashscreen = true;
public int time = 0;
public int timer = 140;
public int btime = 0;
public int btimer = 1;
private int FPS = 5;
private long targetTime = 1000 / FPS;
public Comp() {
setPreferredSize(size);
setFocusable(true);
addKeyListener(this);
addKeyListener(new listen());
addMouseListener(new listen());
addMouseMotionListener(new listen());
addMouseWheelListener(new listen());
m = new manager();
requestFocus();
// h = new InputHandler(this);
}
public void start() {
run = true;
frame.add(this);
frame.pack();
frame.requestFocus();
frame.setTitle("In The Maze");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("res/traps/beartrap1.png").getImage());
t = new Thread(this);
t.start();
}
public void stop() {
run = false;
}
public static void main(String[] args) {
Comp c = new Comp();
c.start();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 1000, 1000);
if (!splashscreen) {
m.render(g);
}
if(splashscreen){
ImageIcon i62 = new ImageIcon("res/splashscreen.png");
img = i62.getImage();
g.drawImage(img,(int)0 ,(int)0,Comp.size.width+10,Comp.size.height + 10,null);
time++;
}
g.dispose();
bs.show();
}
public void tick() {
if(time >= timer){
time = 0;
splashscreen = false;
}
if(!splashscreen){
m.tick();
}
Keys.update();
Esentials.tick();
}
public void run() {
screen = createVolatileImage(pixel.width, pixel.height);
long start;
long elapsed;
long wait;
long currentTime = System.currentTimeMillis();
while (run) {
start = System.nanoTime();
fps++;
if(System.currentTimeMillis() - currentTime >= 1000){
// System.out.println("fps:" + fps);
currentTime = System.currentTimeMillis();
fps = 0;
}
tick();
render();
elapsed = System.nanoTime() -start;
wait = targetTime = elapsed / 1000000;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void keyTyped(KeyEvent i) {
}
public void keyPressed(KeyEvent i) {
Keys.keySet(i.getKeyCode(), true);
}
public void keyReleased(KeyEvent i) {
Keys.keySet(i.getKeyCode(), false);
}
}
Try to use Toolkit.getScreenSize()
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
For resizing the image you can use simple imgscalr library
BufferedImage scaledImage = Scalr.resize(myImage, 100);
Determining the screen size...
How to set present screensize in Java Swing?
Full-Screen Exclusive Mode API
Scaling the image...
Java: maintaining aspect ratio of JPanel background image
Quality of Image after resize very low -- Java
The Perils of Image.getScaledInstance()
I'd also discourage the use KeyListener and encourage the use of the Key Bindings API. See How to use key bindings for more details
Using the default Graphics object from the java.awt package, you can use the drawImage method with 10 parameters:
int destinationWidth = ...;
int destinationHeight = ...;
g.drawImage(originalImage, 0, 0, destinationWidth, destinationHeight, 0, 0, originalImage.getWidth(null), originalImage.getHeight(null), null);
That way your image will be scaled and painted to g. Note that simply by using as in the example provided will stretch and deform your image if the aspect ratio of your window is different from the aspect ratio of the image.
I'd like to do JButton with nice transition effect. I write a class which extend by JButton and add to it custom MouseAdapter. It almost works, but if opacity should have 0 my one BufferedImage don't vanish.
Here my all source code:
public class ImageHoverButton extends JButton {
public class MouseListener extends MouseAdapter
{
public void mouseExited(MouseEvent me)
{
new Thread(new Runnable()
{
public void run()
{
for (float i = 1f; i >= 0f; i -= .03f)
{
setOpacity(i);
try
{
Thread.sleep(10);
}
catch (Exception e)
{
}
}
}
}).start();
}
public void mouseEntered(MouseEvent me)
{
new Thread(new Runnable()
{
public void run()
{
for (float i = 0f; i <= 1f; i += .03f)
{
setOpacity(i);
try
{
Thread.sleep(10);
}
catch (Exception e)
{
}
}
}
}).start();
}
public void mousePressed(MouseEvent me)
{
new Thread(new Runnable()
{
public void run()
{
for (float i = 1f; i >= 0.6f; i -= .1f)
{
setOpacity(i);
try
{
Thread.sleep(1);
}
catch (Exception e)
{
}
}
}
}).start();
}
}
private static final long serialVersionUID = 1L;
private BufferedImage imgBottom;
private BufferedImage imgHover;
private BufferedImage imgHoverRGB;
// filter to imgInActive
float[] scales = { 1f, 1f, 1f, 0f};
float[] offsets = new float[4];
RescaleOp rop = new RescaleOp(scales, offsets, null);
/**
* Constructor for image path
* #param img
* #param x
* #param y
*/
public ImageHoverButton(String imgBottomPath, String imgHoverPath, int x, int y) {
try {
this.imgBottom = ImageIO.read(new File(imgBottomPath));
this.imgHover = ImageIO.read(new File(imgHoverPath));
imgHoverRGB = new BufferedImage(imgHover.getWidth(null),
imgHover.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics g = imgHoverRGB.getGraphics();
g.drawImage(imgHover, 0, 0, null);
} catch (IOException e) {
}
this.setBounds(x, y, imgBottom.getWidth() + 40 , imgBottom.getHeight() + 50);
addMouseListener(new MouseListener());
setOpacity(0f);
setOpaque(false);
setBorderPainted(false);
setRolloverEnabled(false);
setCursor(new Cursor(Cursor.HAND_CURSOR));
setLayout(null);
}
public void setOpacity(float opacity) {
scales[3] = opacity;
rop = new RescaleOp(scales, offsets, null);
repaint();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(imgBottom, 50, 50, null);
g2d.drawImage(imgHoverRGB, rop, 0, 0);
}
}
Have any idea how to improve this?
I'm not so familiar with RescaleOp, and can't remember having used this before. But it seems like the results of applying it in this case are somewhat unexpected.
As an alternative, you might consider an AlphaComposite. The minimum modification that is necessary to achieve the desired effect would then be to change the line
g2d.drawImage(imgHoverRGB, rop, 0, 0);
to
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, scales[3]));
g2d.drawImage(imgHoverRGB, 0, 0, null);
However, there are several other issues with the code:
don't override paint. Instead, override paintComponent
don't call setBounds on a component (particlularly not in a constructor). The placement should be done by a layout manager
don't swallow Exceptions silently
don't load the images in the constructor of the button
implement getPreferredSize properly
don't spawn hundreds of threads due to mouse movement. (When you quickly move the mouse in and out, you'll have several threads running - some of them increasing the opacity, and some of them decreasing the opacity)
I created an example showing one possible approach: It contains an OpacityAnimator that allows a transition between two opacities, with a predefined delay in milliseconds. This animator is used to increase the opacity of the foreground image when the button is hovered with the mouse, and to decrease it when the mouse leaves the button.
(Note that this could be generalized further, and there are many possible "configuration settings" (like the transition delay) that could be exposed, but this is just intended as an example)
import java.awt.AlphaComposite;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class HoverButtonTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
try
{
createAndShowGUI();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
private static void createAndShowGUI() throws IOException
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage backgroundImage = loadImage("background.png");
BufferedImage foregroundImage = loadImage("foreground.png");
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(
new ImageHoverButton(backgroundImage, foregroundImage));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static BufferedImage loadImage(String path) throws IOException
{
return convertToARGB(ImageIO.read(new File(path)));
}
public static BufferedImage convertToARGB(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
}
class ImageHoverButton extends JButton
{
private class MouseHoverListener extends MouseAdapter
{
#Override
public void mouseExited(MouseEvent me)
{
opacityAnimator.changeOpacity(0.0f, 250);
}
#Override
public void mouseEntered(MouseEvent me)
{
opacityAnimator.changeOpacity(1.0f, 1000);
}
#Override
public void mousePressed(MouseEvent me)
{
opacityAnimator.changeOpacity(0.5f, 50);
}
}
private class OpacityAnimator
{
private final int DELAY_MS = 10;
private final Timer timer;
private float targetOpacity;
private float currentOpacity;
private float opacityStep;
OpacityAnimator()
{
timer = new Timer(DELAY_MS, new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if (currentOpacity > targetOpacity)
{
currentOpacity += opacityStep;
currentOpacity = Math.max(
currentOpacity, targetOpacity);
}
else if (currentOpacity < targetOpacity)
{
currentOpacity += opacityStep;
currentOpacity = Math.min(
currentOpacity, targetOpacity);
}
if (currentOpacity == targetOpacity)
{
timer.stop();
}
setOpacity(currentOpacity);
}
});
}
void changeOpacity(float targetOpacity, int durationMs)
{
timer.stop();
this.targetOpacity = targetOpacity;
float delta = targetOpacity - currentOpacity;
if (durationMs > 0)
{
opacityStep = (delta / durationMs) * DELAY_MS;
}
else
{
opacityStep = delta;
}
timer.start();
}
}
private final OpacityAnimator opacityAnimator;
private final BufferedImage backgroundImage;
private final BufferedImage foregroundImage;
private float opacity = 0.0f;
public ImageHoverButton(BufferedImage backgroundImage,
BufferedImage foregroundImage)
{
this.backgroundImage = backgroundImage;
this.foregroundImage = foregroundImage;
this.opacityAnimator = new OpacityAnimator();
addMouseListener(new MouseHoverListener());
setOpaque(false);
setBorderPainted(false);
setRolloverEnabled(false);
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
#Override
public Dimension getPreferredSize()
{
if (super.isPreferredSizeSet())
{
return super.getPreferredSize();
}
int w = Math
.max(backgroundImage.getWidth(), foregroundImage.getWidth());
int h = Math.max(backgroundImage.getHeight(),
foregroundImage.getHeight());
return new Dimension(w, h);
}
public void setOpacity(float opacity)
{
this.opacity = opacity;
repaint();
}
#Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
g.drawImage(backgroundImage, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
opacity));
g.drawImage(foregroundImage, 0, 0, null);
}
}
Don't access Swing components from other threads. Use a swing Timer instead.
See How to use swing timers
For some reason, my program can't find an image. I've tried absolutely everything and made sure the file path is exact. It worked fine in my previous game but now it just won't find the image at all.
Entity class
package Entities;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Entity
{
public double x, y, width, height, dx, dy;
public BufferedImage image;
public Entity(String s)
{
try
{ //error occurs here
image = ImageIO.read(getClass().getResourceAsStream(s));
}
catch (IOException e)
{
e.printStackTrace();
}
width = image.getWidth();
height = image.getHeight();
}
public void update()
{
x += dx;
y += dy;
}
public void draw(Graphics2D g)
{
g.drawImage(image, (int)x, (int)y, null);
}
}
GamePanel class
package Main;
//import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import Entities.Entity;
#SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener
{
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
//game thread
private Thread thread;
private boolean running;
private int fps = 60;
private long targetTime = 1000/fps;
//image
private BufferedImage image;
private Graphics2D g;
//test
public Entity e;
public GamePanel()
{
super();
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
public void addNotify()
{
super.addNotify();
if(thread == null)
{
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
public void init()
{
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;
e = new Entity("Resources/Test/test.png");
}
#Override
public void keyPressed(KeyEvent key)
{
//p1.keyPressed(key.getKeyCode());
}
#Override
public void keyReleased(KeyEvent key)
{
//p1.keyReleased(key.getKeyCode());
}
#Override
public void keyTyped(KeyEvent arg0)
{
}
#Override
public void run()
{
init();
long start;
long elapsed;
long wait;
//game loop
while(running)
{
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed/1000000;
if(wait < 0)
{
wait = 5;
}
try
{
Thread.sleep(wait);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
private void drawToScreen()
{
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();
}
private void draw()
{
//e.draw(g);
}
private void update()
{
}
}
This is the error
Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Entities.Entity.<init>(Entity.java:19)
at Main.GamePanel.init(GamePanel.java:67)
at Main.GamePanel.run(GamePanel.java:93)
at java.lang.Thread.run(Unknown Source)
A path Resources/Test/test.png suggest a relative path from the location of the class reference used to try and load it.
Since you are using your Entity class to try and load the image, and Entity resides in the Entities package, this will generate an absolute path (from the context of the class path) of /Entities/Resources/Test/test.png, which is obviously incorrect.
Try using /Resources/Test/test.png instead (assuming the Resources resides at the top of the path tree, for example...
+ Resources
+ Test
- test.png
+ Entities
- Entity
+ Main
- GamePanel