计数器更新/重新绘制图形

时间:2016-06-06 11:42:25

标签: java swing graphics counter

我创建了一个Hangman游戏,其中玩家输入一个单词而另一个玩家试图猜出该单词。屏幕上有26个JButton(字母表中的每个字母)。

每次选择正确的字母时,计数器变量都会递增。 计数器变量的数量决定了刽子手的绘制量(7是完全绘制刽子手的时候)。当选择了正确的字母时,我可以让计数器递增但是当它增加时我无法得到挂起的部分刽子手。

这是我的Hangman类,用于绘制Hangman并包含incrementCounter方法:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class HangMan extends JPanel implements ActionListener {

private int xS = 8;
private int yS = 30;
public int counter;
private Color hatColor = Color.BLACK;

private int dx = 0;
private int dy = 0;

public HangMan(int initalX, int initalY) {
    // starting location
    xS = initalX;
    yS = initalY;
    counter = 0;

    Dimension dim = new Dimension(250, 475);
    this.setPreferredSize(dim);
    setFocusable(true);
    requestFocusInWindow();
    this.setVisible(true);

}

public void drawHangMan(Graphics g) {
    g.setColor(Color.BLACK);
    if (counter == 0) {
        repaint();

    }
    if (counter == 1) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim  
        repaint();

    }
    if (counter == 2) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        repaint();

    }
    if (counter == 3) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        drawBody(xS + 25, yS + 80, xS + 25, yS + 175, g);  // body
        repaint();

    }
    if (counter == 4) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        drawBody(xS + 25, yS + 80, xS + 25, yS + 175, g);  // body
        drawLeftLeg(xS - 20, yS + 225, xS + 25, yS + 175, g); //left leg 
        repaint();

    }
    if (counter == 5) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        drawBody(xS + 25, yS + 80, xS + 25, yS + 175, g);  // body
        drawLeftLeg(xS - 20, yS + 225, xS + 25, yS + 175, g); //left leg 
        drawRightLeg(xS + 25, yS + 175, xS + 65, yS + 225, g); //right leg
        repaint();

    }
    if (counter == 6) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        drawBody(xS + 25, yS + 80, xS + 25, yS + 175, g);  // body
        drawLeftLeg(xS - 20, yS + 225, xS + 25, yS + 175, g); //left leg 
        drawRightLeg(xS + 25, yS + 175, xS + 65, yS + 225, g); //right leg
        drawLeftArm(xS - 20, yS + 90, xS + 25, yS + 120, g); //left arm 
        repaint();

    }

    if (counter == 7) {
        drawHat(xS + 15, yS - 1, 20, 25, g);  // hat
        drawBrim(xS + 10, yS + 25, 30, 6, g);  // brim
        drawHead(xS, yS + 30, 50, 50, g); // head
        drawBody(xS + 25, yS + 80, xS + 25, yS + 175, g);  // body
        drawLeftLeg(xS - 20, yS + 225, xS + 25, yS + 175, g); //left leg 
        drawRightLeg(xS + 25, yS + 175, xS + 65, yS + 225, g); //right leg
        drawLeftArm(xS - 20, yS + 90, xS + 25, yS + 120, g); //left arm 
        drawRightArm(xS + 25, yS + 120, xS + 70, yS + 90, g); //right arm  
        repaint();

    }

}

public void drawLeftArm(int a, int b, int x, int y, Graphics g) {
    g.drawLine(a, b, x, y); //left arm
    repaint();

}

public void drawRightArm(int a, int b, int x, int y, Graphics g) {
    g.drawLine(a, b, x, y); //left arm
    repaint();

}

public void drawLeftLeg(int a, int b, int x, int y, Graphics g) {
    g.drawLine(a, b, x, y); //left arm
    repaint();

}

public void drawRightLeg(int a, int b, int x, int y, Graphics g) {
    g.drawLine(a, b, x, y); //left arm
    repaint();

}

public void drawBody(int a, int b, int x, int y, Graphics g) {
    g.drawLine(a, b, x, y); //left arm
    repaint();

}

