Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Graphics in Jpanel are not showing! Help please ! T^T

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Graphics in Jpanel are not showing! Help please ! T^T

    Okay. So I am trying to program a game where you can decorate your own cake. When you click on the buttons their suppose to draw graphics on the top panel. But when I click on the button nothing shows up. I tried everything i could. Please help if you can, Thanks !

    P.S. I am so sorry if i posted in the wrong forum, this is my first post.

       import javax.swing.*;
       import java.awt.*;
       import java.awt.Color;
       import java.awt.event.*;
       import java.awt.Graphics;
       import java.awt.Graphics2D;
       import javax.swing.JComponent;
       import java.awt.event.ActionListener.*;
       import java.awt.event.ActionListener;
       import java.awt.event.MouseAdapter;
       import java.awt.event.MouseEvent;
     
     
       public class CakeMakerFINAL extends JPanel
     
       {
       //Colours
          public static final Color LIGHTBLUE = new Color (0xAD, 0xD8, 0xE6);
          JFrame window;
          JPanel panel = new JPanel(new GridBagLayout());
          JPanel panel1 = new JPanel(new GridBagLayout());
     
          boolean angelcake = false;
          boolean mocha = false;
          boolean vanilla = false;
          boolean choco = false;
          boolean berry = false;
          boolean fruits = false;
          boolean cream = false;
          boolean birthday = false;
     
     
          @Override public void paintComponent(Graphics g)
          {
             super.paintComponent(g);
             g.setColor(Color.RED);
             g.drawRect (200,220,50,20);
             repaint();
          }
     
          //methods to paint on graphics, (The buttons action)     
          public void myPaint(Graphics g)
          {
             if (angelcake)
             {
                g.setColor(Color.RED);
                g.drawRect (30,30,50,20);
                repaint();
             }
     
             if (mocha)
             {
                g.drawOval(10,10, 100, 100);
             }
     
             if (vanilla)
             {
     
             } 
             if (choco)
             {
                g.drawOval(10,10, 100, 100);
             }
     
             if (berry)
             {
     
             }
             if (fruits)
             {
                g.drawOval(10,10, 100, 100);
             }
     
             if (cream)
             {
     
             }
             if (birthday)
             {
                g.drawOval(10,10, 100, 100);
             }     
          }
     
          public CakeMakerFINAL()
          {
          //Frame and panel components
             JFrame frame = new JFrame("Cake Maker Game");
             frame.setVisible(true);
             frame.setSize(600,700);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(panel, BorderLayout.SOUTH);
             frame.getContentPane().add(panel1, BorderLayout.NORTH);
             panel.setBackground(LIGHTBLUE);     	      
     
          //Positions buttons in JPanel
             GridBagConstraints c = new GridBagConstraints();
             //Labels
             JLabel label1 = new JLabel("Type of Cake");
             c.gridx = 0;
             c.gridy = 0;
             c.insets = new Insets(20,20,20,20);
             panel.add(label1, c);
             JLabel label2 = new JLabel("Icing");
             c.gridx = 1;
             c.gridy = 0;
             panel.add(label2, c);
             JLabel label3 = new JLabel("Decorations");
             c.gridx = 2;
             c.gridy = 0;
             panel.add(label3, c);
     
            //BUTTONS for selection 	
             //Cake Choices
             JButton angelcake = new JButton("AngelCake");
             c.gridx = 0;
             c.gridy = 1;
             panel.add(angelcake, c);
             angelcake.addActionListener(new Action());
     
             JButton mocha = new JButton("Mocha");
             c.gridx = 0;
             c.gridy = 2;
             panel.add(mocha, c);
             mocha.addActionListener(new Action());
             // Icing Choices
             JButton vanilla = new JButton("Vanilla");
             c.gridx = 1;
             c.gridy = 1;
             panel.add(vanilla, c);
             vanilla.addActionListener(new Action());
     
             JButton choco = new JButton("Choco");
             c.gridx = 1;
             c.gridy = 2;
             panel.add(choco, c);
             choco.addActionListener(new Action());
     
             JButton berry = new JButton("Strawberry");
             c.gridx = 1;
             c.gridy = 3;
             panel.add(berry, c);
             berry.addActionListener(new Action());
     
             // Decoration Choices
             JButton fruits = new JButton("Fruits");
             c.gridx = 2;
             c.gridy = 1;
             panel.add(fruits, c);
             fruits.addActionListener(new Action());
     
             JButton cream = new JButton("Whip Cream");
             c.gridx = 2;
             c.gridy = 2;
             panel.add(cream, c);
             cream.addActionListener(new Action());
     
             JButton birthday = new JButton("Birthday!~");
             c.gridx = 2;
             c.gridy = 3;
             panel.add(birthday, c);
             birthday.addActionListener(new Action());
     
          }  
       //Button Action, Draws graphic. 	
          public class Action implements ActionListener
          {
             public void actionPerformed (ActionEvent e)
             {
                if(e.getSource() == angelcake)
                {
                   angelcake = true;
                }
     
                else if (e.getSource() == mocha)
                {
                   mocha = true;
                }
     
                else if (e.getSource() == vanilla)
                {
                   vanilla = true;
                }
                else if (e.getSource() == choco)
                {
                   choco = true;
                }
                else if (e.getSource() == berry)
                {
                   berry = true;
                }
                else if (e.getSource() == fruits)
                {
                   fruits = true;
                }
                else if (e.getSource() == cream)
                {
                   cream = true;
                }
                else if (e.getSource() == birthday)
                {
                   birthday  = true;
                }
             }
          }	
          public static void main(String[] args)
          {
             CakeMakerFINAL cakemakerFinal = new CakeMakerFINAL();
          }
     
       }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Graphics in Jpanel are not showing! Help please ! T^T

    As a general rule: work one small step at a time.

    So step back from your cake application as it currently stands and write something with a single button that makes a single change to the GUI. Then, when you have something that does what you want, apply it as a "pattern".

    Quite likely the problem lies in the fact that your action handler doesn't cause a repaint.

Similar Threads

  1. Cannot draw graphics on JPanel
    By ICEPower in forum AWT / Java Swing
    Replies: 2
    Last Post: March 19th, 2013, 05:42 AM
  2. Moving content (graphics) inside a JPanel
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: February 1st, 2012, 01:21 PM
  3. [SOLVED] JPanel not showing in JFrame
    By Tjstretch in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2011, 12:27 AM
  4. Showing JPanel error
    By Fermen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 12:12 PM
  5. How to add scrollbar to a JPanel with Graphics
    By Tanguo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 08:53 PM

Tags for this Thread