无法翻译线条(形状)

时间:2016-09-22 00:58:22

标签: java line shapes

对于作业,我们被要求修改现有代码以包含多个形状,多种颜色,并使形状移动。我有点小气,开始添加更多东西,但现在我们遇到了问题。一切顺利,直到我添加了左臂(如屏幕上所示)。当将图形向右移动时,我在前一个地方留下一块手臂有问题。向上,向下和向左都运行良好,只有在我遇到问题的右边。我已经添加了一个屏幕截图,显示了将图形向右移动两次之后的样子。感谢您提供的任何帮助。 roboMan output

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Scanner;
import java.awt.geom.Ellipse2D;  

public class SwingBot1 {

public static void main(String[] args) {

    // contruction of new JFrame object
    JFrame frame = new JFrame();

    // mutators
    frame.setSize(400,400);
    frame.setTitle("SwingBot");

    // program ends when window closes
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Robot r = new Robot();

    frame.add(r);

    // voila!
    frame.setVisible(true);


    // your Scanner-based command loop goes here
    Scanner in = new Scanner(System.in);
    boolean active = true;
    while(active){
        System.out.println("Enter \"up\", \"down\", \"left\", or  \"right\" "
                + "to move.\nClose the window to quit.");
        String direction = in.nextLine();
        switch(direction){
        case "up":
            r.moveBot(0, -20);
            break;
        case "down":
            r.moveBot(0, 20);
            break;
        case "right":
            r.moveBot(20, 0);
            break;
        case "left":
            r.moveBot(-20, 0);
            break;

        }
    }
}
public static class Robot extends JComponent
{
    private Rectangle rect = new Rectangle(45, 30, 20, 20);

    int x = 30;
    int y = 50;
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        // set the color
        g2.setColor(Color.RED);
        // draw the shape`
        g2.fill(rect);

        Graphics2D g3 = (Graphics2D) g;
        g3.setColor(Color.BLUE);
        g3.draw(new Ellipse2D.Double(30, 50, 50, 100));
        g3.fillOval(30, 50, 50, 100);

        Graphics2D g4 = (Graphics2D) g;
        g4.setColor(Color.GREEN);
        g4.draw(new Line2D.Double(70, 141, 90, 190));
        g4.draw(new Line2D.Double(39, 141, 30, 191));

        Graphics2D g5 = (Graphics2D) g;
        g5.setColor(Color.RED);
        g5.draw(new Line2D.Double(80, 85, 125, 70));

        //arm, left as viewed
        g5.draw(new Line2D.Double(0, 70, 30, 85));


    }


    public void moveBot(int x, int y)
    {
        setX(getX()+x);
        setY(getY()+y);
        repaint();
    }

    public int getX()
    {
        return x;
    }
    public void setX(int x)
    {
        this.x = x;
    }
    public int getY()
    {
        return y;
    }
    public void setY(int y)
    {
        this.y = y;
    }
    /*  public void moveBot(int x, int y)
    {
        // move the rectangle 
        rect.translate(x,y);
        // redraw the window
        poly.translate(x, y);
        repaint();

    }*/

  }
}

1 个答案:

答案 0 :(得分:0)

看起来你需要重新绘制框架。