我不太清楚为什么我的船不动。 如果有人能够详细解释这将是伟大的。
这就是创造和展示船的原因:
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) {
}
}
}
}
答案 0 :(得分:2)
所以,根据两件事的代码片段跳出来......
SailBoat
Applet
方法时,为什么draw
会延长boatApplet
在repaint
内拨打SailBoat
将不会做任何事情,因为它实际上并不是一个可显示的组件(它没有&#34;添加到#34;}并根据你的使用方式,它应该是)
extends Applet
移除SailBoat
,只会让此事感到困惑Thread
移至boatApplet
,让它在move
上调用SailBoat
方法,这将更新对象的内部位置,然后{{1}自己调用boatApplet
repaint
super.paint
而不是Timer
,它会在EDT的上下文中更新,从而更新UI的状态更安全。有关详细信息,请参阅Concurrency in Swing和How to use Swing Timers Thread
并覆盖其JPanel
方法(并调用paintComponent
)并免费获得双缓冲,然后您可以将此面板添加到最高级别你想要的容器