Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我有动画时,如何使对象*不*移动?_Java_Swing - Fatal编程技术网

Java 当我有动画时,如何使对象*不*移动?

Java 当我有动画时,如何使对象*不*移动?,java,swing,Java,Swing,在swing图形中,我需要制作一些东西,使木棍图形前后移动,但某些东西保持不变。我用蓝色的水平线作为测试。它不动,但随着木棍人物的剑及时消失。我怎样才能阻止它 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class AnimatedBoxman extends JApplet implements Runnable,ActionListener {

在swing图形中,我需要制作一些东西,使木棍图形前后移动,但某些东西保持不变。我用蓝色的水平线作为测试。它不动,但随着木棍人物的剑及时消失。我怎样才能阻止它

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);

     }

   }
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.*;
公共类AnimatedBoxman扩展了JApplet,实现了可运行的ActionListener
{   
int size=50;
int x=0;
int y=300;
int side2side=50;
螺纹t;
布尔值sworddrawn=false;
JButton myButton=新JButton(“更改”);
静态随机myRan=新随机();
公共void init()
{
容器内容=getContentPane();
内容。挫折背景(颜色:红色);
setLayout(新的FlowLayout());
添加(我的按钮);
myButton.addActionListener(这个);
}
已执行的公共无效行动(行动事件ae)
{
side2side=-side2side;
容器内容=getContentPane();
内容。挫折背景(新颜色)
(Math.abs(myRan.nextInt())%256,
Math.abs(myRan.nextInt())%256,
abs(myRan.nextInt())%256));
重新油漆();
}
公开作废运行()
{
while(true)
{
x+=侧2侧;
如果(x>this.getSize().width-50 | | x<0)
side2side=-side2side;
如果(x%3==0)
sworddrawn=!sworddrawn;
重新油漆();
试一试{
睡眠(900);
}
捕获(例外e){}
}
}
公共无效开始()
{
t=新螺纹(本螺纹);
t、 开始();
}
公共空间涂料(图g)
{
超级油漆(g);
g、 setColor(Color.blue);
g、 抽绳(x-10,y,x-30,y);
g、 抽绳(x-30,y,x-30,y-20);
g、 抽绳(x-10、y-20、x-10、y);
g、 抽绳(x-10、y-20、x+10、y-20);
g、 抽绳(x-10、y-20、x+10、y-20);
g、 抽绳(x-10,y,x+10,y);
g、 抽绳(x-10,y,x-10,y+20);
g、 抽绳(x,y+20,x,y);
g、 抽绳(x+10,y+20,x+10,y);
g、 抽绳(x+10,y,x+30,y);
g、 抽绳(x+30,y,x+30,y-20);
g、 抽绳(x+10,y-20,x+10,y);
g、 抽绳(x,y+20,x+10,y+20);
g、 抽绳(x-10,y+20,x-10,y+30);
g、 抽绳(x-10,y+30,x,y+30);
g、 抽绳(x,y+30,x,y+20);
g、 抽绳(x,y+30,x+10,y+30);
g、 抽绳(x+10,y+30,x+10,y+20);
g、 抽绳(x-10,y+20,x,y+20);
g、 牵引椭圆(x-7,y-20,5,5);
g、 牵引椭圆(x+1,y-20,5,5);
g、 抽绳(x-4、y-8、x-5、y-5);
g、 抽绳(x-5、y-8、x+6、y-8);
g、 抽绳(x+6,y-5,x+6,y-8);
如果(宣誓)
{
g、 抽绳(500400700400);
}
其他的
{
多边形myPolygon2;
myPolygon2=新多边形();
myPolygon2.添加点(x-42,y-184);
myPolygon2.添加点(x-32,y-195);
myPolygon2.添加点(x-21,y-184);
g、 填充多边形(myPolygon2);
g、 fillRect(x-41,y-184,20,100);
g、 圆角弧(x-50,y-104,40,20,180,180);
g、 圆角弧(x-35,y-94,20,35,90,180);
g、 抽绳(x-30、y-164、x-30、y-24);
g、 drawOval(x-40,y-24,20,30);
}
}
}

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

System.out.println("getSize(): " + getSize());
在我的笔记本电脑上,大小为400 x 300像素:

getSize(): java.awt.Dimension[width=400,height=300]
这将产生一条不可见的蓝色水平线:

g.drawLine(500, 400, 700, 400);
如果x和y坐标的值较低,则该线可见:

g.drawLine(50, 40, 70, 40);

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

请解释您所说的“如何停止”是什么意思