Results 1 to 4 of 4
  1. #1
    kandt is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default Graphics with JFrame

    I'm making a hangman game for my Java class, and want to do some graphics with JFrame and some .pngs. I get the frame to pop up with the first image and first text, but after that it won't change as the user enters guesses into the program.
    Here is my code (two classes for drawing the frame/graphics, and it's called from a separate class.)

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

    public class DrawHangmanDriver
    {
    DrawHangman hangman = new DrawHangman();
    JFrame frame = new JFrame();

    //this method is called first
    public void startDraw()
    {

    frame.setSize(400, 600);
    frame.setTitle("Hangman");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);

    }

    //this method is called directly after startDraw when the program begins. it is called after each guess, but the frame fails
    //to change what's displayed on it.
    public void draw(int i, String s, String one, String two, String three, String four, String five, String six, String seven, String eight, String nine, String ten, String eleven, String twelve, String thirteen, String fourteen, String fifteen, String sixteen, String seventeen, String eighteen, String nineteen, String twenty, String twentyone, String twentytwo, String twentythree, String twentyfour, String twentyfive, String twentysix)
    {

    hangman.get(i);
    hangman.getS(s);
    hangman.getChars(one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twentyone, twentytwo, twentythree, twentyfour, twentyfive, twentysix);
    frame.add(hangman);
    frame.update(g);

    }




    }



    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;

    public class DrawHangman extends JComponent
    {
    int w;
    String s;
    int l;
    String phrase;
    String oneV = " ";
    String twoV = " ";
    String threeV = " ";
    String fourV = " ";
    String fiveV = " ";
    String sixV = " ";
    String sevenV = " ";
    String eightV = " ";
    String nineV = " ";
    String tenV = " ";
    String elevenV = " ";
    String twelveV = " ";
    String thirteenV = " ";
    String fourteenV = " ";
    String fifteenV = " ";
    String sixteenV = " ";
    String seventeenV = " ";
    String eighteenV = " ";
    String nineteenV = " ";
    String twentyV = " ";
    String twentyoneV = " ";
    String twentytwoV = " ";
    String twentythreeV = " ";
    String twentyfourV = " ";
    String twentyfiveV = " ";
    String twentysixV = " ";


    public void get(int i)
    {
    w = i;
    }

    public void getLoc(int i)
    {
    l = i;
    }

    public void getS(String st)
    {
    s = st;
    }

    //gets guessed characters from draw driver class

    public void getChars(String one, String two, String three, String four, String five, String six, String seven, String eight, String nine, String ten, String eleven, String twelve, String thirteen, String fourteen, String fifteen, String sixteen, String seventeen, String eighteen, String nineteen, String twenty, String twentyone, String twentytwo, String twentythree, String twentyfour, String twentyfive, String twentysix)
    {
    oneV = one;
    twoV = two;
    threeV = three;
    fourV = four;
    fiveV = five;
    sixV = six;
    sevenV = seven;
    eightV = eight;
    nineV = nine;
    tenV = ten;
    elevenV = eleven;
    twelveV = twelve;
    thirteenV = thirteen;
    fourteenV = fourteen;
    fifteenV = fifteen;
    sixteenV = sixteen;
    seventeenV = seventeen;
    eighteenV = eighteen;
    nineteenV = nineteen;
    twentyV = twenty;
    twentyoneV = twentyone;
    twentytwoV = twentytwo;
    twentythreeV = twentythree;
    twentyfourV = twentyfour;
    twentyfiveV = twentyfive;
    twentysixV = twentysix;
    }

    public void paintComponent(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;

    //sets images from .pngs

    BufferedImage zero = null;
    try {
    zero = ImageIO.read(new File("zero.png"));
    } catch (IOException e) {
    }

    BufferedImage one = null;
    try {
    one = ImageIO.read(new File("one.png"));
    } catch (IOException e) {
    }

    BufferedImage two = null;
    try {
    two = ImageIO.read(new File("two.png"));
    } catch (IOException e) {
    }

    BufferedImage three = null;
    try {
    three = ImageIO.read(new File("three.png"));
    } catch (IOException e) {
    }

    BufferedImage four = null;
    try {
    four = ImageIO.read(new File("four.png"));
    } catch (IOException e) {
    }

    BufferedImage five = null;
    try {
    five = ImageIO.read(new File("five.png"));
    } catch (IOException e) {
    }

    BufferedImage six = null;
    try {
    six = ImageIO.read(new File("six.png"));
    } catch (IOException e) {
    }

    BufferedImage seven = null;
    try {
    seven = ImageIO.read(new File("seven.png"));
    } catch (IOException e) {
    }

    g2.drawString("The phrase is: " + s, 10, 430);
    g2.drawString("You've guessed: " + oneV + twoV + threeV + fourV + fiveV + sixV + sevenV + eightV + nineV + tenV + elevenV + twelveV + thirteenV + fourteenV + fifteenV + sixteenV + seventeenV + eighteenV + nineteenV + twentyV + twentyoneV + twentytwoV + twentythreeV + twentyfourV + twentyfiveV + twentysixV, 10, 445);
    g2.drawString("You have " + (7 - w) + " wrong guesses left.", 10, 460);
    g2.drawString("Enter next guess: ", 10, 475);


    if (w == 0)
    {
    g2.drawImage(zero, null, 85, 10);
    }

    if (w == 1)
    {
    g2.drawImage(one, null, 85, 10);
    }

    if (w == 2)
    {
    g2.drawImage(two, null, 85, 10);
    }

    if (w == 3)
    {
    g2.drawImage(three, null, 85, 10);
    }

    if (w == 4)
    {
    g2.drawImage(four, null, 85, 10);
    }

    if (w == 5)
    {
    g2.drawImage(five, null, 85, 10);
    }

    if (w == 6)
    {
    g2.drawImage(six, null, 85, 10);
    }

    if (w == 7)
    {
    g2.drawImage(seven, null, 85, 10);
    }
    this.out();

    }

    //text field, im lost here.

    public String out()
    {
    TextField tf1 = new TextField(20);
    tf1.setSize(20,10);
    Pause.waitForEnter();
    String phraseIn = tf1.getText();

    return phraseIn;

    }

    }



    this is a side question, but how would i put a text box? i've done a good bit of looking and i cant figure it out. as you can see i have some code for one but it does not work.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Text boxes have several events, textbook way to do it in Java is to have an event handler, which does something link getText()...

    Don't code like you have started Pause.waitForEnter() - it will do nothing, you work with the ItemChanged or whatever it is.

    Read on Events in Java.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    An example of a background image changing:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class ChangeImages
    {
      private static final String[] imageNames =
      {
        "zero", "one", "two", "three", "four", "five", "six", "seven"
      };
    
      private static final Dimension IMAGE_PANEL_SIZE = new Dimension(400, 400);
    
      //  **** change this
      private static final String IMAGE_PATH = "fubar/d4i/images/"; //TODO: enter image file path
      //  **** change this
    
      private static final int TIMER_DELAY = 1000;
      private Image image;
      private Image[] images = new Image[imageNames.length];
      private JPanel imagePanel = new JPanel()
      {
        protected void paintComponent(Graphics g)
        {
          super.paintComponent(g);
          myPaint(g);
        }
      };
      
      public ChangeImages()
      {
        imagePanel.setPreferredSize(IMAGE_PANEL_SIZE);
        for (int i = 0; i < imageNames.length; i++)
        {
          try
          {
            images[i] = ImageIO.read(
              new File(IMAGE_PATH + imageNames[i] + ".png"));
          }
          catch (IOException e)
          {
            e.printStackTrace();
          }
        }
        image = images[0];
        new javax.swing.Timer(TIMER_DELAY, new ActionListener()
        {
          int imageIndex = 0;
          public void actionPerformed(ActionEvent arg0)
          {
            imageIndex++;
            imageIndex %= imageNames.length;
            image = images[imageIndex]; // changing the image here
            imagePanel.repaint();  // and requesting a repaint to display it
          }
        }).start();
      }
      
      public JComponent getPanel()
      {
        return imagePanel;
      }
      
      private void myPaint(Graphics g)
      {
        if (image != null)
        {
          g.drawImage(image, 0, 0, 
            imagePanel.getWidth(), imagePanel.getHeight(), imagePanel);
        }
      }
      
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("ChangeImages Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ChangeImages().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
    
      }
    }

  4. #4
    kandt is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Hello from graphics enthusiast
    By George1935 in forum Introductions
    Replies: 0
    Last Post: 10-12-2008, 07:45 PM
  2. SWT Graphics Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:28 PM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •