| | |
Something about Graphics
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 36
Reputation:
Solved Threads: 0
Good day!
hello! lately i've been working on a project of mine and im almost done. but right now i have a problem. in my hangman program i cant seem to make thing that hangs the hangman appear, it will only appera after ive clicked the check button. can anyone help me please? the code is below.
P.S.
I've already tried the:
using the code above the thing (that hangs the man) still wont appear when i execute the program.
any help is greatly appreciated
hello! lately i've been working on a project of mine and im almost done. but right now i have a problem. in my hangman program i cant seem to make thing that hangs the hangman appear, it will only appera after ive clicked the check button. can anyone help me please? the code is below.
P.S.
I've already tried the:
Java Syntax (Toggle Plain Text)
public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.drawLine(130,450,240,450); g.drawLine(185,40,185,450); g.drawLine(110,40,185,40); g.drawLine(110,40,110,100); }
using the code above the thing (that hangs the man) still wont appear when i execute the program.
any help is greatly appreciated
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Hangman extends JFrame implements ActionListener { // DECLARATIONS JLabel inputL, lettersL, wordL; JTextField inputTF, lettersTF, wordTF; JButton checkB, exitB; final String WORD[] = { "SPLINTER" , "MAGICAL" , "FUNDAMENTAL" , "ONYX" , "UNIVERSAL" , "MYSTERIOUS" , "QUAIL" , "DISCOVER" , "UNIQUE" , "OLYMPICS" }; static int index; int chances = 6; char[] blanks,guess; String usedLetters = ""; public Hangman() { JPanel thePanel = new JPanel(); thePanel.setBackground(Color.black); inputL = new JLabel("Enter a letter"); lettersL = new JLabel("Used Letters"); wordL = new JLabel("The Word"); inputTF = new JTextField(); lettersTF = new JTextField(26); wordTF = new JTextField(16); inputTF.setFont(new Font("Arial",Font.BOLD,20)); inputTF.setHorizontalAlignment(0); lettersTF.setFont(new Font("Arial",Font.BOLD,20)); lettersTF.setEditable(false); lettersTF.setHorizontalAlignment(JTextField.CENTER); wordTF.setFont(new Font("Arial",Font.BOLD,20)); wordTF.setEditable(false); wordTF.setHorizontalAlignment(JTextField.CENTER); String text = ""; for(int ctr=0 ; ctr < WORD[index].length() ; ctr++) text = text + "_ "; wordTF.setText(text); blanks = text.toCharArray(); checkB = new JButton("Check"); checkB.addActionListener(this); exitB = new JButton("EXIT"); exitB.addActionListener(this); Container pane = getContentPane(); pane.setLayout(new GridLayout(1,2,10,0)); Container pane1 = new Container(); pane1.setLayout(new GridLayout(8,1,8,8)); pane1.add(wordL); pane1.add(wordTF); pane1.add(lettersL); pane1.add(lettersTF); pane1.add(inputL); pane1.add(inputTF); pane1.add(checkB,BorderLayout.EAST); pane1.add(exitB,BorderLayout.SOUTH); pane.add(thePanel); pane.add(pane1); setResizable(false); setTitle("Hangman BETA VERSION"); setLocation(120,120); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); } public void actionPerformed (ActionEvent e) { Graphics g = getGraphics(); g.setColor(Color.red); g.drawLine(130,450,240,450); g.drawLine(185,40,185,450); g.drawLine(110,40,185,40); g.drawLine(110,40,110,100); if(e.getSource()==exitB) System.exit(0); else if(e.getSource()==checkB) { char letter; String input; Boolean correct = false; input = inputTF.getText(); letter = Character.toUpperCase( input.charAt(0) ); if(input.equals(null)) JOptionPane.showMessageDialog(null,"You Have to Enter Something!","ERROR",JOptionPane.WARNING_MESSAGE); else if( input.length() != 1) JOptionPane.showMessageDialog(null,"ENTER A SINGLE CHARACTER!","ERROR",JOptionPane.WARNING_MESSAGE); else if(Character.isDigit(letter)) JOptionPane.showMessageDialog(null,"ENTER A CHARACTER NOT A NUMBER","ERROR",JOptionPane.WARNING_MESSAGE); else { guess = WORD[index].toCharArray(); for(int ctr =0 ; ctr < WORD[index].length() ; ctr++) { if( letter == guess[ctr] ) { blanks[ctr*2]=guess[ctr]; correct = true; } } if (!correct) { usedLetters = usedLetters + letter + " "; switch (chances) { case 1: g.drawArc(85,150,50,30,0,180); chances--; break; case 2: g.drawLine(90,130,105,130); g.drawLine(115,130,130,130); chances--; break; case 3: g.drawLine(70,230,150,230); chances--; break; case 4: g.drawLine(110,350,70,400); g.drawLine(110,350,150,400); chances--; break; case 5: g.drawLine(110,180,110,350); chances--; break; case 6: g.drawOval(70,100,80,80); chances--; break; } if(chances==0) { JOptionPane.showMessageDialog(null,"GAMEOVER!\n The Word is " + WORD[index],"GAMEOVER!",JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } wordTF.setText(new String(blanks)); } lettersTF.setText(new String(usedLetters)); inputTF.setText(""); } } public static void main (String [] args) { index = (int) ( ( Math.random() ) * 10 ); Hangman theFrame = new Hangman(); } }
your the reason it won't draw is because your doing all of your drawing in your action listener, and it doesn't get called at the start of your program, so nothing gets drawn.
one way i see to solve the problem...
make a new boolean variable, i would call it something like startup and initially set it to true.
then surround everything in your action performed from
or...
for a shorter way put this:
at the bottom of your constructor
one way i see to solve the problem...
make a new boolean variable, i would call it something like startup and initially set it to true.
then surround everything in your action performed from
if(e.getsourc()==exitB) to the end of the action listener with an if(!startup) block. and then in your constructor call checkB.doClick() then set startup to falseor...
for a shorter way put this:
Java Syntax (Toggle Plain Text)
Graphics g = getGraphics(); g.setColor(Color.red); g.drawLine(130,450,240,450); g.drawLine(185,40,185,450); g.drawLine(110,40,185,40); g.drawLine(110,40,110,100);
Last edited by sciwizeh; Jul 31st, 2008 at 11:54 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
that is what i usually do... but i didn't think he wanted to rewrite his whole project to do that. it would require adding and removing much from his current implementation to account for another class.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
He needn't rewrite the whole thing. Just a new panel where the drawing is to occur and use that graphic context to drawn on instead of the JFrame context.
By the way, be sure to dispose of a graphics context obtained with getGraphics() by calling dispose() on the reference when you are finished with it.
By the way, be sure to dispose of a graphics context obtained with getGraphics() by calling dispose() on the reference when you are finished with it.
ok, sure i forgot about that... i usually extend JPanel so i can give it a functionality which would require rewriting his code. can't be right always oh well
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
Yes, extending a component and overriding paintComponent() is the usual and preferable way to do it and this code would certainly benefit from that (drag another window over the frame and see what happens to the hangman graphic), but at a minimum the existing code could draw to a panel (or label) reference with few changes.
Given the above discussion on using a panel to draw on and the fact that overriding paintComponent() really is the better way to go, I'd recommend the following, which is only a minor re-arrangement of your existing code (see comments on changes) (I did not correct or alter anything else in the code - just the graphics)
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Hangman extends JFrame implements ActionListener { // DECLARATIONS JLabel inputL, lettersL, wordL; JTextField inputTF, lettersTF, wordTF; JButton checkB, exitB; final String WORD[] = {"SPLINTER", "MAGICAL", "FUNDAMENTAL", "ONYX", "UNIVERSAL", "MYSTERIOUS", "QUAIL", "DISCOVER", "UNIQUE", "OLYMPICS"}; static int index; int chances = 6; char[] blanks, guess; String usedLetters = ""; // New reference to your hangman panel JPanel graphicPanel; public Hangman() { // override paintComponent to call the drawHangman() method graphicPanel = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); drawHangman(g); } }; graphicPanel.setBackground(Color.black); inputL = new JLabel("Enter a letter"); lettersL = new JLabel("Used Letters"); wordL = new JLabel("The Word"); inputTF = new JTextField(); lettersTF = new JTextField(26); wordTF = new JTextField(16); inputTF.setFont(new Font("Arial", Font.BOLD, 20)); inputTF.setHorizontalAlignment(0); lettersTF.setFont(new Font("Arial", Font.BOLD, 20)); lettersTF.setEditable(false); lettersTF.setHorizontalAlignment(JTextField.CENTER); wordTF.setFont(new Font("Arial", Font.BOLD, 20)); wordTF.setEditable(false); wordTF.setHorizontalAlignment(JTextField.CENTER); String text = ""; for(int ctr = 0; ctr<WORD[index].length(); ctr++) { text = text+"_ "; } wordTF.setText(text); blanks = text.toCharArray(); checkB = new JButton("Check"); checkB.addActionListener(this); exitB = new JButton("EXIT"); exitB.addActionListener(this); Container pane = getContentPane(); pane.setLayout(new GridLayout(1, 2, 10, 0)); Container pane1 = new Container(); pane1.setLayout(new GridLayout(8, 1, 8, 8)); pane1.add(wordL); pane1.add(wordTF); pane1.add(lettersL); pane1.add(lettersTF); pane1.add(inputL); pane1.add(inputTF); pane1.add(checkB, BorderLayout.EAST); pane1.add(exitB, BorderLayout.SOUTH); pane.add(graphicPanel); pane.add(pane1); setResizable(false); setTitle("Hangman BETA VERSION"); setLocation(120, 120); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500, 500); } /** draws the hangman graphic */ private void drawHangman(Graphics g) { // the gallows g.setColor(Color.red); g.drawLine(130, 450, 240, 450); g.drawLine(185, 40, 185, 450); g.drawLine(110, 40, 185, 40); g.drawLine(110, 40, 110, 100); // intentional switch fall-through to // draw the man as chances decrease switch(chances) { case 0: g.drawArc(85, 150, 50, 30, 0, 180); case 1: g.drawLine(90, 130, 105, 130); g.drawLine(115, 130, 130, 130); case 2: g.drawLine(70, 230, 150, 230); case 3: g.drawLine(110, 350, 70, 400); g.drawLine(110, 350, 150, 400); case 4: g.drawLine(110, 180, 110, 350); case 5: g.drawOval(70, 100, 80, 80); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==exitB) { System.exit(0); } else if(e.getSource()==checkB) { char letter; String input; Boolean correct = false; input = inputTF.getText(); letter = Character.toUpperCase(input.charAt(0)); if(input.equals(null)) { JOptionPane.showMessageDialog(null, "You Have to Enter Something!", "ERROR", JOptionPane.WARNING_MESSAGE); } else if(input.length()!=1) { JOptionPane.showMessageDialog(null, "ENTER A SINGLE CHARACTER!", "ERROR", JOptionPane.WARNING_MESSAGE); } else if(Character.isDigit(letter)) { JOptionPane.showMessageDialog(null, "ENTER A CHARACTER NOT A NUMBER", "ERROR", JOptionPane.WARNING_MESSAGE); } else { guess = WORD[index].toCharArray(); for(int ctr = 0; ctr<WORD[index].length(); ctr++) { if(letter==guess[ctr]) { blanks[ctr*2] = guess[ctr]; correct = true; } } if(!correct) { usedLetters = usedLetters+letter+" "; // update chances and repaint the panel chances--; graphicPanel.repaint(); if(chances==0) { JOptionPane.showMessageDialog(null, "GAMEOVER!\n The Word is "+WORD[index], "GAMEOVER!", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } wordTF.setText(new String(blanks)); } lettersTF.setText(new String(usedLetters)); inputTF.setText(""); } } public static void main(String[] args) { index = (int)((Math.random())*10); Hangman theFrame = new Hangman(); } }
•
•
•
•
Java Syntax (Toggle Plain Text)
graphicPanel = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); drawHangman(g); } };
Last edited by sciwizeh; Aug 1st, 2008 at 7:43 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
![]() |
Similar Threads
- getting graphics into c++ (C++)
- Graphics cards question? (Monitors, Displays and Video Cards)
- Graphics T and L support (Monitors, Displays and Video Cards)
- XP and Graphics card problems (Windows NT / 2000 / XP)
- graphics card (Monitors, Displays and Video Cards)
- recommendations 4 a 3D graphics prog? (Graphics and Multimedia)
Other Threads in the Java Forum
- Previous Thread: EXCEL CSV FILES Handling
- Next Thread: problem with rmi
Views: 1138 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






