2010-12-29 4 views
0

안녕하세요, 저는 연말 연시에 자바에서 행맨 게임을 해왔습니다. 임 거의 다되었지만 창을 다시 그리거나 업데이트하는 데 몇 가지 문제가 있습니다.자바 교수형 집행자 게임

인생을 잃어 버렸을 때 패널을 제거한 다음 다른 패널을 추가하려고하지만 어떤 이유로 왼쪽 또는 가끔씩 오른쪽으로 이동하여 일부 라인이 사라지는 것을 의미합니다. 다시 그려진다.

편집 : 아직 문제가 있습니다. 누군가 다른 의견이없는 한 지금은 그냥 떠날 것입니다.

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

/** class to display a JFrame object using border layout */ 

public class HangmanJFrame extends JFrame implements ActionListener { 

private JPanel centerPanel; 
private JPanel southPanel; 
private JTextField textField; 
private LinePanel line; 
private String [] wordList = {"computer","java","activity","alaska","appearance","article", 
    "automobile","basket","birthday","canada","central","character","chicken","chosen", 
    "cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact", 
    "establishment","fifteen","football","foreign","frequently","frighten","function","gradually", 
    "hurried","identity","importance","impossible","invented","italian","journey","lincoln", 
    "london","massage","minerals","outer","paint","particles","personal","physical","progress", 
    "quarter","recognise","replace","rhythm","situation","slightly","steady","stepped", 
    "strike","successful","sudden","terrible","traffic","unusual","volume","yesterday" }; 

public ArrayList<String> usedLetters = new ArrayList(); // list of used letter by user 
public ArrayList<String> correctLetters = new ArrayList(); 
public String userInput = ""; 

private int numLives = 6; // number of lives 
public String theWord; // the wrong which is chosen 
JButton exitButton; 
JButton playAgain; 

// no-argument constructor 
public HangmanJFrame() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    theWord = pickWord(); 
    correctLetters = new ArrayList(theWord.length()); 

    setSize(600,500); 
    setLocation(100, 100); 
    setTitle("Hangman Game"); 
    setLayout(new BorderLayout()); 

    centerPanel = new JPanel(); 
    line = new LinePanel(15,theWord,usedLetters); 
    centerPanel.setSize(500,500); 
    centerPanel.setBackground(Color.WHITE); 
    centerPanel.add(line); 
    add(centerPanel, BorderLayout.CENTER); 

    textField = new JTextField(20); 
    textField.addActionListener(this); 

    playAgain = new JButton("Play Again"); 
    exitButton = new JButton("Exit"); 
    exitButton.addActionListener(this); 
    playAgain.addActionListener(this); 

    southPanel = new JPanel(); 
    southPanel.setBackground(Color.BLACK); 
    southPanel.setLayout(new GridLayout(0,3)); 
    southPanel.add(playAgain); 
    southPanel.add(textField); 
    southPanel.add(exitButton); 
    add(southPanel, BorderLayout.SOUTH); 

} 

// Picks a word, latter it will be picked randomly. 
private String pickWord(){ 
    return wordList[0]; 
} 

// This method check wither the input is valid 
// i.e. its in the alphabet. 
private boolean checkInput(String s){ 
    String [] alphabet = {"a","b","c","d","e","f", 
    "g","h","i","j","k","l","m","n","o","p", 
    "q","r","s","t","u","v","w","x","y","z"}; 

    for (int i = 0; i < alphabet.length; i++){ 
    if (s.equals(alphabet[i]) && s.length() <= 1){ 
    return true; 
    } 
    } 
    return false; 
} 

/** 
    * 
    */ 
public void Play(){ 


    if (numLives > 0){ 

    if (userInput.length() == 1 && usedLetters.contains(userInput) == false && 
    checkInput(userInput) == true){ 
    usedLetters.add(userInput); 

    if (theWord.contains(userInput) == true){ 
    correctLetters.add(userInput); 
    centerPanel.removeAll(); 

    line = new LinePanel(20,theWord,correctLetters); 
    centerPanel.add(line); 
    centerPanel.revalidate(); 
    } 

    else{ 
    numLives = numLives - 1; 

    centerPanel.removeAll(); 
    line = new LinePanel(numLives,theWord,correctLetters); 

    centerPanel.add(line); 
    centerPanel.revalidate(); 
    } 
    } 

    else if (userInput.length() > 1) 
    System.out.println("Enter a valid letter"); 

    else if (userInput.length() == 1 && checkInput(userInput) == true && theWord.contains(userInput)){ 
    correctLetters.add(userInput); 
    } 

    centerPanel.removeAll(); 

    line = new LinePanel(20,theWord,usedLetters); 
    centerPanel.add(line); 
    centerPanel.revalidate(); 
    } 
} 
// return true if the word and the correctly used letters list match 
public boolean checkWord(String s, ArrayList<String> t){ 
    String temp = ""; 

    for (int i = 0; i < s.length(); i++){ 
    if (t.contains(s.substring(i, i+1)) == true){ 
    temp += s.substring(i, i+1); 
    } 
    } 

    if (s.equals(temp)){ 
    return true; 
    } 

    return false; 
} 

