平滑地移动图形而不是Java中的跳过

时间:2017-03-05 05:06:56

标签: java swing graphics awt repaint

我正在练习使用Java移动图形,我似乎遇到了问题。我现在画的是两个外星人的面孔。目前,我的程序设置为根据用户输入在特定方向上一次移动面部1步。我想做的是让异形面从A点移动到B点。例如,现在它一次移动1步,因为我的面部分在x和y上增加1。我的想法是在方法MoveHorizo​​ntal()之前开始一个循环,这样它会运行很多次,我会从一个点平滑过渡到另一个点。虽然这对我不起作用。它会从它开始的地方跳到循环结束的地方而不是一次一步。所以我的问题是,我怎么能这样做?大声笑。谢谢。

//First Class
import javax.swing.JFrame;
import java.util.Scanner;


public class AlienFace
{
    public static void main(String[] args)
    {
       String userInput;
       Scanner input = new Scanner(System.in);
       JFrame frame = new JFrame();
       frame.setSize(700, 700);
       frame.setTitle("An Alien Face");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


       FaceComponent component = new FaceComponent();
       frame.add(component);

       frame.setVisible(true);

       System.out.println("Which direction would you like the Alien faces to move? ");
       System.out.print("H/h for horizontally, V/v for vertically, D/d for diagonally, or Q/q to quit: ");
       userInput = input.nextLine();
       System.out.println(userInput);

       while (!userInput.equals("q") || !userInput.equals("Q"))
       {
           System.out.println("Which direction would you like the Alien faces to move? ");
           System.out.print("H/h for horizontally, V/v for vertically, D/d for diagonally, or Q/q to quit: ");
           userInput = input.nextLine();

           if (userInput.equals("H") || userInput.equals("h"))
           {
               component.MoveHorizontal();
               frame.repaint();
           }



           if (userInput.equals("V") || userInput.equals("v"))
           {
               component.MoveVertical();
               frame.repaint();
           }

           if (userInput.equals("D") || userInput.equals("d"))
           {
               component.MoveDiagonal();
               frame.repaint();
           }
       }

       if (userInput.equals("q") || userInput.equals("Q"))
       {
           frame.setVisible(false);
       }
   } 

}

//Second Class
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.JComponent;

public class FaceComponent extends JComponent
{
    int head2X = 5;
    int head2Y = 10;
    int head3X = 105;
    int head3Y = 115;
    int eye2X = 25;
    int eye2Y = 75;
    int eye3X = 125;
    int eye3Y = 175;
    int mouth2X1 = 30;
    int mouth2Y1 = 110;
    int mouth3X1 = 130;
    int mouth3Y1 = 210;
    int mouth2X2 = 80;
    int mouth2Y2 = 110;
    int mouth3X2 = 180;
    int mouth3Y2 = 210;

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    Graphics2D g3 = (Graphics2D) g;

    Ellipse2D.Double head = new Ellipse2D.Double(head2X, head2Y, 100, 150);
    g2.draw(head);

    Ellipse2D.Double head1 = new Ellipse2D.Double(head3X, head3Y, 100, 150);
    g3.draw(head1);

    g2.setColor(Color.GREEN);
    Rectangle eye = new Rectangle(eye2X, eye2Y, 15, 15);
    g2.fill(eye);
    eye.translate(50, 0);
    g2.fill(eye);

    g3.setColor(Color.GREEN);
    Rectangle eye1 = new Rectangle(eye3X, eye3Y, 15, 15);
    g3.fill(eye1);
    eye1.translate(50, 0);
    g3.fill(eye1);

    Line2D.Double mouth = new Line2D.Double(mouth2X1, mouth2Y1, mouth2X2, mouth2Y2);
    g2.setColor(Color.RED);
    g2.draw(mouth);

    Line2D.Double mouth1 = new Line2D.Double(mouth3X1, mouth3Y1, mouth3X2, mouth3Y2);
    g3.setColor(Color.RED);
    g3.draw(mouth1);

    g2.setColor(Color.BLUE);
    g2.drawString("Hello, World!", 5, 175);
}

public void MoveHorizontal()
{
    head2X++;
    head3X++;
    eye2X++;
    eye3X++;
    mouth2X1++;
    mouth3X1++;
    mouth2X2++;
    mouth3X2++;

}

public void MoveVertical()
{
    head2Y++;
    head3Y++;
    eye2Y++;
    eye3Y++;
    mouth2Y1++;
    mouth3Y1++;
    mouth2Y2++;
    mouth3Y2++;
}

public void MoveDiagonal()
{
    head2X++;
    head3X++;
    eye2X++;
    eye3X++;
    mouth2X1++;
    mouth3X1++;
    mouth2X2++;
    mouth3X2++;
    head2Y++;
    head3Y++;
    eye2Y++;
    eye3Y++;
    mouth2Y1++;
    mouth3Y1++;
    mouth2Y2++;
    mouth3Y2++;
}

}

0 个答案:

没有答案