/*
* Musical Skill Coach - An interactive midi device friendly program to help music students improve their skills
* Copyright (C) 2011 Paul-Emile Gaudet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package music.ui.images;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import music.ui.MusicalSkillCoach;
public abstract class BasicImage extends Component
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected BasicImage(String resourcePathName)
{
Image image = getImage();
if (image != null)
return;
Toolkit toolkit = Toolkit.getDefaultToolkit();
image = toolkit.getImage(MusicalSkillCoach.class.getResource(resourcePathName));
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 1);
boolean imagesLoaded;
do
{
imagesLoaded = true;
try
{
tracker.waitForAll();
}
catch (InterruptedException e)
{
imagesLoaded = false;
}
}
while (imagesLoaded == false);
tracker.removeImage(image);
setImage(image);
}
public Point getImageSize()
{
Image image = getImage();
return new Point(image.getWidth(null), image.getHeight(null));
}
protected abstract Image getImage();
protected abstract void setImage(Image image);
protected abstract Point getReferencePoint();
public void paint(Graphics g, int x, int y)
{
Image image = getImage();
Point p = getReferencePoint();
x -= p.x;
y -= p.y;
g.drawImage(image, x, y, this);
}
}