This is a pizza building program. My drawPanel (extends JPanel) that\'s responsi
ID: 3635708 • Letter: T
Question
This is a pizza building program. My drawPanel (extends JPanel) that's responsible for drawing the image of the pizzas isn't showing up. I can't figure out why. In the set up method (see below) I add the pizzaPanel (object in question) to my gui, but it's not showing up. I set the background color to green just so I'd know whether or not it was actually there and the problem wasn't actually with the drawing of the images. There are a multitude of classes associated with this so you won't be able to test it yourself but if you could look this code over and see where I might potentially be going wrong I'd appreciate it.import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.dnd.*;
// Note* CenterFrame is a class that extends JFrame
public class PizzaGUI extends CenterFrame implements ActionListener, Drawable
{
public static void main (String[] args)
{
PizzaGUI pg = new PizzaGUI(800, 425);
}
private Image image;
private JPanel controls;
private DrawPanel pizzaPanel;
private JRadioButton small;
private JRadioButton medium;
private JRadioButton large;
private JRadioButton thin;
private JRadioButton handtossed;
private JRadioButton deepdish;
private JLabel pizzaCrust;
private JLabel pizzaSize;
private JLabel pizzaToppings;
private JButton btn;
private JCheckBox mushrooms;
private JCheckBox pepperoni;
private JCheckBox peppers;
private JCheckBox onions;
private JCheckBox sausage;
private ToppingList lstToppings;
private DecoratedPizza pizza;
public PizzaGUI(int width, int height)
{
super (width, height, "Pizza Time!");
pizzaPanel = new DrawPanel();
pizzaPanel.setBackground(Color.green);
setResizable(false);
setUp(width, height);
setVisible(true);
}
private void setUp(int width, int height)
{
setBackground(Color.white);
controls = new JPanel();
controls.setBackground(Color.white);
setLayout(new GridLayout(1,2)); //changed from 1,2
add(controls);
add(pizzaPanel);
small = new JRadioButton("S", true);
small.setBackground(Color.white);
medium = new JRadioButton("M");
medium.setBackground(Color.white);
large = new JRadioButton("L");
large.setBackground(Color.white);
ButtonGroup size = new ButtonGroup();
size.add(small);
size.add(medium);
size.add(large);
thin = new JRadioButton("Thin", true);
thin.setBackground(Color.white);
handtossed = new JRadioButton("Hand-Tossed");
handtossed.setBackground(Color.white);
deepdish = new JRadioButton("Deep Dish");
deepdish.setBackground(Color.white);
ButtonGroup crust = new ButtonGroup();
crust.add(thin);
crust.add(handtossed);
crust.add(deepdish);
btn = new JButton("Order!");
btn.setBackground(Color.white);
pizzaSize = new JLabel("Select Pizza Size:");
pizzaCrust = new JLabel("Select Pizza Crust:");
pizzaToppings = new JLabel("Select Pizza Toppings:");
mushrooms = new JCheckBox("Mushrooms");
mushrooms.setBackground(Color.white);
pepperoni = new JCheckBox("Pepperoni");
pepperoni.setBackground(Color.white);
sausage = new JCheckBox("Sausage");
sausage.setBackground(Color.white);
JCheckBox("Onions");
onions.setBackground(Color.white);
peppers = new JCheckBox("Green Peppers");
peppers.setBackground(Color.white);
EasyGridBag bag = new EasyGridBag(6, 3, controls);
controls.setLayout(bag);
bag.fillCell(1, 1, pizzaSize);
bag.fillCell(2,1,GridBagConstraints.WEST,small);
bag.fillCell(3,1,GridBagConstraints.WEST,medium);
bag.fillCell(4,1,GridBagConstraints.WEST,large);
bag.fillCell(1,1,GridBagConstraints.WEST,pizzaSize);
bag.fillCell(1,2,GridBagConstraints.WEST,pizzaCrust);
bag.fillCell(2,2,GridBagConstraints.WEST,thin);
bag.fillCell(3,2,GridBagConstraints.WEST,handtossed);
bag.fillCell(4,2,GridBagConstraints.WEST,deepdish);
bag.fillCell(1,3,GridBagConstraints.WEST,pizzaToppings);
//this next code will be commented out and replaced with a JList, lstToppings
/* bag.fillCell(2,3,GridBagConstraints.WEST,pepperoni);
bag.fillCell(3,3,GridBagConstraints.WEST,onions);
bag.fillCell(4,3,GridBagConstraints.WEST,peppers);
bag.fillCell(5,3,GridBagConstraints.WEST,sausage);
bag.fillCell(6,3,GridBagConstraints.WEST,mushrooms);
*/
lstToppings = new ToppingList();
bag.fillCell(2, 3, 5, 1, GridBagConstraints.BOTH, lstToppings);
bag.fillCell(6, 2, btn);
btn.addActionListener(this); //a JFrame can listen for events on itself
btn.setActionCommand("Order");
//
//add some drag and drop setup statements here
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(lstToppings, DnDConstants.ACTION_COPY, lstToppings);
DropTarget droptarget = new DropTarget(pizzaPanel, lstToppings);
pizza = buildPizza(); //build and display the default pizza
}
//
//complete this build pizza method
public DecoratedPizza buildPizza()
{
//call methods to build the pizza
Pizza pizza = new Pizza();
setCrust((Pizza)pizza);
setSize((Pizza)pizza);
DecoratedPizza pizzaWithToppings = setToppings(pizza);
pizzaPanel.setDrawable((Drawable)pizzaWithToppings);
repaint(); //paints the DrawPanel, which calls draw here in the GUI, which draws the newly configured pizza
return pizzaWithToppings;
}
public void setCrust(Pizza pizza)
{
if (thin.isSelected())
{
pizza.setCrust("thin");
}
else if (handtossed.isSelected())
{
pizza.setCrust("hand");
}
else
{
pizza.setCrust("pan");
}
}
public void setSize(Pizza pizza)
{
if (small.isSelected())
{
pizza.setSize('S');
}
else if (medium.isSelected())
{
pizza.setSize('M');
}
else
{
pizza.setSize('L');
}
}
public DecoratedPizza setToppings(DecoratedPizza pizza)
{
pepperoni.setSelected(true);
sausage.setSelected(true);
onions.setSelected(true);
peppers.setSelected(true);
mushrooms.setSelected(true);
ListModel model = lstToppings.getModel();
for(int i = 0; i < model.getSize(); i++)
{
String topping = (String)model.getElementAt(i);
if(topping.equals("Pepperoni"))
pepperoni.setSelected(false);
else if(topping.equals("Sausage"))
sausage.setSelected(false);
else if(topping.equals("Onions"))
onions.setSelected(false);
else if(topping.equals("Peppers"))
peppers.setSelected(false);
else if(topping.equals("Mushrooms"))
mushrooms.setSelected(false);
}
if (pepperoni.isSelected())
{
pizza = new Pepperoni(pizza);
}
if (onions.isSelected())
{
pizza = new Onions(pizza);
}
if (peppers.isSelected())
{
pizza = new Peppers(pizza);
}
if (sausage.isSelected())
{
pizza = new Sausage(pizza);
}
if (mushrooms.isSelected())
{
pizza = new Mushrooms(pizza);
}
return pizza;
}
public void draw(Graphics g, int width, int height)
{
pizza.draw(g,width,height);
}
public void actionPerformed(ActionEvent ae)
{
//the pizza has been ordered, so now we make it
if (ae.getActionCommand().equals("Order"))
{
String temp = pizza.toString();
java.text.DecimalFormat fmt = new java.text.DecimalFormat("0.00");
temp = temp + "$" + fmt.format(pizza.pizzaCost());
SimpleDialogs.normalOutput(temp, "Pizza Order");
//reset some stuff
lstToppings.reset();
resetCheckBoxes();
}
//called after each interaction with the GUI to make sure the latest pizza is displayed
pizza = buildPizza();
}
public void resetCheckBoxes()
{
pepperoni.setSelected(false);
sausage.setSelected(false);
onions.setSelected(false);
peppers.setSelected(false);
mushrooms.setSelected(false);
}
}
Explanation / Answer
add pizzaPanel.show(); at the end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.