Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Animation {
- JFrame frame;
- Figura figura;
- private int oneX = 200;
- private int oneY = 200;
- private int dX = 2;
- private int dY = 2;
- public static void main(String[] args)
- {
- new Animation().go();
- }
- private void go()
- {
- frame = new JFrame("Test");
- figura = new Figura();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setSize(1500, 1000);
- frame.add(figura);
- frame.setVisible(true);
- moveIt();
- //тут можна викликати новий метод, а попередній закоментувати
- }
- class Figura extends JPanel
- {
- public void paintComponent(Graphics g)
- {
- // g.setColor(Color.BLUE);
- // g.fillRect(0, 0, this.getWidth(), this.getHeight());
- // g.setColor(Color.RED);
- // g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
- // g.setColor(Color.WHITE);
- // g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
- g.setColor(Color.BLACK);
- g.fillOval(oneX, oneY, 30, 30);
- g.setColor(Color.white);
- g.fillOval(oneX+5, oneY+5, 20, 20);
- }
- }
- private void moveIt()
- {
- while (true)
- {
- // oneX = oneX + dX;
- oneY = oneY + dY;
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 280) {
- dX = -dX;
- }
- if (oneY + dY < 0 || oneY + dY > 850) {
- dY = -dY;
- }
- try
- {
- Thread.sleep(3);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- //тут можна дописати новий метод
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement