Tips/Verbesserungen für diese Class?

Kai008

Erfahrenes Mitglied
Ich habe eine Klasse geschrieben, die dazu da ist, den Bildschirm teilweiße abzudecken, um bei Szenenwechsel einen langsamen schönen Ein/Ausblendeffekt hinzubekommen. Hat jemand eventuell Tips oder Verbesserungen dafür?

Java:
package core;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Cover extends Object
{
	private static final int SCREEN_WIDTH = 800;
	private static final int SCREEN_HEIGHT = 600;
	
	private static final int IMAGE_WIDTH = 25;
	private static final int IMAGE_HEIGHT = 25;

	private static boolean panelIsAdded;
	
	private static JPanel coverPanel;
	private static JPanel[] panels;
	
	private static Thread loadThread;
	private static Thread coverThread;
	
	static
	{
		loadThread = new LoadCovers();
		loadThread.start();
	}
	public Cover()
	{
		super();
	}
	public static void init(){}
	
	public synchronized static void coverDark(boolean removePanel, long speed, JFrame jframe)
	{
		try
		{
			loadThread.join();
		}
		catch(InterruptedException e){}
		
		coverThread = new CoverDark(removePanel, speed, jframe);
		coverThread.start();
	}
	public synchronized static void coverBright(boolean removePanel, long speed, JFrame jframe)
	{
		try
		{
			loadThread.join();
		}
		catch(InterruptedException e){}
		
		coverThread = new CoverBright(removePanel, speed, jframe);
		coverThread.start();
	}
	
	public static Thread getCoverThread()
	{
		return(coverThread);
	}
	
	
	private static class LoadCovers extends Thread
	{
		public LoadCovers()
		{
			super();
		}
		public void run()
		{
			coverPanel = new JPanel(null);
			coverPanel.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
			coverPanel.setOpaque(false);
			
			ImageIcon coverIcon = new ImageIcon("img\\cover.png");
			int coverCount = coverIcon.getIconWidth() / IMAGE_WIDTH;
			
			panels = new JPanel[coverCount];
			for(byte b = 0; b < coverCount; b++)
			{
				panels[b] = new JPanel(null);
				panels[b].setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
				panels[b].setOpaque(false);
				
				int hLine = SCREEN_WIDTH / IMAGE_WIDTH;
				int vLine = SCREEN_HEIGHT / IMAGE_HEIGHT;
				
				BufferedImage cover = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
				Graphics g = cover.getGraphics();
				g.drawImage(coverIcon.getImage(), (0 - (b * IMAGE_WIDTH)), 0, null);
				g.dispose();
				
				for(byte vLineCounter = 0; vLineCounter < vLine; vLineCounter++)
				{
					for(byte hLineCounter = 0; hLineCounter < hLine; hLineCounter++)
					{
						JLabel label = new JLabel(new ImageIcon(cover));
						label.setBounds((hLineCounter * IMAGE_WIDTH), (vLineCounter * IMAGE_HEIGHT), IMAGE_WIDTH,
								IMAGE_HEIGHT);
						panels[b].add(label);
					}
				}
			}
		}
	}
	
	private static class CoverDark extends Thread
	{
		private boolean removePanel;
		private long speed;
		private JFrame jframe;

		public CoverDark(boolean removePanel, long speed, JFrame jframe)
		{
			super();
			
			this.removePanel = removePanel;
			this.speed = speed;
			this.jframe = jframe;
		}
		public void run()
		{
			for(byte b = 0; b < panels.length; b++)
				coverPanel.remove(panels[b]);
			
			if(speed <= 0)
			{
				coverPanel.add(panels[10]);
				
				if(!panelIsAdded)
				{
					jframe.add(coverPanel);
					panelIsAdded = true;
				}
			}
			else
			{
				if(!panelIsAdded)
				{
					jframe.add(coverPanel, 0);
					panelIsAdded = true;
				}
				
				for(byte b = 0; b <= 10; b++)
				{
					coverPanel.add(panels[b]);
					
					if(b > 0)
					{
						coverPanel.remove(panels[b - 1]);
					}
					coverPanel.repaint();
				
					try
					{
						Thread.sleep(speed);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				if(removePanel)
				{
					jframe.remove(coverPanel);
					jframe.repaint();
					panelIsAdded = false;
				}
			}
		}
	}
	
	private static class CoverBright extends Thread
	{
		private boolean removePanel;
		private long speed;
		private JFrame jframe;

		public CoverBright(boolean removePanel, long speed, JFrame jframe)
		{
			super();
			
			this.removePanel = removePanel;
			this.speed = speed;
			this.jframe = jframe;
		}
		public void run()
		{
			for(byte b = 0; b < panels.length; b++)
				coverPanel.remove(panels[b]);
			
			if(speed <= 0)
			{
				coverPanel.add(panels[0]);
				
				if(!panelIsAdded)
				{
					jframe.add(coverPanel, 0);
					panelIsAdded = true;
				}
			}
			else
			{
				if(!panelIsAdded)
				{
					jframe.add(coverPanel, 0);
					panelIsAdded = true;
				}
				
				for(byte b = 10; b > 0; b--)
				{
					coverPanel.add(panels[b]);
					
					if(b < 10)
					{
						coverPanel.remove(panels[b + 1]);
					}
					coverPanel.repaint();
				
					try
					{
						Thread.sleep(speed);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				if(removePanel)
				{
					jframe.remove(coverPanel);
					jframe.repaint();
					panelIsAdded = false;
				}
			}
		}
	}
}
 

Anhänge

  • cover.png
    cover.png
    246 Bytes · Aufrufe: 8
Zurück