Link to home
Start Free TrialLog in
Avatar of usmbay
usmbayFlag for United States of America

asked on

draw digit and change background color

in the following code I'm tring to draw digit (must use drawLine ) and change the background color
but the drawing digit not appers when I put the JList scrollpane
can u take a look and tell me what's wrong!!!

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ColorList extends JFrame {
private JList colorList;
private Container container;

private String colorNames[] = { "Black", "Blue",
                  "Green", "Orange",
                  "Pink", "Red",
                  "White", "Yellow" };

 private Color colors[] = { Color.black, Color.blue,
                  Color.green, Color.orange,
                  Color.pink, Color.red,
                  Color.white, Color.yellow };


/////////////////*******************/////////////////

public void paint( Graphics g)
{

      int digit,x,y;
      super.paint(g);


      x = 10; // x location of the digit (upper left corner)
      y = 10; // y location of the digit (upper left corner)

      for (int counter=0; counter<=10; counter++)  // to draw digit 4
      {
            drawLine2 (g,x+counter,y);
            drawLine3 (g,x+counter,y);
            drawLine6 (g,x+counter,y);
            drawLine7 (g,x,y+counter);
      }

}

private void drawLine2 (Graphics g, int x, int y)
      {
      g.drawLine(x+20,y,x+20,y+20);  // vertical line at top right of digit
      }
private void drawLine3 (Graphics g, int x, int y)
      {
      g.drawLine(x+20,y+20,x+20,y+40);  // vertical line at bottom right of digit
      }

private void drawLine6 (Graphics g, int x, int y)
      {
      g.drawLine(x,y,x,y+20);  // vertical line at top left of digit
      }

private void drawLine7 (Graphics g, int x, int y)
      {
      g.drawLine(x,y+20,x+20,y+20);  // horizontal line in middle of digit
      }



///////////***************//////////////////*************

public ColorList()
{
      super( "Background Color List" );

      container = getContentPane();
      container.setLayout( new FlowLayout() );


      // list with items in colorNames array
      colorList = new JList( colorNames );
      colorList.setVisibleRowCount( 4 );

      colorList.setSelectionMode(
      ListSelectionModel.SINGLE_SELECTION );

      container.add( new JScrollPane( colorList ) );

      colorList.addListSelectionListener( new ListSelectionListener()
      {
            public void valueChanged( ListSelectionEvent event )
            {
            container.setBackground(colors[ colorList.getSelectedIndex() ] );
            }

      } // end of addListSelectionListener

); // end of ColorList

setSize( 400, 200 );
setVisible( true );
 }

public static void main(String args[])
{

ColorList application = new ColorList();
}

}


Avatar of Giant2
Giant2

>but the drawing digit not appers when I put the JList scrollpane
> container.add( new JScrollPane( colorList ) );
in this manner the JList and its scrollpane full all the JFrame, so your number disappear.
Try to draw your number in a panel/canvas and add it to your JFrame.
Avatar of Mayank S
Does the JList take the entire space?
Avatar of usmbay

ASKER

no it taks part of it, but I don't know how to draw the number in a panel and add it to Jframe
Avatar of usmbay

ASKER

I cann't find a solution for this!!!
Try putting it in an image and draw the image instead.
Avatar of usmbay

ASKER

haw I can do that??
Using g.drawImage ()
What is not clear in the example-idea I told you?
Avatar of usmbay

ASKER

I just used repaint()
and it works

thanks
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial