The Artima Developer Community
Sponsored Link

Java Answers Forum
NEWBIE ALERT: Problem with displaying shape on my GUI

1 reply on 1 page. Most recent reply: Nov 11, 2002 4:43 PM by Mike Yuen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Mike Yuen

Posts: 3
Nickname: kaboom
Registered: Nov, 2002

NEWBIE ALERT: Problem with displaying shape on my GUI Posted: Nov 10, 2002 11:43 PM
Reply to this message Reply
Advertisement
I'm just going through a small book on Java programming and the goal is to press a button to show a shape (ie: circle) and then to press a button to hide that same shape.

My idea was to simply add it in but change the visibility to true and false. Now, I get it to show perfectly but I can't get it to hide (ie: setVisiblity to false) for some reason. The problem lies in the actionPerformed method of the CommandMain class. I'm at a loss as to why I can't simply change the visibility of the object to true and false.

I've include the relevant code below
Thanks for all your help in advance.


================= COMMANDMAIN.JAVA =================
public class CommandMain extends JFrame implements ActionListener
{ JButton circleButton = new JButton("Make Circle");
JButton squareButton = new JButton("Make Square");
JButton undoButton = new JButton("Undo Last");
JButton exitButton = new JButton("Exit");
private static Vector objectList = new Vector(); // Stores objects in order
JPanel container = new JPanel();
Button newCircle; // Used to add a Circle object to the vector
Button newSquare;
Button newShape;
Button objectToHide;

// CONSTRUCTOR
// PURPOSE: Used to set up all the buttons on our Panel
public CommandMain()
{ setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add interactive ability to buttons
circleButton.addActionListener(this);
squareButton.addActionListener(this);
undoButton.addActionListener(this);
exitButton.addActionListener(this);

// Add them so we can see them on the panel
this.container.add(circleButton);
this.container.add(squareButton);
this.container.add(undoButton);
this.container.add(exitButton);
setContentPane(container);
setVisible(true);
}

// PURPOSE: Used to create an instance of itself so we can see the frame
public static void main(String args[]) throws IOException
{ CommandMain window = new CommandMain(); }

// PURPOSE: Used to perform actions based on the button picked by the user
public void actionPerformed(ActionEvent picked)
{ Object choice = picked.getSource();
if(choice == circleButton) // CIRCLE WAS SELECTED
{ newShape = new Button("Circle");
Button newCircle = newShape.getShape();
this.container.add(newCircle); // Add it to my container
newShape.execute();
objectList.add(newShape); // Add the object to my Vector
setContentPane(container);
repaint(); // Redraw the GUI because it changed
}
else if(choice == undoButton) // UNDO WAS SELECTED
{ if(objectList.size() > 0) // Check to ensure we don't overstep the boundaries
{ System.out.println("UNDO Pressed");
objectToHide = (Button)objectList.elementAt(objectList.size()-1);
objectToHide.hideShape(); // <-- PROBLEM: NOT HIDING MY SHAPE

objectList.removeElementAt(objectList.size()-1);
setContentPane(container);
repaint();
} // End if(objectList.size() > 0)
} // End undoButton
else if(choice == exitButton) // EXIT WAS SELECTED
{ System.out.println("Goodbye: Application exiting");
dispose();
System.exit(0);
}
else // No idea what button was selected
{ System.out.println("ERROR: Invalid option"); }
}
}

========================== BUTTON.JAVA ==================
import java.awt.*;
import javax.swing.*;

public class Button extends JPanel
{ String objectShape;

Button()
{ }

Button(String shape)
{ this.objectShape = shape; }

public void execute()
{ setVisible(true); }

public void hideShape()
{ System.out.println("hideShape called");
super.setVisible(false);
}

public Button getShape()
{ if(this.objectShape.equals("Circle"))
{ System.out.println("Making a circle");
Circle newCircle = new Circle();
return newCircle;
}

else if(this.objectShape.equals("Square"))
{ System.out.println("Making a square");
Square newSquare = new Square();
return newSquare;
}
return null;
}
}


Mike Yuen

Posts: 3
Nickname: kaboom
Registered: Nov, 2002

Re: NEWBIE ALERT: Problem with displaying shape on my GUI Posted: Nov 11, 2002 4:43 PM
Reply to this message Reply
Ok, here's what i've tried.

I've tried:
1. checked isVisible() to see if my object should be on the GUI. Sure enough, it says it should be invisible, so I thought, ok. Just repaint the Frame.

2. Well, to repaint the frame AND the object objectToHide, I tried:
- repaint()
- validate()
- show()
- revalidate()

All of them did nothing. Can someone please help me out?

Flat View: This topic has 1 reply on 1 page
Topic: Java Virtual Machine Previous Topic   Next Topic Topic: searching a character from a string

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use