problem with awt.Color

Discussion in 'Java' started by tss, Dec 19, 2003.

  1. tss

    tss Guest

    Hi,
    It's my first application with JFrame. I have a problem with changing
    colors in my application.
    Frame name is: JFrame1.
    It contains a JPanel class named jPanel1.
    The panel contains a JButton.
    Event handler for the button has this code, which does nothing to the
    colors. It only draws in black no matter what the color is:

    jPanel1.getGraphics().setColor(java.awt.Color.blue);
    jPanel1.getGraphics().drawRect(0,0,100,100);

    I tried to change the foreground:

    jPanel1.setForeground(java.awt.Color.blue);

    It drew also in black !!!!!!

    can anyone explain this to me?????

    Thanks alot :)
     
    tss, Dec 19, 2003
    #1
    1. Advertising

  2. tss

    ak Guest

    //your JButton

    Color rc;

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(rc);
    g.drawRect(0,0,100,100);
    }

    event handler should set rc and call repaint();
    --

    ____________

    http://reader.imagero.com the best java image reader.
     
    ak, Dec 19, 2003
    #2
    1. Advertising

  3. "ak" <> wrote in message news:brvqu6$1fl$...
    > //your JButton
    >
    > Color rc;
    >
    > public void paintComponent(Graphics g) {


    What is it with coder's penchant for stuffing
    paint/repaint/paintComponent?

    Personally I blame all the 'Java in 24hrs'
    books that start with things like..
    '..now lets set the layout of the applet to
    null before we call paint'.. Aaaargh!

    Try this code that does not use any of those things..
    (I might put it on my site later ..no it's too trivial)
    ____________________________________________
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.BorderLayout;

    public class JColoredButtonFrame extends JFrame
    {
    JButton b1, b2;

    JColoredButtonFrame()
    {
    b1 = new JButton("White");
    b1.setForeground(Color.WHITE);
    b1.setBackground(Color.BLACK);
    b1.setOpaque(true);

    b2 = new JButton("Normal");

    getContentPane().add(b1, BorderLayout.NORTH);

    getContentPane().add(b2, BorderLayout.SOUTH);

    pack();
    setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
    setVisible(true);
    }

    public static void main(String args[])
    {
    JColoredButtonFrame cbf = new JColoredButtonFrame();
    }
    }
     
    Andrew Thompson, Dec 20, 2003
    #3
  4. "Andrew Thompson" <> wrote in message
    news:wSQEb.58946$...
    > "ak" <> wrote in message news:brvqu6$1fl$...
    > > //your JButton
    > >
    > > Color rc;
    > >
    > > public void paintComponent(Graphics g) {

    >
    > What is it with coder's penchant for stuffing
    > paint/repaint/paintComponent?


    ...err. That _shoulda_ read
    > What is it with coder's penchant for stuffing


    *** about with ***

    > paint/repaint/paintComponent?


    ;-)
     
    Andrew Thompson, Dec 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Warr

    awt Dialog color problem

    Chris Warr, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    903
    Chris Warr
    Jul 1, 2003
  2. Robert Mark Bram
    Replies:
    2
    Views:
    5,967
    Robert Mark Bram
    Dec 11, 2003
  3. Replies:
    0
    Views:
    416
  4. fig000
    Replies:
    0
    Views:
    4,556
    fig000
    Sep 6, 2004
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    429
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page