Results 1 to 15 of 15
  1. #1

    Need help on Java graphics

    Hi, i wrote this driver class and a GUI class derived from JPanel.

    in the GUI class..

    i have the following statement..
    setBackground (Color.black);

    but whenever i run the application, the background is always white..

    anyone know why?

    Thx..

  2. #2
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    I would first try doing a repaint() right after your set BG color.

    Next thing to try would be a setOpaque(true); before the set bg color.

    Lastly try a super.paintComponent() right after you set the bg color...

    If none of those solve it then I'll need to see the code to say more.

  3. #3
    yay.. super.paintComponent() worked.. thx.

    another question..

    i have two buttons in my application.. but i can't see the buttons when i run it.. i can only see it when at click at the place where the button is.. and when i lift my finger, the button disapplear..

    anyone know wat's wrong..

    thx..

  4. #4
    Join Date
    Apr 2003
    Posts
    55
    try this...

    button.setVisible(true);

    (button is your button instance name)

    Cedric

  5. #5
    hmm.. it didn't work..

  6. #6
    Join Date
    Apr 2003
    Posts
    55
    i will need to see the code to help further, it looks like something simple but I can't tell till i can see at least the code chuck in question.

    Cedric

  7. #7
    oh ok.. here is the part involving the buntton..

    public class RocketPanel extends JPanel
    {
    private final int WIDTH = 250, HEIGHT = 300;
    private JButton on_button, off_button;
    }

    public RocketPanel ()
    {
    on_button = new JButton ("Click here to start the engine!");
    off_button = new JButton ("Click here to stop the engine!");

    on_button.addActionListener (new first_ButtonListener());
    off_button.addActionListener (new second_ButtonListener());

    setLayout (new FlowLayout());
    add (on_button);
    setLayout (new FlowLayout());
    add (off_button);

    setBackground (Color.black);
    setPreferredSize (new Dimension(WIDTH, HEIGHT));
    }

    private class first_ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {
    condition = Color.red;
    repaint();
    }
    }

    private class second_ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {
    condition = Color.black;
    repaint();
    }
    }

  8. #8
    hmm the indents didn't show up..

    public class RocketPanel extends JPanel
    {
    private final int WIDTH = 250, HEIGHT = 300;
    private JButton on_button, off_button;
    }<--- ops, this goes to very end..

  9. #9
    Join Date
    Feb 2004
    Posts
    772
    Hi ,

    Can i know how you have used this setBackground(Color.black)?
    Actually you have to use

    <JpanelInstanceName>.setBackground(Color.black);If it doesn't work let me know your few lines of code to find the correct place of your bug.

    Regards,

    Bright

  10. #10
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Hi... I modified your code so that it could compile and ran it... It worked just fine. The buttons showed up before I clicked on them.

    Here is what I used:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class RocketPanel extends JPanel
    {
        private final int WIDTH = 250, HEIGHT = 300;
        private JButton on_button, off_button;
    
        public RocketPanel (){
            on_button = new JButton ("Click here to start the engine!");
            off_button = new JButton ("Click here to stop the engine!");
    
            on_button.addActionListener (new first_ButtonListener());
            off_button.addActionListener (new second_ButtonListener());
    
            setLayout (new FlowLayout());
            add (on_button);
            setLayout (new FlowLayout());
            add (off_button);
    
            setBackground (Color.black);
            setPreferredSize (new Dimension(WIDTH, HEIGHT));
        }
      
        public static void main(String arg[]){
            RocketPanel rp = new RocketPanel();
            JFrame jf = new JFrame();
            jf.getContentPane().add(rp);
            jf.pack();
            jf.show();
        }
    
        private class first_ButtonListener implements ActionListener{
            public void actionPerformed (ActionEvent event){
                //condition = Color.red;
                repaint();
            }
        }
    
        private class second_ButtonListener implements ActionListener{
            public void actionPerformed (ActionEvent event){
                //condition = Color.black;
                repaint();
            }
        }
    }

  11. #11
    oh.. right.. that worked.. i have that additional part that u added in in my main driver class as follow.. is there a way that i can make it seperate while not having the button problem?

    import javax.swing.*;

    public class Rocket
    {
    public static void main(String args[])
    {
    JFrame rocket_frame = new JFrame ("Fire the rocket!");

    rocket_frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    rocket_frame.getContentPane().add (new RocketPanel());
    rocket_frame.pack();
    rocket_frame.show();
    }
    }

    Thx..

    ----


    to Brightadmin,

    i know wat u mean.. as u can see my main driver class has wat it need to write that line.. but it just doesn't work.. it worked when i add this line *** in this part of my program..

    public void paint (Graphics page)
    {
    super.paintComponent(page); // <--***
    }

  12. #12
    anyone got an idea?

  13. #13
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Not sure what the problem is. You should be able to create the object from anywhere and it should act the same. Maybe if you could post the code that doesn't work...

  14. #14
    i find out the problem.. it's all cuz my method name^ is not same as the super.name^..

    it was originally this:

    public void paint (Graphics page)
    {
    super.paintComponent(page);

    }

    now change to this and it worked!

    public void paintComponent (Graphics page)
    {
    super.paintComponent(page);

    }
    OR
    public void paint (Graphics page)
    {
    super.paint(page);

    }

    thx..

  15. #15
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    k. Good luck on the project

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •