Jump to content

Java: Fun with JPalette

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Hey C.C !

Dropping in with a small tutorial how to create a colour palette !
Anyhow lets begin with it.

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class JPalette extends JFrame {


//Our buttons and functions

   private JButton ChooseColour;

   private Color Colour= Color.lightGray;

   private Container c;


//Constructor

   public JPalette ()

   {

      super( "JColour Changer" );

//The Layout

      c = getContentPane();

      c.setLayout( new FlowLayout() );

//The button and palettes function layout

      ChooseColour= new JButton( "Colour Palette" );

      ChooseColour.addActionListener(new ActionListener() {

            public void actionPerformed( ActionEvent e ) {

               Colour =

                  JColorChooser.showDialog( JPalette.this,

                     "ChooseColour", Colour );

//If to check if listener works fine...

               if ( Colour== null )

                  Colour= Color.lightGray;


               c.setBackground(Colour);

               c.repaint();

            }

         }

      );

      c.add( ChooseColour);

//Size of the layout

      setSize( 400, 130 );

      show();

   }

//The main

   public static void main( String args[] )

   {

      JPalette JP= new JPalette ();

//Listener to the main to be able to close whenever we use "default" closing

which is the X button...

      JP.addWindowListener(

         new WindowAdapter() {

            public void windowClosing( WindowEvent e )

            {

               System.exit( 0 );

            }

         }

      );

   }

}

So there you go !
Have fun and enjoy :)
Posted Image

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I moved this to the Code section since you didn't explain anything. Thanks for the share!

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Jordan said:

I moved this to the Code section since you didn't explain anything. Thanks for the share!

Oh, okay well hmm, should have explained abit better, hate it when I can't explain :S
Posted Image