Advertisement
NB52053

Scale_Template

Aug 4th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. // GUI JAVA
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.Timer;
  12.  
  13.  
  14. public class Scale extends JPanel implements ActionListener, KeyListener {
  15.  
  16.     int ball_x = 100; // X axis inisialization
  17.     int ball_y = 20;  // y asix inisialization
  18.  
  19.     int x2 = 1;  //First move trhough X axis
  20.     int y2 = 1;  //First move trhough Y axis
  21.  
  22.     Timer t = new Timer(5, this);
  23.     int x = 0, y = 0, velx = 0, vely = 0;
  24.  
  25.  
  26.  
  27.     public void ballMove(){
  28.  
  29.         if (ball_x > (300 - 48)) {  // 48 is size limit through X
  30.             x2 = -1;
  31.         }
  32.         if (ball_x < 0) {
  33.             x2 = 1;
  34.         }
  35.         if (ball_y > (300 - 65)) {  // 65 is size limit through Y
  36.             y2 = -1;
  37.         }
  38.         if (ball_y < 0) {
  39.             y2 = 1;
  40.         }
  41.  
  42.  
  43.         ball_x = ball_x + x2; // setting its position to initial X axis
  44.         ball_y = ball_y + y2; // setting its position to initial Y axis
  45.     }
  46.  
  47.  
  48.  
  49.     public Scale() {
  50.  
  51.         t.start();
  52.         addKeyListener(this);
  53.         setFocusable(true);
  54.         setFocusTraversalKeysEnabled(false);
  55.     }
  56.  
  57.  
  58.  
  59.     public void paintComponent(Graphics s ) {
  60.         super.paintComponent(s);
  61.         s.setColor(Color.BLUE);                   // Set rectangle colour
  62.         s.fillRect(x, y, 15, 15);   // Set rectangle bar size
  63.         //Graphics2D g2d = (Graphics2D) s;
  64.         //g2d.fillOval(ball_x, ball_y, 30, 30);
  65.         //s.setColor(Color.BLACK);
  66.  
  67.     }
  68.     public void paint(Graphics g) {
  69.         super.paint(g);
  70.         g.setColor(Color.orange);
  71.         Graphics2D g2d = (Graphics2D) g;
  72.         g2d.fillOval(ball_x, ball_y, 30, 30);
  73.  
  74.     }
  75.  
  76.     public void actionPerformed(ActionEvent e) {
  77.         if (x < 0) {
  78.             velx = 0;
  79.             x = 0;
  80.         }
  81.  
  82.         if (x > 270) {
  83.             velx = 0;
  84.             x = 270;
  85.         }
  86.  
  87.         if (y < 0) {
  88.             vely = 0;
  89.             y = 0;
  90.         }
  91.  
  92.         if (y > 250) {
  93.             vely = 0;
  94.             y = 250;
  95.         }
  96.  
  97.  
  98.         x += velx;
  99.         y += vely;
  100.         repaint();
  101.     }
  102.  
  103.     public void keyPressed(KeyEvent e) {
  104.         int code = e.getKeyCode();
  105.  
  106.         if (code == KeyEvent.VK_DOWN) {
  107.             vely = 1;
  108.             velx = 0;
  109.         }
  110.         if (code == KeyEvent.VK_UP) {
  111.             vely = -1;
  112.             velx = 0;
  113.         }
  114.         if (code == KeyEvent.VK_LEFT) {
  115.             vely = 0;
  116.             velx = -1;
  117.         }
  118.         if (code == KeyEvent.VK_RIGHT) {
  119.             vely = 0;
  120.             velx = 1;
  121.  
  122.         }
  123.     }
  124.  
  125.     public void keyTyped(KeyEvent e) {
  126.     }
  127.  
  128.     public void keyReleased(KeyEvent e) {
  129.         velx = 0;
  130.         vely = 0;
  131.     }
  132.  
  133.  
  134.  
  135.     public static void main(String arge[]) throws InterruptedException{
  136.  
  137.  
  138.  
  139.         JFrame f = new JFrame("Scale");
  140.         Scale s = new Scale();
  141.         f.add(s);
  142.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  143.         f.setSize(300, 300);  // Set Main Frame Size.
  144.         f.setVisible(true);
  145.  
  146.  
  147.         while (true) {
  148.  
  149.             s.ballMove();
  150.             s.repaint();
  151.             Thread.sleep(10);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement