Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.java.main;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class Main extends JFrame
- {
- public Main()
- {
- add(new Board());
- setTitle("Star");
- setResizable(false);
- pack();
- setLocationRelativeTo(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args)
- {
- EventQueue.invokeLater(new Runnable()
- {
- @Override
- public void run()
- {
- JFrame ex = new Main();
- ex.setVisible(true);
- }
- });
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- // In this example, the timer will regularly call the run() method of the ScheduleTask class.
- package com.java.main;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- @SuppressWarnings("serial")
- public class Board extends JPanel
- {
- private final int B_WIDTH = 350;
- private final int B_HEIGHT = 350;
- private final int INITIAL_X = -40;
- private final int INITIAL_Y = -40;
- private final int INITIAL_DELAY = 100;
- private final int PERIOD_INTERVAL = 25;
- private Image star;
- private Timer timer;
- private int x, y;
- public Board()
- {
- loadImage();
- initBoard();
- }
- private void loadImage()
- {
- ImageIcon ii = new ImageIcon("star.png");
- star = ii.getImage();
- }
- private void initBoard()
- {
- setBackground(Color.BLACK);
- setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
- setDoubleBuffered(true);
- x = INITIAL_X;
- y = INITIAL_Y;
- // Here we create a timer and schedule a task with a specific interval. There is an initial delay.
- timer = new Timer();
- timer.scheduleAtFixedRate(new ScheduleTask(),
- INITIAL_DELAY, PERIOD_INTERVAL);
- }
- @Override
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- drawStar(g);
- }
- private void drawStar(Graphics g)
- {
- Graphics2D g2d = (Graphics2D)g;
- g2d.drawImage(star, x, y, this);
- Toolkit.getDefaultToolkit().sync();
- g.dispose();
- }
- private class ScheduleTask extends TimerTask {
- // Each 10ms the timer will call this run() method.
- @Override
- public void run()
- {
- x += 1;
- y += 1;
- if (y > B_HEIGHT)
- {
- y = INITIAL_Y;
- x = INITIAL_X;
- }
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment