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.

4

1 回答 1

0

让我们从您已经覆盖paint并且也未能调用super.paint并继续它的事实开始String,Java 中的比较不是用,==而是用equals,例如

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

应该

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

如果您要覆盖paint顶级容器(如JFrameor JApplet),则应避免这样做。相反,您应该创建一个自定义组件,例如JPanel并覆盖它的paintComponent方法,确保您也调用super.paintComponent,否则您将以一些讨厌的绘画工件结束

请查看执行自定义绘画以获取更多详细信息...

于 2013-10-15T00:01:43.663 回答