Exemple #1
0
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    paint.setColor(color);

    if (rad > getHeight() / 2 || rad > getWidth() / 2)
      canvas.drawCircle(getWidth() / 2, getHeight() / 2, 0, paint);
    else canvas.drawCircle(getWidth() / 2, getHeight() / 2, rad, paint);
  }
Exemple #2
0
  public void drawStick(MotionEvent arg1) {
    position_x = (int) (arg1.getX() - (params.width / 2));
    position_y = (int) (arg1.getY() - (params.height / 2));
    distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
    angle = (float) cal_angle(position_x, position_y);

    if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
      if (distance <= (params.width / 2) - OFFSET) {
        draw.position(arg1.getX(), arg1.getY());
        draw();
        touch_state = true;
      }
    } else if (arg1.getAction() == MotionEvent.ACTION_MOVE && touch_state) {
      if (distance <= (params.width / 2) - OFFSET) {
        draw.position(arg1.getX(), arg1.getY());
        draw();
      } else if (distance > (params.width / 2) - OFFSET) {
        float x =
            (float)
                (Math.cos(Math.toRadians(cal_angle(position_x, position_y)))
                    * ((params.width / 2) - OFFSET));
        float y =
            (float)
                (Math.sin(Math.toRadians(cal_angle(position_x, position_y)))
                    * ((params.height / 2) - OFFSET));
        x += (params.width / 2);
        y += (params.height / 2);
        draw.position(x, y);
        draw();
      } else {
        mLayout.removeView(draw);
      }
    } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
      mLayout.removeView(draw);
      touch_state = false;
    }
  }
Exemple #3
0
 public void drawStick(double x, double y) {
   draw.position(x, y);
   draw();
 }
  ////////////////////////////////////////////////////////
  // CONSTRUCTOR FOR GUI
  ////////////////////////////////////////////////////////
  public MoveABall() {

    // Set up a panel for the buttons
    JPanel btnPanel = new JPanel(new FlowLayout()); // automatically centers and wraps components

    // Set up a custom drawing JPanel
    canvas = new DrawCanvas();
    canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

    // Add both panels to this JFrame
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(canvas, BorderLayout.CENTER);
    cp.add(btnPanel, BorderLayout.SOUTH);

    // Add Components to btnPanel
    JButton btnUp = new JButton("Move Up ");
    btnPanel.add(btnUp);
    btnUp.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            y -= 10;
            canvas.repaint();
            requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
        });
    JButton btnLeft = new JButton("Move Left ");
    btnPanel.add(btnLeft);
    btnLeft.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            x -= 10;
            canvas.repaint();
            requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
        });
    JButton btnRight = new JButton("Move Right");
    btnPanel.add(btnRight);
    btnRight.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            x += 10;
            canvas.repaint();
            requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
        });
    JButton btnDown = new JButton("Move Down");
    btnPanel.add(btnDown);
    btnDown.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            y += 10;
            canvas.repaint();
            requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
        });

    // "this" JFrame fires KeyEvent
    addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent evt) {
            switch (evt.getKeyCode()) {
              case KeyEvent.VK_LEFT:
                x -= 10;
                repaint();
                break;
              case KeyEvent.VK_RIGHT:
                x += 10;
                repaint();
                break;
              case KeyEvent.VK_UP:
                y -= 10;
                repaint();
                break;
              case KeyEvent.VK_DOWN:
                y += 10;
                repaint();
                break;

              case KeyEvent.VK_A:
                x -= 10;
                repaint();
                break;
              case KeyEvent.VK_D:
                x += 10;
                repaint();
                break;
              case KeyEvent.VK_W:
                y -= 10;
                repaint();
                break;
              case KeyEvent.VK_S:
                y += 10;
                repaint();
                break;
            }
          }
        });

    // Setup the Frame the way we want it

    setTitle("Move a Ball");
    pack(); // automagically resize the frame to fit the components
    setVisible(true); // this line is always after adding components

    requestFocus(); // set the focus to JFrame to receive KeyEvent
  } // end constructor