1 урок
Скачать исходники проекта
Главная программа

package cat;

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


public class Cat {
   
public static void main(String[] args) {
        
World voronezh=new World(seasons.autumn);
Kitty barsik=new Kitty(voronezh);

barsik.leftEyeClose();
barsik.goLeft();
}}

Класс World

package cat;

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


enum seasons{winter, summer,autumn,spring};


public class World {
public JFrame frame;    
protected JLabel background;  
    
World(seasons type)
{
frame= new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 700);
frame.setResizable(false);

background=new JLabel("");
background.setBounds(0,0,1000,700);

switch(type) {
     case winter:
     background.setIcon(new ImageIcon(getClass().getResource("/pic/winter.jpg")));
     break;
     case summer:
     background.setIcon(new ImageIcon(getClass().getResource("/pic/summer.jpg")));
     break;
     case autumn:
     background.setIcon(new ImageIcon(getClass().getResource("/pic/autumn.png")));
     break;
     case spring:
     background.setIcon(new ImageIcon(getClass().getResource("/pic/spring.jpg")));
     break;
}


background.setLayout(null);
frame.add(background);
frame.setVisible(true);

}
}
Класс Kitty

package cat;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.*;


public class Kitty {
private int catx,caty;
JLabel labelpic;
JFrame frame;
    
Kitty(World place)
{
catx=(int)(Math.random()*800);
caty=(int)(Math.random()*400);
    
labelpic= new JLabel(""); 
labelpic.setLayout(null);
labelpic.setBounds(catx,caty,270,270);
labelpic.setIcon(new ImageIcon(getClass().getResource("/pic/cat1.png")));   
place.background.add(labelpic);
place.frame.repaint();
this.frame=place.frame;

}        

public void leftEyeClose()
{
    try {
        Thread.sleep(1400);
    } catch (InterruptedException ex) {
        Logger.getLogger(Kitty.class.getName()).log(Level.SEVERE, null, ex);
    }
labelpic.setIcon(new ImageIcon(getClass().getResource("/pic/cat_eye2_close.png")));   
frame.repaint();
    
}

public void rigthEyeClose()
{
    try {
        Thread.sleep(1400);
    } catch (InterruptedException ex) {
        Logger.getLogger(Kitty.class.getName()).log(Level.SEVERE, null, ex);
    }
labelpic.setIcon(new ImageIcon(getClass().getResource("/pic/cat_eye1_close.png")));   
frame.repaint();
    
}

public void bothEyeClose()
{
    try {
        Thread.sleep(1400);
    } catch (InterruptedException ex) {
        Logger.getLogger(Kitty.class.getName()).log(Level.SEVERE, null, ex);
    }
labelpic.setIcon(new ImageIcon(getClass().getResource("/pic/cat_2eye_close.png")));   
frame.repaint();
    
}

public void goLeft()
{    
for (int i=1;i<30;i++)
{
labelpic.setBounds(catx+i,caty,270,270);    

  try {
        Thread.sleep(40);
    } catch (InterruptedException ex) {
        Logger.getLogger(Kitty.class.getName()).log(Level.SEVERE, null, ex);
    }
}
catx+=30;
}

public void bothEyeOpen()
{
    try {
        Thread.sleep(1400);
    } catch (InterruptedException ex) {
        Logger.getLogger(Kitty.class.getName()).log(Level.SEVERE, null, ex);
    }
labelpic.setIcon(new ImageIcon(getClass().getResource("/pic/cat1.png")));   
frame.repaint();
    
}
}
Картинки для проекта