Okay, so programming was never my strong side, but I have to get it done for next tuesday.
So the idea behind my program is:
There is a class for drawing a square, then there is the ship class that extends the square (assembles 5 of them into a ship)
and theres the one that i've got a problem with. A fleet of ships which is basically a set of 2 or more ships (sets of 5 squares) that can move together.
My code:
Square:
package assignment1; import java.awt.*; public class Square { private double x,y; public Color color; private int side; public Square() { x = 100; y = 100; side = 5; color = Color.orange; } public void draw(Graphics g) { g.setColor(color); g.fillRect( (int)(x), (int)(y), (int)(2*side), (int)(2*side) ); } public void setSide(int s) { side = s; } public int getSide() { return side; } public int getX() { return (int) x; } public int getY() { return (int) y; } public void setX(int xIn) { x = xIn; } public void setY(int setY) { y = setY; } }
Ship:
package assignment1; import java.awt.*; public class Ship extends Square { public Ship() { super(); } public void draw(Graphics g) { int side = super.getSide(); int x = super.getX(); int y = super.getY(); g.setColor(color); g.fillRect( (int)(x), (int)(y), (int)(2*side), (int)(2*side) ); g.setColor(color); g.fillRect( (int)(x), (int)(y - (2*side)), (int)(2*side), (int)(2*side) ); g.setColor(color); g.fillRect( (int)(x - (2*side)), (int)(y - (2*side)), (int)(2*side), (int)(2*side) ); g.setColor(color); g.fillRect( (int)(x + (2*side)), (int)(y - (2*side)), (int)(2*side), (int)(2*side) ); g.setColor(color); g.fillRect( (int)(x), (int)(y - (4*side)), (int)(2*side), (int)(2*side) ); } }
And the FleetOfShips:
package assignment1; import java.awt.*; public class FleetOfShips { Ship[] fleetMembers; int total = 5; FleetOfShips() { fleetMembers = new Ship[total]; for (int i = 0; i < total; i = i++) { fleetMembers[i] = new Ship(); fleetMembers[i].setX (i * 10); fleetMembers[i].setY (i * 10); } } public void draw(Graphics g) { for (int i = 0; i < total; i = i++) { fleetMembers[i].draw(g); } } public void goSouth(int howFar) { for (int i = 0; i < total; i = i + 1) { fleetMembers[i].setY (fleetMembers[i].getY() + howFar); } } }
All of these 3 seem fine, they compile with no problems and first two work fine.
How do I know that the square and the ship are fine? I use this piece of code to check if everything is working:
package assignment1; import java.awt.event.*; import java.applet.Applet; import java.awt.*; import javax.swing.*; public class AnimateInTabSquare extends JApplet implements ActionListener, MouseListener, MouseMotionListener { JPanel display; JTabbedPane tabpane; JEditorPane helpPane; Square mySquare; private int x; private int y; private Timer timer = new Timer(100,this); public AnimateInTabSquare() // constructor { display = new JPanel() { public void paintComponent(Graphics g) { drawFrame(g); } }; //getContentPane().add(display); } public void init( ) { display.addMouseListener(this); display.addMouseMotionListener(this); timer.setDelay(10); timer.start(); mySquare = new Square(); tabpane = new JTabbedPane( ); helpPane = new JEditorPane(); // allow embedded html in your rendered text helpPane.setContentType( "text/html" ); String helpText; helpText = "<html><body>line 1 of help<br>"; helpText = helpText + "<em> another lne of help </em><br>"; helpText = helpText + "last line of help</body></html>"; helpPane.setText(helpText); // add tabs tabpane.addTab( "Game", display ); tabpane.addTab( "Help", helpPane ); // add tabbed pane to applet add( tabpane, BorderLayout.CENTER); } public void drawFrame(Graphics g) { // to compute and draw the next frame in the animation. g.setColor(Color.black); // fill the applet with a black background. g.fillRect(0, 0, getSize().width, getSize().height); // place here code that should happen every time that the timer makes the frame get re-drawn // Ask the myFleet object to draw itself. mySquare.draw(g); } // end drawFrame() // empty methods below must be here for mouseEventListener public void mousePressed(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { if (mySquare.getSide() == 5) mySquare.setSide(50); else mySquare.setSide(5); display.repaint(); } public void actionPerformed(ActionEvent evt) // triggered by timer { //display.repaint(); } }
This works perfectly fine for my "square" and "ship" classes, but unfortunately I am unable to make it work with the FleetOfShips class.
Could anyone help me please?
I really need to get this thing done.
All I need is to get the fleet of ships (2 or more ships) to be drawn in an applet and react to a mouse click (move, change colour etc)
I spent like my entire day trying to figure this out, but I can't :/
@EDIT
I am almost sure that the problem lies somewhere around this part:
FleetOfShips() { fleetMembers = new Ship[total]; for (int i = 0; i < total; i = i++) { fleetMembers[i] = new Ship(); fleetMembers[i].setX (i * 10); fleetMembers[i].setY (i * 10); } }