Results 1 to 9 of 9
Thread: Silly boolean buttons
- 09-27-2012, 02:02 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
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
- 09-27-2012, 02:18 AM #2
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) {)
-
Re: Silly boolean buttons
- 09-27-2012, 03:52 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
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?
-
Re: Silly boolean buttons
-
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.
- 09-27-2012, 04:27 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
Re: Silly boolean buttons
i don't understand what you mean about the brackets and tags
-
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.
- 10-05-2012, 01:50 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
this is silly basic question in java . please help me make me clear
By coolforyou in forum New To JavaReplies: 5Last Post: 04-11-2012, 10:03 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 01:01 AM -
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 10:04 AM -
Boolean help
By DMarsh12 in forum New To JavaReplies: 8Last Post: 04-11-2011, 10:41 PM -
Eclipse is making me go silly with it's syntax errors
By magicmojo in forum New To JavaReplies: 5Last Post: 02-22-2011, 02:48 PM
Bookmarks