darkor

oop_blet_9_finally

Apr 30th, 2020
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. //Kravets
  2. package com.company;
  3.  
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. public class w extends JPanel implements Runnable{
  11.     Image img = null, ship_1 = null, ship_2 = null;
  12.  
  13.     private int x = 0;
  14.     protected Thread th_1;
  15.     protected Thread th_2;
  16.     static int width = 600;
  17.     static int height = 400;
  18.  
  19.     public static void main ( String[] args ) {
  20.         JFrame frame = new JFrame ( "Lab 9" );
  21.         w panel = new w ();
  22.         frame.getContentPane().add ( panel );
  23.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  24.         frame.setBounds(screenSize.width/2 - (width / 2), screenSize.height/2 - (height / 2), width, height);
  25.         frame.setVisible ( true );
  26.         frame.setResizable(false);
  27.     }
  28.  
  29.     public w(){
  30.         th_1 = new Thread ( this );
  31.         th_1.start();
  32.  
  33.         th_2 = new Thread( this );
  34.         th_2.start();
  35.     }
  36.  
  37.     public void paintComponent(Graphics gr) {
  38.         try {
  39.             img = ImageIO.read(new File("sea.jpg"));
  40.             ship_1 = ImageIO.read(new File("ship_1_ed.png"));
  41.             ship_2 = ImageIO.read(new File("ship_2_ed.png"));
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.         gr.drawImage(img, 0, 0, null);
  46.         gr.drawImage(ship_1, x, 0, null);
  47.         gr.drawImage(ship_2, 450 - x, 150, null);
  48.         if (x<=500)
  49.             x += 10;
  50.     }
  51.  
  52.     public void update ( Graphics g ) {
  53.         paintComponent ( g );
  54.     }
  55.  
  56.     public void run () {
  57.         while ( Thread.currentThread() == th_1 || Thread.currentThread() == th_2) {
  58.             repaint ();
  59.             try {
  60.                 Thread.sleep ( 100 );
  61.             }
  62.             catch ( InterruptedException e ) {
  63.                 System.out.println ( "Exception: " + e.getMessage() );
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment