Recently I've been working on a small project until I've encountered something rather annoying.
What the program currently does is read certain folders which are generated by the program in which files with the file extension ".jpg" and ".png" are displayed (for this to work, the user the user has to manually insert the images in these folders).
That part works, until we get to the animation part that I've created.This is a rectangle with a certain RGBA value which changes its alpha value once the mouse hovers over it and exits it. With low resolution images, this animation works smoothly, but with high resolution images, its very laggy and the animation doesn't look so smooth.
mainFrame Class
package seriesorganiser;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Luca Muscat
*/
public class mainFrame {
private static boolean check;
private static final String DOCUMENTS_LOCATION = System.getProperty("user.home") + "\\Documents";
public static void main(String[] args) throws IOException {
check = new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").exists();
if (!check) {
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").mkdir();
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Shows").mkdir();
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Movies").mkdir();
}
JFrame frame = new JFrame("Series Organiser");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Layout Handler
JPanel container = new JPanel();
//Movies=0, Shows=1.
JPanel moviesPanel = new moviesShowsPanel(0);
JPanel showsPanel = new moviesShowsPanel(1);
container.setLayout(new GridLayout(1, 2));
container.add(moviesPanel);
container.add(showsPanel);
frame.add(container);
frame.pack();
}
}
moviesShowsPanel
package seriesorganiser;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
*
* @author Luca Muscat
*/
public class moviesShowsPanel extends JPanel {
int alpha = 255 / 2;
boolean isMouseOver = false;
boolean isMouseHover = false;
public final static int SHOWS_GENRE = 1;
public final static int MOVIES_GENRE = 0;
private int genreHolder;
public moviesShowsPanel(int genre) {
genreHolder=genre;
setPreferredSize(new Dimension(600, 600));
setBorder(BorderFactory.createLineBorder(Color.black));
setBackground(Color.LIGHT_GRAY);
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent me) {
if(!isMouseOver)repaint();
isMouseOver = true;
}
@Override
public void mouseExited(MouseEvent me) {
if(isMouseOver)repaint();
isMouseOver = false;
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
scanForImage findImage = new scanForImage();
if (isMouseOver && alpha>10) {
alpha-=10;
}
if(!isMouseOver && alpha<145){
alpha+=10;
}
try {
if(genreHolder==MOVIES_GENRE){
g.drawImage(findImage.findMoviesImage(), 0, 0, getWidth(), getHeight(), null);
}
if(genreHolder==SHOWS_GENRE){
g.drawImage(findImage.findShowsImage(),0,0,getWidth(),getHeight(),null);
}
} catch (IOException ex) {
Logger.getLogger(moviesShowsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
g.setColor(new Color(214, 229, 255, alpha));
g.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
}
scanForImage
package seriesorganiser;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author Luca Muscat
*/
public class scanForImage {
private static final String DOCUMENTS_LOCATION = System.getProperty("user.home")+"\\Documents";
BufferedImage findMoviesImage() throws IOException{
BufferedImage img = null;
File folderMovies = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Movies");
img=DRY(folderMovies);
return img;
}
BufferedImage findShowsImage()throws IOException{
BufferedImage img=null;
File folderShows = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Shows");
img = DRY(folderShows);
return img;
}
private BufferedImage DRY(File f)throws IOException{
BufferedImage img=null;
String filepath=String.valueOf(f);
File[] listOfFiles=f.listFiles();
for(File c:listOfFiles){
if(c.isFile()){
if(c.getName().contains(".jpg") || c.getName().contains(".png")){
img=ImageIO.read(new File(filepath+"\\"+c.getName()));
break;
}
}
}
return img;
}
}
To test it out all you guys need to do is put an image in both the Movies and Shows files which is found in a folder called SeriesOrganiser in the "Documents" folder library. (Windows 10).
Wouldn't mind some criticism on the code since I know that I have a lot to improve on.
A couple of comments from the paintComponent() method:
scanForImage findImage = new scanForImage();
Don't do I/O in a painting method. A painting method is for painting only
repaint();
Don't invoke repaint()
. This will cause a painting request to be continually added to the event queue.
If you need animation then use a Swing Timer to schedule the animation.
g.dispose();
Don't dispose of the Graphics object that was passed to the method. Other components on the frame may use this Graphics.
You only use the dispose()
method on a Graphic
object that you create within the paintComponent()
method.