Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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();
}

}


0
usmbay
Asked:
usmbay
  • 4
  • 4
  • 3
  • +1
1 Solution
 
Giant2Commented:
>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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Does the JList take the entire space?
0
 
usmbayAuthor Commented:
no it taks part of it, but I don't know how to draw the number in a panel and add it to Jframe
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Giant2Commented:
0
 
Giant2Commented:
0
 
usmbayAuthor Commented:
I cann't find a solution for this!!!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try putting it in an image and draw the image instead.
0
 
usmbayAuthor Commented:
haw I can do that??
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Using g.drawImage ()
0
 
Giant2Commented:
What is not clear in the example-idea I told you?
0
 
usmbayAuthor Commented:
I just used repaint()
and it works

thanks
0
 
CetusMODCommented:
PAQed with points refunded (150)

CetusMOD
Community Support Moderator
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now