Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- public class w {
- public static void main(String[] args) {
- int width = 600;
- int height = 400;
- MyFrame fr = new MyFrame(width, height);
- }
- }
- class MyFrame extends JFrame {
- public MyFrame(int width, int height) {
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- setBounds(screenSize.width/2 - (width / 2), screenSize.height/2 - (height / 2), width, height);
- setVisible(true);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- MyPane1 np = new MyPane1();
- add(np);
- np.setBounds(0, 0, width, height);
- }
- }
- class MyPane1 extends JPanel {
- Image img = null, car_red = null, car_blue = null;
- private int x = 0;
- private int y = 0;
- MyPane1() {
- try {
- img = ImageIO.read(new File("src/road.png"));
- car_red = ImageIO.read(new File("src/car_red.png"));
- car_blue = ImageIO.read(new File("src/car_blue.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- Timer timer = new Timer(1000, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- repaint();
- }
- });
- timer.start();
- }
- public void paintComponent(Graphics gr) {
- gr.drawImage(img, 0, 0, null);
- gr.drawImage(car_blue, x, 0, null);
- gr.drawImage(car_red, y, 150, null);
- if (x<=400)
- x += 100;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement