public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}
}
public class MainFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel P;
public MainFrame()
{
Dimension dimension = new Dimension(500, 500);
setPreferredSize(dimension);
setMaximumSize(dimension);
setMinimumSize(dimension);
setTitle("Сніговик");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
P = new JPanel();
P.setPreferredSize(dimension);
P.setMaximumSize(dimension);
P.setMinimumSize(dimension);
add(P);
mypaint();
lisener();
}
private void mypaint(){
try
{
P.setLayout(null);
Comp c = new Comp();
P.add(c);
}catch( Exception e){
}
}
private void lisener(){
P.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if (e.getModifiers()==e.BUTTON1_MASK){
Comp c = new Comp();
P.add(c);
int x = e.getX();
int y = e.getY();
int r, g, b;
r = (255*x)/500;
g = (255*y)/500;
b = (255*(x+y))/1000;
c.animation(new Color(r, g, b), x, y);
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}
public class Comp extends Component{
private int X, Y, Width, Height;
private Image Img;
public Comp(){
}
public void setLocation(int x, int y){
this.X = x;
this.Y = y;
super.setBounds(this.X, this.Y, this.Width, this.Height);
}
public synchronized void setImage(Image img){
this.Img = img;
}
public void setSizeComponent(int width, int height){
this.Width = width;
this.Height = height;
super.setBounds(this.X, this.Y, this.Width, this.Height);
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(Img, 0, 0, null);
//g.fillRect(0, 0, Width, Height);
}
public void animation(Color color, int x, int y){
Animation a = new Animation(this);
a.setLocation(x, y);
a.setColor(color);
a.start();
}
}
public class Animation extends Thread {
private ArrayList<String> Frames;
private Comp Element;
private int ImgWidth, ImgHeight;
private Color color;
public Animation(Comp element) {
this.Element = element;
Frames = new ArrayList<>();
//Кадри анімації
Frames.add(new String("1.jpg"));
Frames.add(new String("2.jpg"));
Frames.add(new String("3.jpg"));
Frames.add(new String("4.jpg"));
}
public void run(){
int i, n = Frames.size();
int array[];
RepaintThred repaint = null;
Image img;
for (i=0; i<n; i++){
array = getPixels(loadImage(Frames.get(i)));
repaintFrame(array);
img = createImage(array);
Element.setSizeComponent(ImgWidth, ImgHeight);
if ((repaint!=null)&&(repaint.isAlive())){
try {
repaint.join(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint = new RepaintThred(Element, img);
} else {
repaint = new RepaintThred(Element, img);
}
}
}
private Image loadImage(String address) {
try {
Image img = ImageIO.read(new File(address));
this.ImgWidth = img.getWidth(null);
this.ImgHeight = img.getHeight(null);
return img;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
this.ImgWidth = 0;
this.ImgHeight = 0;
return null;
}
}
private int[] getPixels(Image img) {
int pixels[] = new int[this.ImgWidth*this.ImgHeight];
PixelGrabber gr = new PixelGrabber( img,
0,
0,
this.ImgWidth,
this.ImgHeight,
pixels,
0,
this.ImgWidth);
try {
gr.grabPixels();
return pixels;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private Image createImage(int pixels[]) {
Component comp = new Component() {
private static final long serialVersionUID = 1L;
};
Image img = comp.createImage(new MemoryImageSource (
this.ImgWidth,
this.ImgHeight,
pixels,
0,
this.ImgWidth)
);
return img;
}
public void start(){
super.start();
}
// Зміна основного кольору
private void repaintFrame(int pixels[]) {
int i, r, g, b;
Color c = color;
int Red = c.getRed(), Blue = c.getBlue(), Green = c.getGreen();
for (i = 0; i < pixels.length; i++) {
if (pixels[i] == -16777216) {
pixels[i] = 0x00000000;
continue;
}
pixels[i] = -(pixels[i] + 1);
r = pixels[i]/0x10000;
g = pixels[i]/0x100 - r*0x100;
b = pixels[i] - r*0x10000 - g*0x100;
r = (Red+r)>255?255:(Red+r);
g = (Green+g)>255?255:(Green+g);
b = (Blue+b)>255?255:(Blue+b);
pixels[i] = -(255-r)*0x10000 - (255-g)*0x100 - (255-b)-1;
}
}
public void setLocation(int x, int y){
loadImage(Frames.get(0));
Element.setLocation(x-ImgWidth/2, y-ImgHeight/2);
}
public void setColor(Color color){
this.color = color;
}
}
public class RepaintThred extends Thread{
private Comp Element;
public RepaintThred(Comp element, Image img) {
// TODO Auto-generated constructor stub
this.Element = element;
element.setImage(img);
super.start();
}
public void run(){
Element.repaint();
try {
super.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}