2017-11-30 5 views
1

だから、私はスティックマンをユーザー入力で動かす必要があります。ユーザーが部品(頭、手、足、後)をクリックして移動する必要があり、これについてどうやって行くのか分かりません。 可能であれば、文字の周りに閉じ込める必要があります。各パーツをどのくらい引っ張ることができるかには限界があります。 私のコードは以下を参照してください。このStickmanをインタラクティブにするにはどうすればいいですか?

// Created by Charlie Carr - (28/11/17 - /11/17) 
    import java.awt.*; 
    import java.applet.Applet; 
    import javax.swing.*; 
    import java.awt.Graphics; 
    import java.awt.geom.*; 
    import java.awt.image.BufferedImage; 
    //Imports complete 

    //Suppress warning about undeclared static final serialVersionUID field in VS Code 
    @SuppressWarnings("serial") 
    public class Animator extends JPanel { 
      public static class AnimatorWindow extends JPanel { 
        public void paint(Graphics page) { 
          setBackground(Color.gray); 
          setForeground(Color.white); 
          super.paintComponent(page); 
          page.drawString("Stickmen Animation Station", 150, 15); 
          //draw the head 
          //x1, y1, x2, y2 
          page.drawOval(90, 60, 20, 20); 
          // draw the body 
          page.drawLine(100, 80, 100, 110); 
          // draw the hands 
          page.drawLine(100, 90, 80, 105); 
          page.drawLine(100, 90, 120, 105); 
          //draw the legs, he hasn't a leg to stand on.. 
          page.drawLine(100, 110, 85, 135); 
          page.drawLine(100, 110, 115, 135); 
        } 
      } 

      public static void main(String[] args) { 
        AnimatorWindow displayPanel = new AnimatorWindow(); 

        JPanel content = new JPanel(); 
        content.setLayout(new BorderLayout()); 
        content.add(displayPanel, BorderLayout.CENTER); 
        //declare window size 
        int x = 480; 
        int y = 240; 

        JFrame window = new JFrame("GUI"); 
        window.setContentPane(content); 
        window.setSize(x, y); 
        window.setLocation(101, 101); 
        window.setVisible(true); 

      } 

    } 

答えて

1

マウスイベントの処理にはMouseListenerを使用してください。

paint()も境界線などの塗りつぶしを行うため、paint()の代わりにpaintComponent()メソッドをオーバーライドする必要があります。スイングイベントの詳細については

public static class AnimatorWindow extends JPanel implements MouseListener{ 
    public AnimatorWindow(){ 
     setBackground(Color.gray); 
     setForeground(Color.white); 
     //add the listener 
     addMouseListener(this); 
    } 
    public void paintComponent(Graphics page) { 
     super.paintComponent(page); 
     //You should not alter the Graphics object passed in 
     Graphics2D g = (Graphics2D) page.create(); 

     //draw your stuff with g 
     g.drawString("Stickmen Animation Station", 150, 15); 
     ....... 

     //finish 
     g.dispose(); 
    } 


    public void mouseEntered(MouseEvent e){} 
    public void mouseExited(MouseEvent e){} 
    public void mousePressed(MouseEvent e){} 
    public void mouseReleased(MouseEvent e){} 
    public void mouseClicked(MouseEvent e){ 
     //implement your clicking here 
     //Use e.getX() and e.getY() to get the click position 
    } 
} 

this site

EDITチェック:あなたの問題はまた、アニメーションを含み、そしてあなたがそれを行うためにjavax.swing.Timerを使用することができます。

関連する問題