Jamie Mcallianton
Jamie Mcallianton

Reputation: 3

Moving a graphics 2d object with threads

I am not quite sure why my boat is not moving. If anyone could explain in detail that would be great.

This is what creates and shows the boat:

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
}

This is my sailboat class that draws a boat with poly lines.

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

Upvotes: 0

Views: 1182

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347294

So, based on the snippets of code of two things jump out of me...

  1. Why does SailBoat extend Applet, when you are calling it's draw method from boatApplet
  2. Calling repaint within SailBoat will do nothing, as it's not actually a displayable component (it's not "added" to anything and based on how you are trying to use, it should be)

    • Remove extends Applet from SailBoat, it's only confusing the matter
    • Move the Thread to the boatApplet, have it call a move method on the SailBoat which will update the internal position of the object, then have boatApplet call repaint on itself
    • Call super.paint before doing any custom painting
    • Use a Swing Timer instead of a Thread, it updates within the context of the EDT making it safer to update the state of the UI. See Concurrency in Swing and How to use Swing Timers for more details
    • Avoid applets if you can, they have a bunch of there own issues which you probably don't need to deal with right now
    • Move your core logic to a JPanel and override its paintComponent method (and call super.paintComponent) and get double buffering for free, then you can add this panel to what ever top level container you want

Upvotes: 2

Related Questions