0

This is the bit of code that does all of the drawing, but it's only drawing half of the arrow. When it's a vertical arrow (North or South), it cuts off the left side, and when it's horizontal (East or West) it cuts of the top, both centered right at the tip; becoming a perfect half an arrow.

public void paint(Graphics g) {
        setSize(100, steps * 50);
        for (int i = 1; i < steps + 1; i++) {
            y = i * 20;

            if (directions.peek() == "North") {
                int[] xNPoints = {x, x + 6, x + 2, x + 2, x + 2, x + 2, x + 6};
                int[] yNPoints = {y - 5,  y, y, y, y + 10, y, y};
                g.drawPolygon(xNPoints, yNPoints, 7);
            }
            else if (directions.peek() == "East") {
                int[] xEPoints = {x + 10, x + 5, x + 5, x - 5, x - 5, x + 5, x + 5};
                int[] yEPoints = {y, y + 6, y + 2, y + 2, y + 2, y + 2, y + 6};
                g.drawPolygon(xEPoints, yEPoints, 7);
            }
            else if (directions.peek() == "South") {
                int[] xSPoints = {x, x + 6, x + 2, x + 2, x + 2, x + 2, x + 6};
                int[] ySPoints = {y + 5, y, y, y - 10, y - 10, y, y};
                g.drawPolygon(xSPoints, ySPoints, 7);
            }
            else if (directions.peek() == "West") {
                int[] xWPoints = {x - 5, x, x, x + 10, x + 10, x, x};
                int[] yWPoints = {y, y + 6, y + 2, y + 2, y + 2, y + 2, y + 6};
                g.drawPolygon(xWPoints, yWPoints, 7);
            }
            g.drawString((String)directions.pop(), 5, y + 5);

The goal of the program is to return a set of user provided directions in reverse order, and I have a lot to clean up (add exceptions, clean up the GUI), but all of the code aside from what I just provided is used for prompting and storing the user's input for how many steps there are and what they are.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Let's start with the fact the you've overridden paint and also failed to call super.paint and move onto it String comparison in Java is not done with == but with equals, for example

if (directions.peek() == "North") {

Should be

if ("North".equals(directions.peek())) {

If you are overriding paint of a top level container (like JFrame or JApplet), you should avoid doing so. Instead, you should create a custom component, like JPanel and override it's paintComponent method, making sure that you also call super.paintComponent, otherwise you will end with some nasty paint artifacts

Take a look at Performing Custom Painting for more details...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The string comparison was definitely an oversight. Thanks. I'm still learning the very basics of this GUI stuff, and our teacher didn't hardly go into it, so I think this resource should be helpful. Thank you. I couldn't find the duplicate post you said there was though. Could I get a link? – user2880732 Oct 15 '13 at 16:27
  • At the top of your question, there is now a link... – MadProgrammer Oct 15 '13 at 19:17
  • That isn't what I'm asking here. I realized I shouldn't have been using the == operator, and I've changed that now. The problem is still that only half the drawing is showing up, and there is an infinite loop of errors after paint starts. – user2880732 Oct 19 '13 at 20:31
  • In order to provide any other possible solutions, we're going to need to see a runnable example, this way we can run the code through a debugger – MadProgrammer Oct 19 '13 at 20:33