使两个绘制的对象在Java中移动

时间:2017-03-22 14:53:55

标签: java

我希望你能帮助我。我写了两个课程,目的是让两个外星人的脸在框架内移动。移动将基于用户输入。我的程序运行,但不会发生面部的移动。我不确定外星人面部的x和y坐标的翻译是否有问题。我的问题是:

我是否正确实施翻译,我该如何解决?

这两个课程如下。感谢您的时间和帮助!

import javax.swing.JFrame;
import java.util.Scanner;


/**
 * This program will print two alien faces in a frame and do one of several actions
 * based on user input.
 * @author ryanhabershaw
 *
 */

 public class FaceTester
 {
	 /**
	  * This is the main method of the program. No return.
	  * @param args
	  */
	 public static void main(String[] args)
	 {	 
		 Scanner in = new Scanner(System.in);
		 System.out.println("Enter q to terminate program. ");
		 System.out.println();
		 System.out.println("Enter d to make the faces move diagonally. ");
		 System.out.println();
		 System.out.println("Enter h to make the faces move horizontally. ");
		 System.out.println();
		 System.out.println("Enter v to make the faces move vertically. ");
		 System.out.println();
		 
		 JFrame frame = new JFrame();
		 frame.setSize(500, 500);
		 frame.setTitle("Two Alien Faces");
		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		 FaceComponents component = new FaceComponents();
		 
		 
		 
		 int control = 5;
		 while(control < 6)
		 {
			 System.out.println("Please enter an input: ");
			 System.out.println();
			 String user = in.next();
			 if(user.equals("q") || (user.equals("Q"))) 
			 {
				 control++;
				 System.out.println("Thanks for playing! The program has now terminated.");
			 }
			 else if(user.equals("d") || (user.equals("D")))
			 {
				 frame.add(component);
				 FaceComponents.translate(100,200);
				 
				 frame.setVisible(true);
			 }
			 else if(user.equals("v") || (user.equals("V")))
			 {
				 frame.add(component);
				 FaceComponents.translate(0,100);
				 
				 frame.setVisible(true);
			 }
			 else if(user.equals("h") || (user.equals("H")))
			 {
				 frame.add(component);
				 FaceComponents.translate(100,0);
				 
				 frame.setVisible(true);
			 }
		 }
		 
		 in.close();
 	}
 }

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 FaceComponents extends JComponent
{  
	/**
	 * This is the method where both of the alien faces and 
	 * their features are created. No return.
	 * @param g
	 */
	public void paintComponent(Graphics g)
	{  
		Graphics2D g2 = (Graphics2D) g;

		
		Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150);
		g2.draw(head);

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


    	Line2D.Double mouth = new Line2D.Double(30, 110, 80, 110);
    	g2.setColor(Color.RED);
    	g2.draw(mouth);

    	//Separation between the heads for organization purposes.

    	g2.setColor(Color.BLUE);
    	g2.drawString("Greetings Earthling", 5, 175);
    	
    	Ellipse2D.Double secondHead = new Ellipse2D.Double(150, 10, 100, 150);
		g2.draw(secondHead);

		g2.setColor(Color.RED);
    	Rectangle secondEye = new Rectangle(165, 70, 15, 15);
    	g2.fill(secondEye);
    	secondEye.translate(50, 0);
    	g2.fill(secondEye);


    	Line2D.Double secondMouth = new Line2D.Double(225, 110, 175, 110);
    	g2.setColor(Color.ORANGE);
    	g2.draw(secondMouth);
    	
    	g2.setColor(Color.BLUE);
    	g2.drawString("Take us to your leader", 150, 175);
   }

	public static void translate(int i, int j) 
	{
		
	}

	
 }

0 个答案:

没有答案