Solved

# draw digit and change background color

Posted on 2006-05-17
245 Views
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 ) );

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

); // end of ColorList

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

public static void main(String args[])
{

ColorList application = new ColorList();
}

}

0
Question by:usmbay

LVL 12

Expert Comment

>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.
0

LVL 30

Expert Comment

Does the JList take the entire space?
0

Author Comment

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

LVL 12

Expert Comment

0

LVL 12

Expert Comment

0

Author Comment

I cann't find a solution for this!!!
0

LVL 30

Expert Comment

Try putting it in an image and draw the image instead.
0

Author Comment

haw I can do that??
0

LVL 30

Expert Comment

Using g.drawImage ()
0

LVL 12

Expert Comment

What is not clear in the example-idea I told you?
0

Author Comment

I just used repaint()
and it works

thanks
0

Accepted Solution

PAQed with points refunded (150)

CetusMOD
Community Support Moderator
0

## Featured Post

### Suggested Solutions

Connect to IP Camera using Java 4 57
sumDigits  challenge 7 37
ArrayLists in ArrayLists/HashMaps? 4 26
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.