gpt4 book ai didi

java - 使用线程移动图形 2d 对象

转载 作者:行者123 更新时间:2023-12-02 04:36:33 25 4
gpt4 key购买 nike

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

这就是创建并显示船的内容:

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
}

这是我的帆船类(class),它用多段线绘制一艘船。

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) {
}
}
}
}

最佳答案

所以,根据我跳出来的两件事的代码片段......

  1. 当您从 boatApplet 调用 SailBoatdraw 方法时,为什么 SailBoat 会扩展 Applet
  2. SailBoat 中调用 repaint 不会执行任何操作,因为它实际上不是可显示的组件(它不会“添加”到任何内容,并且基于您的尝试方式)使用,应该是)

    • SailBoat 中删除 extends Applet,这只会让事情变得困惑
    • Thread移动到boatApplet,让它调用SailBoat上的move方法来更新对象的内部位置,然后让 boatApplet 对其自身调用 repaint
    • 在进行任何自定义绘画之前调用 super.paint
    • 使用 Swing Timer 而不是 Thread,它在 EDT 的上下文中更新,从而使更新 UI 状态更安全。请参阅Concurrency in SwingHow to use Swing Timers了解更多详情
    • 如果可以的话,避免使用小程序,它们有很多自己的问题,您现在可能不需要处理这些问题
    • 将核心逻辑移至 JPanel 并重写其 paintComponent 方法(并调用 super.paintComponent)并免费获得双缓冲,然后您可以将此面板添加到您想要的任何顶级容器

关于java - 使用线程移动图形 2d 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com