Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Swing_Graphics - Fatal编程技术网

Java 使用线程移动图形二维对象

Java 使用线程移动图形二维对象,java,multithreading,swing,graphics,Java,Multithreading,Swing,Graphics,我不太清楚为什么我的船不动了。 如果有人能详细解释,那就太好了 这就是创建和显示船的原因: import java.awt.Color; import java.awt.Graphics; import javax.swing.JApplet; @SuppressWarnings("serial") public class boatApplet extends JApplet{ public void paint(Graphics g) { setBackgrou

我不太清楚为什么我的船不动了。 如果有人能详细解释,那就太好了

这就是创建和显示船的原因:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;


@SuppressWarnings("serial")
public class boatApplet extends JApplet{

    public void paint(Graphics g) {
        setBackground(Color.blue);
        // Xposition, YPosition, widthofboat, hieght of boat
        SailBoat boat = new SailBoat(25,500, 350, 400);
        boat.draw(g);
    } //ends method
}
这是我的帆船课,用多边形线条画一艘船

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;


@SuppressWarnings("serial")
/*
 * sailboat class extends applet intx is starting x position inty is starting y
 * position boatW is the length of the boat boatH is the boat height - the boat
 * and cabin height polyline is used to draw the boat
 */
public class SailBoat extends Applet implements Runnable{
    public int x;
    public int y;
    public int boatW;
    public int boatH;
    private GeneralPath polyline;

    // constructor takes in x pos, y pos, width of boat, height of boat
    public SailBoat(int xx, int yy, int w, int h) {
        setBackground(Color.blue);
        boatW = w;
        boatH = h;
        x = xx;
        y = yy;
        Thread t = new Thread(this);
        t.start();
    }

    public void draw(Graphics g) {
        /*
         * This method builds the boat from various polyline actionscreaes a
         * graphics 2d object anduses to build the boatrectangle with width of
         * 200 for skybuild the boat basebuild the cabinbuild themastbuild the
         * front sailbuild the backsail
         */
        Graphics2D g2d = (Graphics2D) g;
        // sky
        g2d.setColor(new Color(135, 206, 250));
        // color light blue
        Rectangle2D.Double sky = new Rectangle2D.Double(0, 0, 2000, 520);
        // draws a regtangle
        g2d.draw(sky);
        // draws it to g2d
        g2d.fill(sky);
        // fills with color

        g2d.setColor(Color.BLACK);
        // change color to black
        g2d.setStroke(new BasicStroke(2));
        // set stroke size 2

        // boat base
        int[] xBoat = { x, x + boatW, x + boatW - 25, x + 25, x };
        // x pos array
        int[] yBoat = { y, y, y + 50, y + 50, y };
        // y pos array
        polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xBoat.length);
        polyline.moveTo(xBoat[0], yBoat[0]);
        // moves polyline to first position of array x.y
        for (int index = 1; index < yBoat.length; index++) {
            // for each pos
            polyline.lineTo(xBoat[index], yBoat[index]);
            // draw the lines
        }
        ; // ends loop
        polyline.closePath();
        // closes poly
        g2d.draw(polyline);
        // draws it
        g2d.setColor(Color.white);
        // colors the boat base white
        g2d.fill(polyline);
        // fills the poly shape

        // cabin
        g2d.setColor(Color.black);
        // change color to black
        // cabins x position is x + 60% of the boats length on -1 y to
        // compensate for stroke size
        int[] xCabin = { (int) (x + (boatW * .6)) - 10,
                (int) (x + (boatW * .6)),
                (int) ((x + (boatW * .6)) + (boatW * .15)),
                (int) ((x + (boatW * .6)) + (boatW * .15)) + 10,
                (int) (x + boatW * .6) };
        // cabin y pos
        int[] yCabin = { y - 1, y - 25, y - 25, y - 1, y - 1 };
        polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xCabin.length);// gp
        polyline.moveTo(xCabin[0], yCabin[0]);
        // move poly line
        for (int index = 1; index < yCabin.length; index++) {
            // for each pos
            polyline.lineTo(xCabin[index], yCabin[index]);
            // draws lines
        }
        ; // ends loop
        polyline.closePath();
        // close path
        g2d.draw(polyline);
        // draws the poly to g2d
        g2d.setColor(Color.WHITE);
        // sets color white
        g2d.fill(polyline);
        // fills cabin

        // mast
        g2d.setColor(Color.BLACK);
        // set color black
        g2d.setStroke(new BasicStroke(4));
        // set stroke size for mast to 4
        g2d.drawLine((int) (x + (boatW * .65)), y - 28,
                (int) (x + (boatW * .65)), (int) (y - boatH + 50));
        // draws a line from top of cabin to the remainder of boats height

        // frontsail
        g2d.setStroke(new BasicStroke(2));
        // set stroke to size 2
        // front sail x position is based on mast pos - 5
        int[] xFSail = { (int) (x + (boatW * .65) - 5), x,
                (int) (x + (boatW * .65) - 5), (int) (x + (boatW * .65) - 5) };
        // front sail y position is set to height to 30 from boat base
        int[] yFSail = { (y - boatH + 50), y - 35, y - 35, (y - boatH + 50) };
        // poly gp
        polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xFSail.length);
        polyline.moveTo(xFSail[0], yFSail[0]);
        // moves fs poly line
        for (int index = 1; index < yFSail.length; index++) {
            // for each pos
            polyline.lineTo(xFSail[index], yFSail[index]);
            // draw line to
        }
        ; // ends loop
        polyline.closePath();
        // closes path
        g2d.draw(polyline);
        // draws path to g2d
        g2d.setPaint(new GradientPaint(x, y, Color.white, 450, y, Color.black));
        // set paint as gradient
        // fills sail
        g2d.fill(polyline);

        // backsail
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(2));
        int[] xBSail = { (int) (x + (boatW * .65) + 5),
                (int) (x + (boatW * .65) + 5), x + boatW,
                (int) (x + (boatW * .65) + 5) };
        int[] yBSail = { (y - boatH + 50), y - 35, y - 35, (y - boatH + 50) };
        polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xBSail.length);
        polyline.moveTo(xBSail[0], yBSail[0]);
        for (int index = 1; index < yBSail.length; index++) {
            polyline.lineTo(xBSail[index], yBSail[index]);
        }
        ; // ends loop
        polyline.closePath();
        g2d.draw(polyline);
        g2d.setPaint(new GradientPaint(x, y, Color.white, 300, 200, Color.black));
        g2d.fill(polyline);
    }

    @Override
    public void run() {
        for(int time = 0; time < 1000; time += 10){
            x += 1;
            repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }
}
import java.applet.applet;
导入java.awt.BasicStroke;
导入java.awt.Color;
导入java.awt.GradientPaint;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.geom.GeneralPath;
导入java.awt.geom.Rectangle2D;
@抑制警告(“串行”)
/*
*帆船类扩展小程序intx开始x位置inty开始y
*position boatW是船的长度boatH是船的高度-船
*船舱高度多段线用于绘制船
*/
公共级帆船可运行{
公共int x;
公共智力;
公共内河;
公共内河;
专用通用路径多段线;
//建造师采用x位置、y位置、船宽、船高
公共帆船(国际xx、国际yy、国际w、国际h){
挫折背景(颜色:蓝色);
boatW=w;
波阿斯=小时;
x=xx;
y=yy;
螺纹t=新螺纹(此);
t、 start();
}
公共空间绘制(图g){
/*
*此方法从各种多段线操作区域构建船
*图形2d对象,用于构建宽度为
*200天建造船坞建造船舱建造最高层建造
*前帆和后帆
*/
Graphics2D g2d=(Graphics2D)g;
//天空
g2d.setColor(新颜色(135206250));
//浅蓝色
矩形2D.Double天空=新矩形2D.Double(0,0,2000,520);
//绘制一个正则表达式
g2d.绘制(天空);
//将其绘制到g2d
g2d.填充(天空);
//充满色彩
g2d.setColor(Color.BLACK);
//把颜色改成黑色
g2d.设定行程(新基本行程(2));
//设置笔划大小2
//船底
int[]xBoat={x,x+boatW,x+boatW-25,x+25,x};
//x pos阵列
int[]yBoat={y,y,y+50,y+50,y};
//y-pos阵列
多段线=新的GeneralPath(GeneralPath.WIND\u偶数\u奇数,xBoat.length);
polyline.moveTo(xBoat[0],yBoat[0]);
//将多段线移动到阵列x.y的第一个位置
for(int index=1;index