public void drawHead(int a, int b, int x, int y, Graphics g) {
    g.drawOval(a, b, x, y); //left arm
    repaint();

}

public void drawHat(int a, int b, int x, int y, Graphics g) {
    g.drawRect(a, b, x, y); //left arm
    repaint();

}

public void drawBrim(int a, int b, int x, int y, Graphics g) {
    g.drawRect(a, b, x, y); //left arm
    repaint();

}

public void drawHanger(Graphics g) {
    g.fillRect(xS - 125, yS + 300, 80, 20);
    g.drawLine(xS - 85, yS + 300, xS - 85, yS + 80);
    g.drawLine(xS - 85, yS + 80, xS + 22, yS + 80);

}

public void incrementCounter() {
    counter++;
    System.out.println("counter = " + counter);
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawHangMan(g);
    drawHanger(g);
    repaint();
    g.setFont(new Font("Sherif", Font.BOLD, 40));

}

public void actionPerformed(ActionEvent e) {
    repaint();
}

}

现在这里是我的Button类,它绘制了26个JButtons并包含了每次单击按钮时都会调用update方法的ActionListener

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Button extends JPanel {

JButton[] grid = new JButton[26];

Dashes dash;


public Button(String str, Dashes dash) {

    this.dash = dash;
    final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Dimension dim = new Dimension(360, 360);
    this.setPreferredSize(dim);

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String action = e.getActionCommand();
            System.out.println(action);


            dash.update(action);
            dash.repaint();
            /*
                    add(new JLabel(action + ""));
                    setVisible(true);
             */

        }
    };

    for (int i = 0; i < grid.length; i++) {
        grid[i] = (JButton) this.add(new JButton(alphabet.charAt(i) + ""));
        grid[i].addActionListener(listener);
        this.setVisible(true);
    }

}

}

这是我的Dashes类,其中包含更新方法

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Dashes extends JPanel {

JLabel label;
String word;
String guess = "";
HangMan hang;

public Dashes(String word) {

    this.word = word;
    hang = new HangMan(150, 150);
    for (int i = 0; i < word.length(); i++) {
        guess += " ";
    }
    word = word.toUpperCase();
    Dimension dim = new Dimension(1500, 300);
    this.setPreferredSize(dim);

    this.setVisible(true);

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    drawDashes(g2d);

}

private void drawDashes(Graphics2D g2d) {

    for (int i = 0; i < word.length() * 50; i += 50) {
        g2d.draw(new Line2D.Double(100.0 + i, 200.0, 125.0 + i, 200.0));

        for (int j = 0; j < guess.length(); j++) {
            g2d.drawString(guess.charAt(j) + "", 108 + j * 50, 198);
        }
        // g2d.drawString("m", 108 + i, 198);
    }

}

public void update(String str) {
    for (int i = 0; i < word.length(); i++) {
        if (str.equals(word.charAt(i) + "")) {

            guess = guess.substring(0, i) + str + guess.substring(i + 1);
            hang.incrementCounter();
            hang.repaint();
        }

    }

}

}

我很抱歉这么长时间犯这么小的问题。我想提一下,如果我最初将计数器设置为2,那么它将正确地绘制帽子,边缘和头部。一些背景:我被分配了一个Java图形项目而没有真正被教过图形!

1 个答案:

答案 0 :(得分:0)

1 - 从绘画逻辑中删除repaint()(如评论中所述)
2 - 在Dashes.update()中使用String.equalsIgnoreCase()而不是String.equals()(可能您使用小写字词并与Button类中的大写字符进行比较)。
3 - 在Dashes.update()增量计数器中并仅在您错过时重新绘制HangMan

public void update(String str) {
    boolean found = false;
    for (int i = 0; i < word.length(); i++) {
        if (str.equalsIgnoreCase(word.charAt(i) + "")) {
            guess = guess.substring(0, i) + str + guess.substring(i + 1);
            found = true;
        }

    }
    if(!found) {
        hang.incrementCounter();
        hang.repaint();
    }
}