public void actionPerformed(ActionEvent evt) { 
    String temp = textField.getText(); 
    if (temp.length() == 1){ 
    userInput = temp; 
    } 
    textField.selectAll(); 
    if (checkWord(theWord, correctLetters) != true){ 
    Play(); 
    } 

    if (evt.getSource() == exitButton){ 
    System.exit(0); 
    } 

// if (evt.getSource() == playAgain){ 
// removeAll(); 
// repaint(); 
// 
// } 

} 


} 

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 
import java.util.ArrayList; 

import javax.swing.JPanel; 

public class LinePanel extends JPanel { 

int x = 5; 
String theWord = ""; 
ArrayList<String> letterList; 

public LinePanel(int num, String t, ArrayList<String> s) { 
    super(); // 300,350 
    setPreferredSize(new Dimension(400,400)); 
    setBackground(Color.RED); 
    this.x = num; 
    this.theWord = t; 
    letterList = cloneList(s); 
} 

private ArrayList<String> cloneList(ArrayList<String> aList) { 
    ArrayList<String> clonedList = new ArrayList<String>(aList.size()); 
    for (String letter : aList) 
    clonedList.add(letter); 
    return clonedList; 
} 



public int getX() { 
    return x; 
} 

public void setX(int x) { 
    this.x = x; 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    if (x == 15 || x != 15){ 
    Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base 
    Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line 
    Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line 
    Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man 
    g2.setStroke(new BasicStroke(5.0f)); // Line thickness 
    g2.setColor(Color.BLACK); // Line colour 

    //draw shape of line 
    g2.draw(line); 
    g2.draw(line2); 
    g2.draw(line3); 
    g2.draw(line4); 

    int x1 = 0; int y = 320; 
    for (int i = 0; i < theWord.length();i++){ 
    g2.drawLine(x1, y, x1 + 20, y); 
    x1 += 50; 
    } 
    } 

    // head 
    if (x == 5 || x < 5){ 
    System.out.println("uheuheuha"); 
    g2.setStroke(new BasicStroke(5.0f)); 
    g2.drawOval(135, 85, 35, 35); 
    } 

    // body 
    if (x == 4 || x < 4){ 
    g2.drawLine(150, 120, 150, 190); 
    } 

    // left arm 
    if (x == 3 || x < 3){ 
    g2.drawLine(150, 140, 125, 155); 
    } 

    // right arm 
    if (x == 2 || x < 2){ 
    g2.drawLine(150, 140, 175, 155); 
    } 

    // left leg and foot 
    if (x == 1 || x < 1){ 
    g2.drawLine(150, 190, 125, 200); // leg 
    g2.drawLine(125, 200, 120, 190); // foot 
    } 

    // right leg and foot 
    if (x == 0){ 
    g2.drawLine(150, 190, 175, 200); // leg 
    g2.drawLine(175, 200, 180, 190); // foot 
    } 

    // Show whole word on screen 
    if (x == 20){ 

    int x1 = 3; int y = 317; 

    String temp = ""; 

    for (int i = 0; i < theWord.length(); i++){ 
    if (letterList.contains(theWord.substring(i, i+1)) == true){ 
    temp += theWord.substring(i, i+1); 
    } 
    else{ 
    temp += " "; 
    } 
    } 

    for (int i = 0; i < temp.length() ;i++){ 
    g2.setColor(Color.BLACK); 
    Font font = new Font("Arial", Font.PLAIN, 25); 
    g2.setFont(font); 
    g2.drawString(temp.substring(i, i+1), x1, y); 
    x1 += 50; 
    } 


    x1 = 3; y = 370; 
    for (int i = 0; i < letterList.size() ;i++){ 

    if (theWord.contains(letterList.get(i)) == false){ 
    g2.drawString(letterList.get(i), x1, y); 
    x1 += 50; 
    } 

    } 


    } 
} 
} 

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 
import java.util.ArrayList; 

import javax.swing.JPanel; 

