usmbay
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+2 0); // 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+2 0); // 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.setVisibleRowCou nt( 4 );
colorList.setSelectionMode (
ListSelectionModel.SINGLE_ SELECTION );
container.add( new JScrollPane( colorList ) );
colorList.addListSelection Listener( new ListSelectionListener()
{
public void valueChanged( ListSelectionEvent event )
{
container.setBackground(co lors[ colorList.getSelectedIndex () ] );
}
} // end of addListSelectionListener
); // end of ColorList
setSize( 400, 200 );
setVisible( true );
}
public static void main(String args[])
{
ColorList application = new ColorList();
}
}
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+2
}
private void drawLine3 (Graphics g, int x, int y)
{
g.drawLine(x+20,y+20,x+20,
}
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+2
}
///////////***************
public ColorList()
{
super( "Background Color List" );
container = getContentPane();
container.setLayout( new FlowLayout() );
// list with items in colorNames array
colorList = new JList( colorNames );
colorList.setVisibleRowCou
colorList.setSelectionMode
ListSelectionModel.SINGLE_
container.add( new JScrollPane( colorList ) );
colorList.addListSelection
{
public void valueChanged( ListSelectionEvent event )
{
container.setBackground(co
}
} // end of addListSelectionListener
); // end of ColorList
setSize( 400, 200 );
setVisible( true );
}
public static void main(String args[])
{
ColorList application = new ColorList();
}
}
Does the JList take the entire space?
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
Here you can find an idea:
http://javaalmanac.com/egs/java.awt/Pie.html
http://javaalmanac.com/egs/java.awt/Pie.html
ASKER
I cann't find a solution for this!!!
Try putting it in an image and draw the image instead.
ASKER
haw I can do that??
Using g.drawImage ()
What is not clear in the example-idea I told you?
ASKER
I just used repaint()
and it works
thanks
and it works
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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.