Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Silly boolean buttons

  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Silly boolean buttons

    Hi there, i am trying to create an user interface.When a button is pressed then a new image paints. I am using booleans to execute the actions. However,
    my bolean thinks its set to true even though its defined to false please help!

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class HelloWorld {
    static boolean slime = false;
    static boolean dracky = false;
    static Clip clip;
    static AudioInputStream audioInputStream;
    final static JButton slimebutton = new JButton();
    final static JButton drackybutton = new JButton();

    public static void main(String[] args) throws IOException {
    ImageIcon slimeIcon = new ImageIcon("C:/slime.png");
    ImageIcon drackyIcon = new ImageIcon("C:/dracky.png");

    JFrame frame = new JFrame("Test");


    frame.setContentPane(new JPanel() {
    BufferedImage image = ImageIO.read(new File// read file
    ("C:/Back.jpg"));
    private boolean layout = true;

    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    {
    if (slime = true) {
    System.out.println("hi");
    /*
    try {
    BufferedImage SlimeInterface = ImageIO.read(new File// read file
    ("C:/healslime.png"));

    g.drawImage(SlimeInterface,0,0,960,580,this);
    repaint();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    */
    }


    if (dracky = true) {
    image = null;
    try {
    BufferedImage OtherInterface = ImageIO.read(new File// read file
    ("C:/Users/Denys/workspace/Denys Game/lib/Dragon quest game/healslime.png"));
    g.drawImage(OtherInterface,0,0,960,580,this);

    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    }

    }



    if (layout = true) {
    slimebutton.setLocation(218, 370);
    slimebutton.setSize(150, 150);
    drackybutton.setSize(219,150);
    drackybutton.setLocation(565, 370);
    ;}
    g.drawImage(image,0,0,960,580,this);
    }

    } }

    );

    frame.add(slimebutton);
    frame.add(drackybutton);
    drackybutton.setBorderPainted(false);
    drackybutton.setIcon(drackyIcon);
    drackybutton.setLayout(null);
    drackybutton.setBackground(Color.white);
    slimebutton.setBorderPainted(false);
    slimebutton.setIcon(slimeIcon);
    slimebutton.setLayout(null);
    slimebutton.setBackground(Color.white);
    frame.setSize(960, 580);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    try
    {
    clip = AudioSystem.getClip();
    audioInputStream = AudioSystem.getAudioInputStream(new File("C:/WarCry.wav").getAbsoluteFile());
    }
    catch(Exception ex) {}
    try {
    clip.open(audioInputStream);
    } catch (LineUnavailableException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    clip.start();

    drackybutton.addActionListener(new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent e)
    { dracky = true;
    //System.out.println("hi");
    }
    }

    );

    slimebutton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    slime = true;
    }

    }

    );

    frame.addWindowListener(new WindowAdapter() {

    // @Override
    public void windowClosing(WindowEvent e) {

    clip.stop();
    System.exit(0);
    }
    });
    }//main

    }// end of Hello world

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Silly boolean buttons

    Please put your code in [code][/code].
    I can see that you have if (slime = true) {.
    You need to add an another '='. (if (slime == true) {)

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

    Default Re: Silly boolean buttons

    Quote Originally Posted by PhQ View Post
    Please put your code in [code][/code].
    I can see that you have if (slime = true) {.
    You need to add an another '='. (if (slime == true) {)
    No, the == true is redundant and ugly, and so you really need to remove the = true part since your code would look cleaner looking like this:

    Java Code:
    if (slime) {
      // ...
    }
    or conversely,
    Java Code:
    if (!slime) {
      // ...
    }
    PhQ and dd2308 like this.

  4. #4
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Silly boolean buttons

    This helps, but the screen does not repaint the new image, i have to close the screen for the screen to repaint, any suggestions?

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

    Default Re: Silly boolean buttons

    Quote Originally Posted by dd2308 View Post
    This helps, but the screen does not repaint the new image, i have to close the screen for the screen to repaint, any suggestions?
    Please again have a look at PHQ's recommendation about editing your original post and placing [code] [/code] tags around your posted code. Help us be better able to read it and understand it.

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

    Default Re: Silly boolean buttons

    Also do *not* read in a file from within the paintComponent or the paint method. Those methods need to be small, minimal, and as fast as absolutely possible. The last thing you want to do is read a file inside of there as it will slow your perceived responsiveness to a crawl.

  7. #7
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Silly boolean buttons

    i don't understand what you mean about the brackets and tags

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

    Default Re: Silly boolean buttons

    Please read the link in my signature below and here on "how to use code tags". Then please edit your original post and place the tag [code] above the block of code that you've posted and the tag [/code] below the block of code. Otherwise your code remains unformatted, and we simply can't read it and understand it.

  9. #9
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Silly boolean buttons

    I understand now what was wrong thank you for the help.

Similar Threads

  1. Replies: 5
    Last Post: 04-11-2012, 09:03 PM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  4. Boolean help
    By DMarsh12 in forum New To Java
    Replies: 8
    Last Post: 04-11-2011, 09:41 PM
  5. Eclipse is making me go silly with it's syntax errors
    By magicmojo in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 01:48 PM

Tags for this Thread

Posting Permissions

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