public class LinePanel extends JPanel { 

int x = 5; 
String theWord = ""; 
ArrayList<String> letterList; 

public LinePanel(int num, String t, ArrayList<String> s) { 
    super(); // 300,350 
    setPreferredSize(new Dimension(400,400)); 
    setBackground(Color.RED); 
    this.x = num; 
    this.theWord = t; 
    letterList = cloneList(s); 
} 

private ArrayList<String> cloneList(ArrayList<String> aList) { 
    ArrayList<String> clonedList = new ArrayList<String>(aList.size()); 
    for (String letter : aList) 
    clonedList.add(letter); 
    return clonedList; 
} 



public int getX() { 
    return x; 
} 

public void setX(int x) { 
    this.x = x; 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    if (x == 15 || x != 15){ 
    Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base 
    Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line 
    Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line 
    Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man 
    g2.setStroke(new BasicStroke(5.0f)); // Line thickness 
    g2.setColor(Color.BLACK); // Line colour 

    //draw shape of line 
    g2.draw(line); 
    g2.draw(line2); 
    g2.draw(line3); 
    g2.draw(line4); 

    int x1 = 0; int y = 320; 
    for (int i = 0; i < theWord.length();i++){ 
    g2.drawLine(x1, y, x1 + 20, y); 
    x1 += 50; 
    } 
    } 

    // head 
    if (x == 5 || x < 5){ 
    g2.setStroke(new BasicStroke(5.0f)); 
    g2.drawOval(135, 85, 35, 35); 
    } 

    // body 
    if (x == 4 || x < 4){ 
    g2.drawLine(150, 120, 150, 190); 
    } 

    // left arm 
    if (x == 3 || x < 3){ 
    g2.drawLine(150, 140, 125, 155); 
    } 

    // right arm 
    if (x == 2 || x < 2){ 
    g2.drawLine(150, 140, 175, 155); 
    } 

    // left leg and foot 
    if (x == 1 || x < 1){ 
    g2.drawLine(150, 190, 125, 200); // leg 
    g2.drawLine(125, 200, 120, 190); // foot 
    } 

    // right leg and foot 
    if (x == 0){ 
    g2.drawLine(150, 190, 175, 200); // leg 
    g2.drawLine(175, 200, 180, 190); // foot 
    } 

    // Show whole word on screen 
    if (x == 20){ 

    int x1 = 3; int y = 317; 

    String temp = ""; 

    for (int i = 0; i < theWord.length(); i++){ 
    if (letterList.contains(theWord.substring(i, i+1)) == true){ 
    temp += theWord.substring(i, i+1); 
    } 
    else{ 
    temp += " "; 
    } 
    } 

    for (int i = 0; i < temp.length() ;i++){ 
    g2.setColor(Color.BLACK); 
    Font font = new Font("Arial", Font.PLAIN, 25); 
    g2.setFont(font); 
    g2.drawString(temp.substring(i, i+1), x1, y); 
    x1 += 50; 
    } 


    x1 = 3; y = 370; 
    for (int i = 0; i < letterList.size() ;i++){ 

    if (theWord.contains(letterList.get(i)) == false){ 
    g2.drawString(letterList.get(i), x1, y); 
    x1 += 50; 
    } 

    } 


    } 
} 
} 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 


public class PlayHangman { 

/** 
    * @param args 
    */ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    HangmanJFrame window = new HangmanJFrame(); 
    window.setVisible(true); 

} 
} 
+2

좋은 오래된 숙제 냄새가 났습니까? 다행스럽게도, user557240은이 코드의 relevnt 부분만을 보내주었습니다. "이 코드를 읽으면 정말로 고려해야합니다. http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing- the-perfect-question.aspx " – Riduidel

+1

@Riduidel - 귀하의 링크가"액세스가 거부되었습니다 "라고 말합니다. – Rogach

+1

@ hvgotcodes 나는 당신의 실패로 가득 찬 것 같아요. 내 코드를 제대로 포맷 할 기회조차주지 못했다. – user557240

답변

3

패널을 제거하고 추가하지 마십시오. 삶을 저장하는 변수를 변경하고 페인트를 다시하십시오.

+0

제안 해 주셔서 감사합니다.하지만 이렇게하면 새 패널이 바로 이전 패널 바로 아래에 추가됩니다. – user557240

+1

그렇다면 여전히 패널을 추가하고 있다는 의미입니다. – jzd

+0

안녕하세요. 작동하지 않습니다. 이것은 내가 시도한 것입니다. { \t \t \t \t \t numLives = numLives - 1; \t \t \t \t \t line = new LinePanel (numLives, theWord, correctLetters); \t \t \t \t \t line.repaint(); \t \t \t \t \t centerPanel.revalidate(); \t \t \t \t \t repaint(); \t \t \t \t – user557240

0

난 정말 당신의 코드를 테스트 할 수 있지만 나는이 개 모호한 아이디어가 : 스윙 및/또는 AWT 구성 요소의 업데이트가 항상 올바른 실행되어야한다는 것을,

  1. 염두에 두어야을 실. invokeLater와 함께 몇 가지 검사를 고려

    SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           // do your display modifications here 
          } 
    }); 
    
  2. 당신은 당신의 패널 invalidate/validate를 사용하려고 수있는, 즉 뭔가가 변경된 것을, 자바 실현하는 데 도움이 될 수 있습니다.

나는 이것이 당신의 문제를 해결하는 데 도움이되기를 바랍니다. 행운을 빈다.