首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我有动画的时候,我如何让对象*不*移动?

当我有动画的时候,我如何让对象*不*移动?
EN

Stack Overflow用户
提问于 2015-12-13 06:18:38
回答 1查看 42关注 0票数 0

在swing图形中,我需要制作一些东西,其中的杆状图形来回移动,但有些东西保持不变。我使用蓝色水平线作为测试。它不会移动,但随着棍形人物的剑,它会及时消失。我怎么才能阻止它?

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class AnimatedBoxman extends JApplet  implements Runnable,ActionListener
{   
   int size=50;

   int x = 0;
   int y =300;
   int side2side = 50;

   Thread t;
   boolean sworddrawn = false;
   JButton myButton = new JButton("Change");
   static Random myRan = new Random();
   public void init()
   {

       Container content = getContentPane();
         content.setBackground(Color.red);
         setLayout(new FlowLayout());
         add(myButton);
         myButton.addActionListener(this);


   }
   public void actionPerformed (ActionEvent ae)
   {
      side2side= -side2side;
     Container content = getContentPane();
     content.setBackground(new Color 
        (Math.abs(myRan.nextInt())%256,
         Math.abs(myRan.nextInt())%256,
         Math.abs(myRan.nextInt())%256));

     repaint();
   }
   public void run( )
   {
     while( true )
     {
      x+= side2side;
      if (x > this.getSize().width-50 || x < 0)
          side2side = -side2side;
      if (x%3 == 0)
          sworddrawn = !sworddrawn;
      repaint( );
      try {
          Thread.sleep(900);
      }
      catch( Exception e ) { }
     }
   }
   public void start( )
   {
    t = new Thread(this);
    t.start( );
   }

   public void paint ( Graphics g )
   {
     super.paint( g );

        g.setColor(Color.blue);
        g.drawLine(x-10, y, x-30,y);
        g.drawLine(x-30, y, x-30, y-20);
        g.drawLine(x-10, y-20, x-10, y);


        g.drawLine(x-10, y-20, x+10, y-20);
        g.drawLine(x-10, y-20, x+10, y-20);
        g.drawLine(x-10, y, x+10, y);
        g.drawLine(x-10, y, x-10, y+20);
        g.drawLine(x, y+20, x, y);
        g.drawLine(x+10, y+20,x+10, y);

        g.drawLine(x+10, y, x+30, y);
        g.drawLine(x+30, y, x+30, y-20);
        g.drawLine(x+10, y-20, x+10, y);

        g.drawLine(x, y+20, x+10, y+20);
        g.drawLine(x-10, y+20, x-10, y+30);
        g.drawLine(x-10, y+30, x, y+30);
        g.drawLine(x, y+30, x, y+20);
        g.drawLine(x, y+30, x+10, y+30);
        g.drawLine(x+10, y+30, x+10, y+20);
        g.drawLine(x-10, y+20, x, y+20);
        g.drawOval(x-7, y-20, 5, 5);
        g.drawOval(x+1, y-20, 5, 5);
        g.drawLine(x-4, y-8, x-5, y-5);
        g.drawLine(x-5, y-8, x+6, y-8);
        g.drawLine(x+6, y-5, x+6, y-8);


     if( sworddrawn )
     {
         g.drawLine(500, 400, 700, 400);
         }
     else
     {


            Polygon myPolygon2;
            myPolygon2=new Polygon();
            myPolygon2.addPoint(x-42,y-184);
            myPolygon2.addPoint(x-32,y-195);
            myPolygon2.addPoint(x-21,y-184);
            g.fillPolygon(myPolygon2);
            g.fillRect(x-41, y-184, 20, 100);
            g.fillArc(x-50, y-104, 40, 20, 180, 180);
            g.fillArc(x-35, y-94, 20, 35, 90, 180);
            g.drawLine(x-30, y-164, x-30, y-24);

            g.drawOval(x-40, y-24, 20, 30);

     }

   }
}
EN

回答 1

Stack Overflow用户

发布于 2015-12-14 21:46:20

蓝色水平线可以在内容窗格之外,在这种情况下不会绘制它。您可以通过向init方法添加一行来检查内容窗格的大小:

代码语言:javascript
复制
System.out.println("getSize(): " + getSize());

在我的笔记本电脑上,大小是400x300像素:

代码语言:javascript
复制
getSize(): java.awt.Dimension[width=400,height=300]

这会产生一条不可见的蓝色水平线:

代码语言:javascript
复制
g.drawLine(500, 400, 700, 400);

X和y坐标值较低时,该线是可见的:

代码语言:javascript
复制
g.drawLine(50, 40, 70, 40);

最后,当您提出问题时,添加应用程序的屏幕截图和一些额外的文本来解释您的问题是什么(例如,您所期望的与您所看到的相比)可能会很有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34245660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档