NB52053

Assignment_LAB_9

Aug 12th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. 1) Create a GUI application where you will draw a circle of diameter 30 at a point where user clicks
  2. the mouse.
  3.  
  4. public class Lab9_1 extends JFrame {
  5.  
  6.     HandlerMouse handler = new HandlerMouse();
  7.  
  8.     public JPanel panel;
  9.     public int mouseX, mouseY;
  10.     public Point point1, point2;
  11.     Graphics g;
  12.  
  13.     public Lab9_1(){
  14.  
  15.         setTitle("Draw Circle");
  16.         setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE);
  17.         setLayout(null);
  18.  
  19.         panel = new JPanel();
  20.         panel.setSize(300,300);
  21.         panel.setLocation(0, 0);
  22.         panel.setBackground(Color.WHITE);
  23.         panel.addMouseListener(handler);
  24.         panel.addMouseMotionListener(handler);
  25.  
  26.         add(panel);
  27.         setLocationRelativeTo(null);
  28.         setVisible(true);
  29.  }
  30.  
  31.     public void paint(Graphics g, int x, int y){
  32.  
  33.         g.setColor(Color.BLUE);
  34.         g.drawOval(x, y, 30, 30);
  35.         repaint();
  36.     }
  37.  
  38.     public void paint(Graphics g) {
  39.         super.paint(g);
  40.         g.drawOval(x, y, 30, 30);
  41.     }
  42.  
  43.     private class HandlerMouse implements MouseListener, MouseMotionListener{
  44.  
  45.         public void mouseClicked(MouseEvent evt){
  46.             x = evt.getX();
  47.             y = evt.getY();
  48.             repaint();
  49.  
  50.             paint(g, mouseX, mouseY);
  51.         }
  52.  
  53.         public void mouseEntered(MouseEvent arg0){}
  54.  
  55.         public void mouseExited(MouseEvent arg0){}
  56.        
  57.         public void mousePressed(MouseEvent evt){}
  58.  
  59.         public void mouseReleased(MouseEvent arg0){}
  60.        
  61.         public void mouseDragged(MouseEvent e){}
  62.  
  63.         public void mouseMoved(MouseEvent e){}
  64.     }
  65.  
  66.     int x, y;
  67.  
  68.     public static void main(String[] args){
  69.         Lab9_1 lab91 = new Lab9_1();
  70.     }
  71. }
  72.  
  73. (3)
  74. public class Scale extends JPanel implements ActionListener, KeyListener {
  75.  
  76.     int ball_x = 100; // X axis inisialization
  77.     int ball_y = 20;  // y asix inisialization
  78.  
  79.     int x2 = 1;  //First move trhough X axis
  80.     int y2 = 1;  //First move trhough Y axis
  81.  
  82.     Timer t = new Timer(5, this);
  83.     int x = 0, y = 0, velx = 0, vely = 0;
  84.  
  85.  public void ballMove(){
  86.  
  87.         if (ball_x > (300 - 48)) {  // 48 is size limit through X
  88.             x2 = -1;
  89.         }
  90.         if (ball_x < 0) {
  91.             x2 = 1;
  92.         }
  93.         if (ball_y > (300 - 65)) {  // 65 is size limit through Y
  94.             y2 = -1;
  95.         }
  96.         if (ball_y < 0) {
  97.             y2 = 1;
  98.         }
  99.  
  100.         ball_x = ball_x + x2; // setting its position to initial X axis
  101.         ball_y = ball_y + y2; // setting its position to initial Y axis
  102.  }
  103.  
  104.   public Scale() {
  105.  
  106.         t.start();
  107.         addKeyListener(this);
  108.         setFocusable(true);
  109.         setFocusTraversalKeysEnabled(false);
  110.     }
  111.  
  112.      public void paintComponent(Graphics s ) {
  113.         super.paintComponent(s);
  114.         s.setColor(Color.BLUE);                   // Set rectangle colour
  115.         s.fillRect(x, y, 15, 15);   // Set rectangle bar size
  116.         //Graphics2D g2d = (Graphics2D) s;
  117.         //g2d.fillOval(ball_x, ball_y, 30, 30);
  118.         //s.setColor(Color.BLACK);
  119.  
  120.     }
  121.  
  122.  public void paint(Graphics g) {
  123.         super.paint(g);
  124.         g.setColor(Color.orange);
  125.         Graphics2D g2d = (Graphics2D) g;
  126.         g2d.fillOval(ball_x, ball_y, 30, 30);
  127.  
  128.     }
  129.    
  130.  public void actionPerformed(ActionEvent e) {
  131.         if (x < 0) {
  132.             velx = 0;
  133.             x = 0;
  134.         }
  135.  
  136.         if (x > 270) {
  137.             velx = 0;
  138.             x = 270;
  139.         }
  140.  
  141.         if (y < 0) {
  142.             vely = 0;
  143.             y = 0;
  144.         }
  145.  
  146.         if (y > 250) {
  147.             vely = 0;
  148.             y = 250;
  149.         }
  150.  
  151.         x += velx;
  152.         y += vely;
  153.         repaint();
  154.    }
  155.  
  156.    public void keyPressed(KeyEvent e) {
  157.         int code = e.getKeyCode();
  158.  
  159.         if (code == KeyEvent.VK_DOWN) {
  160.             vely = 1;
  161.             velx = 0;
  162.         }
  163.         if (code == KeyEvent.VK_UP) {
  164.             vely = -1;
  165.             velx = 0;
  166.         }
  167.         if (code == KeyEvent.VK_LEFT) {
  168.             vely = 0;
  169.             velx = -1;
  170.         }
  171.         if (code == KeyEvent.VK_RIGHT) {
  172.             vely = 0;
  173.             velx = 1;
  174.  
  175.         }
  176.     }
  177.  
  178.     public void keyTyped(KeyEvent e) {
  179.     }
  180.  
  181.     public void keyReleased(KeyEvent e) {
  182.         velx = 0;
  183.         vely = 0;
  184.     }
  185.  
  186.  public static void main(String arge[]) throws InterruptedException{
  187.        
  188.         JFrame f = new JFrame("Scale");
  189.         Scale s = new Scale();
  190.         f.add(s);
  191.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  192.         f.setSize(300, 300);  // Set Main Frame Size.
  193.         f.setVisible(true);
  194.  
  195.  
  196.         while (true) {
  197.  
  198.             s.ballMove();
  199.             s.repaint();
  200.             Thread.sleep(10);
  201.         }
  202.     }
  203. }
Add Comment
Please, Sign In to add comment