Advertisement
darkor

oop_lab_9

Apr 29th, 2020
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.IOException;
  10.  
  11. public class w {
  12.     public static void main(String[] args) {
  13.         int width = 600;
  14.         int height = 400;
  15.  
  16.         MyFrame fr = new MyFrame(width, height);
  17.  
  18.     }
  19. }
  20.  
  21. class MyFrame extends JFrame {
  22.     public MyFrame(int width, int height) {
  23.  
  24.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  25.         setBounds(screenSize.width/2 - (width / 2), screenSize.height/2 - (height / 2), width, height);
  26.         setVisible(true);
  27.         setResizable(false);
  28.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.  
  30.  
  31.         MyPane1 np = new MyPane1();
  32.         add(np);
  33.         np.setBounds(0, 0, width, height);
  34.     }
  35. }
  36.  
  37.  
  38. class MyPane1 extends JPanel {
  39.  
  40.     Image img = null, car_red = null, car_blue = null;
  41.  
  42.     private int x = 0;
  43.     private int y = 0;
  44.  
  45.     MyPane1() {
  46.         try {
  47.             img = ImageIO.read(new File("src/road.png"));
  48.             car_red = ImageIO.read(new File("src/car_red.png"));
  49.             car_blue = ImageIO.read(new File("src/car_blue.png"));
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.  
  54.         Timer timer = new Timer(1000, new ActionListener() {
  55.             @Override
  56.             public void actionPerformed(ActionEvent actionEvent) {
  57.                 repaint();
  58.             }
  59.         });
  60.  
  61.         timer.start();
  62.     }
  63.  
  64.     public void paintComponent(Graphics gr) {
  65.         gr.drawImage(img, 0, 0, null);
  66.         gr.drawImage(car_blue, x, 0, null);
  67.         gr.drawImage(car_red, y, 150, null);
  68.         if (x<=400)
  69.             x += 100;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement