我不太确定为什么我的船不动。
如果有人可以详细解释,那就太好了。
这就是创建并显示船的内容:
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) {
}
}
}
}
最佳答案
因此,基于两段代码的片段,我跳出了...
为什么SailBoat
扩展Applet
,当您从draw
调用boatApplet
方法时
在repaint
中调用SailBoat
不会执行任何操作,因为它实际上不是可显示的组件(它不会“添加”到任何东西,并且应该根据您的使用方式而定)
从extends Applet
中删除SailBoat
,这只会使事情变得混乱
将Thread
移到boatApplet
,让它在move
上调用SailBoat
方法,这将更新对象的内部位置,然后让boatApplet
对其自身调用repaint
进行任何自定义绘画之前,请致电super.paint
使用Swing Timer
而不是Thread
,它会在EDT上下文中进行更新,从而更安全地更新UI状态。有关更多详细信息,请参见Concurrency in Swing和How to use Swing Timers
如果可以,请避免使用applet,因为applet存在很多问题,您现在可能不需要处理这些问题。
将您的核心逻辑移至JPanel
并覆盖其paintComponent
方法(并调用super.paintComponent
)并免费获得双缓冲,然后可以将此面板添加到所需的顶级容器中