Log in

View Full Version : Java assignment dealing with events


Jess
July 7th, 2013, 09:43 PM
Trying to code up something that uses events...I have it written up, and there are no errors, but it's not working as it should.

/* Jessica Chen
* Sunday 7/7/13
* Java 1.7.0_21 and NetBeans 7.3
* This program displays a glass of milk
*/

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


public class Milk extends JComponent implements ActionListener {
JFrame frame = new JFrame("Glass of Milk");
Container content = frame.getContentPane();
JPanel panel = new JPanel();
JButton drinkAll = new JButton("Drink");
boolean drink = true;

public static void main(String[] args) {
Milk drawing = new Milk();
}

public void paint(Graphics g){
g.drawLine(90, 100, 90, 130);
g.drawLine(115, 100, 115, 130);
g.drawLine(90, 130, 115, 130);
if (drink = false){
g.setColor(Color.WHITE);
g.fillRect(91, 105, 24, 25);
} else {
g.fillRect(0, 0, 0, 0);
}
}

public Milk(){
content.setLayout(new BorderLayout());
content.setBackground(Color.lightGray);
content.add(this, BorderLayout.CENTER);

panel.add(drinkAll);
content.add(panel, BorderLayout.SOUTH);

frame.setSize(200, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

drinkAll.addActionListener(this);

}

public void actionPerformed(ActionEvent e){
content.repaint();

String cmd = e.getActionCommand();

if (cmd.equals("Drink")) {
drink = true;
} else {
drink = false;
}
}
}



What I have drawn is a glass of milk. When I click the button "Drink", the milk (white rectangle) disappears. Something in my code is not making it disappear when I click it. I know I have something wrong, or am missing something, but I don't know what. Someone help please?



UPDATE: This is due TOMORROW before midnight, so please get in your replies before that time.

TheMatrix
July 8th, 2013, 03:36 AM
Where are you assigning actionPerformed to handle the action? In most libraries, you have to specify which function you'd like to use. I don't know how to do that with Java's AWT, for that you'd have to consult the documentation. They do teach you how to use it in class, right? I know our compsci club at school doesn't.

Also, do you know how to use a debugger? It is the most essential tool a programmer can have. I know NetBeans and Eclipse have one built in. If your class does not go over at least the basics of the debugger, then it is not a class you should take seriously.
Think of it with this analogy:
Give a man a fish/answer to a programming issue and he will eat for a day/come back in a few minutes with a new problem. Teach a man to fish/debug, and he will be fed for a lifetime/able to solve problems on his own.

NB: I also don't get what I assume you are describing -- there is no white rectangle at all when I try your code. I only get 3 lines drawn and a button at the bottom.

Jess
July 8th, 2013, 09:27 AM
It's an online class and no, never used a debugger.

There's no white rectangle because for some reason it won't appear. I have the code for it...though if I changed the if (drink = false){ to true not false the white rectangle appears. but I want it to disappear when the button is clicked.

EDIT: Used a double equal sign instead, after "drink", did nothing

EDIT #2: Figured it out

/* Jessica Chen
* Sunday 7/7/13
* Java 1.7.0_21 and NetBeans 7.3
* This program displays a glass of milk
*/

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


public class Milk extends JComponent implements ActionListener {
JFrame frame = new JFrame("Glass of Milk");
Container content = frame.getContentPane();
JPanel panel = new JPanel();
JButton drinkAll = new JButton("Drink");
boolean drink = true;

public static void main(String[] args) {
Milk drawing = new Milk();
}

public void paint(Graphics g){
//the glass
g.drawLine(90, 100, 90, 130);
g.drawLine(115, 100, 115, 130);
g.drawLine(90, 130, 115, 130);
if (drink == true){
g.setColor(Color.WHITE);
g.fillRect(91, 105, 24, 25);
} else {
g.setColor(Color.LIGHT_GRAY);
g.fillRect(91, 105, 24, 25);
}
}

public Milk(){
content.setLayout(new BorderLayout());
content.setBackground(Color.lightGray);
content.add(this, BorderLayout.CENTER);

panel.add(drinkAll);
content.add(panel, BorderLayout.SOUTH);

frame.setSize(200, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

drinkAll.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

String cmd = e.getActionCommand();

if (cmd.equals("Drink")) {
drink = false;
} else {
drink = true;
}

content.repaint();
}
}