Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Kravets
- package com.company;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.*;
- import java.io.File;
- import java.io.IOException;
- public class w extends JPanel implements Runnable{
- Image img = null, ship_1 = null, ship_2 = null;
- private int x = 0;
- protected Thread th_1;
- protected Thread th_2;
- static int width = 600;
- static int height = 400;
- public static void main ( String[] args ) {
- JFrame frame = new JFrame ( "Lab 9" );
- w panel = new w ();
- frame.getContentPane().add ( panel );
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- frame.setBounds(screenSize.width/2 - (width / 2), screenSize.height/2 - (height / 2), width, height);
- frame.setVisible ( true );
- frame.setResizable(false);
- }
- public w(){
- th_1 = new Thread ( this );
- th_1.start();
- th_2 = new Thread( this );
- th_2.start();
- }
- public void paintComponent(Graphics gr) {
- try {
- img = ImageIO.read(new File("sea.jpg"));
- ship_1 = ImageIO.read(new File("ship_1_ed.png"));
- ship_2 = ImageIO.read(new File("ship_2_ed.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- gr.drawImage(img, 0, 0, null);
- gr.drawImage(ship_1, x, 0, null);
- gr.drawImage(ship_2, 450 - x, 150, null);
- if (x<=500)
- x += 10;
- }
- public void update ( Graphics g ) {
- paintComponent ( g );
- }
- public void run () {
- while ( Thread.currentThread() == th_1 || Thread.currentThread() == th_2) {
- repaint ();
- try {
- Thread.sleep ( 100 );
- }
- catch ( InterruptedException e ) {
- System.out.println ( "Exception: " + e.getMessage() );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment