Solved

# draw a digit

Posted on 2006-05-17
449 Views
I need to draw a digit using drawLine method
I know that I sould use    g.drawLine(x1,y1,x2,y2)
but how to the number , any idea !!!

public void paint( Graphics g)
{

int inputNumber;
String inpStr;
super.paint(g);

inpStr =
JOptionPane.showInputDialog
("Enter number to draw: ");
inputNumber = Integer.parseInt(inpStr);

/*
{
int y;
y = 10;
while (y <= 210)
{
g.drawLine(10, y, 100, y);
y = y + 25;
}
}

*/
}

0
Question by:usmbay

LVL 86

Expert Comment

Depending on what component you have, you may need to override paintComponent

g.drawString("900", xPos, yPos);
0

LVL 13

Expert Comment

Hi usmbay,

the paint method must only contains drawing operations, using the Graphics object parameter

->
int inputNumber;  // <-- must be a class member, to reuse value in paint()
{
String inpStr = JOptionPane.showInputDialog("Enter number to draw: ");
inputNumber = Integer.parseInt(inpStr);
}

public void paint( Graphics g)
{
super.paint(g);
//g.drawString("N="+inputNumber, 10,10);
...
}

0

LVL 1

Accepted Solution

Webstorm is correct that you will need to take your get input number code out of paint and move it to its own method.

Actually drawing the number can be a little complicated.

How many digits do you have to be able to draw (how large can the input number be)?

If you ony need to draw one digit your paint method migtht look like this.

public void paint( Graphics g)
{
super.paint(g);

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

// figure out what number to draw
Switch (inputNumber) {
case 1: draw1(g,x,y);
case 2: draw2(g,x,y);
case 3: draw3(g,x,y);
case 4: draw4(g,x,y);
case 5: draw5(g,x,y);
case 6: draw6(g,x,y);
case 7: draw7(g,x,y);
case 8: draw8(g,x,y);
case 8: draw8(g,x,y);
case 9: draw9(g,x,y);
default: draw0(g,x,y);
}
}

Each of the draw methods called above would be able to draw one digit at the location spcified.  If you don't mind your number looking like a digital clock you can draw any digit with some combination of only 7 lines. Each line in the digit could have its own method which would be used by the "drawN" mwthods used by your paint method.  For example:

// Draw lines to be used in a 20 by 40 pixel digital style digit
// inX and inY are the point where you want the digit drawn

private void drawLine1 (Graphics g, int inX, int inY) {
g.drawLine(inX,inY,inX+20,inY)  // horizontal line at the top of digit
}
private void drawLine2 (Graphics g, int inX, int inY) {
g.drawLine(inX+20,inY,inX+20,inY+20)  // vertical line at top right of digit
}
private void drawLine3 (Graphics g, int inX, int inY) {
g.drawLine(inX+20,inY+20,inX+20,inY+40)  // vertical line at bottom right of digit
}
private void drawLine4 (Graphics g, int inX, int inY) {
g.drawLine(inX,inY+40,inX,inY)  // horizontal line at bottom of digit
}
private void drawLine5 (Graphics g, int inX, int inY) {
g.drawLine(inX,inY+20,inX,inY+40)  // vertical line at bottom left of digit
}
private void drawLine6 (Graphics g, int inX, int inY) {
g.drawLine(inX,inY,inX,inY+20)  // vertical line at top left of digit
}
private void drawLine7 (Graphics g, int inX, int inY) {
g.drawLine(inX,inY+20,inX+20,inY+20)  // horizontal line in middle of digit
}

Lines 2 and 3 are all that are needed to draw a digital number 1 so the following should work to get a "1" on the screen.

private void draw1(Graphics g, int inX, int inY) {
drawLine2 (g,inX,inY);
drawLine3 (g,inX,inY);
}

Lines 1,2,3 can make a "7"
private void draw1(Graphics g, int inX, int inY) {
drawLine1 (g,inX,inY);
drawLine2 (g,inX,inY);
drawLine3 (g,inX,inY);
}

All lines are needed to draw an 8
private void draw8(Graphics g, int inX, int inY) {
drawLine1 (g,inX,inY);
drawLine2 (g,inX,inY);
drawLine3 (g,inX,inY);
drawLine4 (g,inX,inY);
drawLine5 (g,inX,inY);
drawLine6 (g,inX,inY);
drawLine7 (g,inX,inY);
}

You should be getting the idea now.  You can also use this method to draw numbers with lots of digits with a little more work.
0

Author Comment

it's good idea , but to control the thickness of the line
0

LVL 1

Expert Comment

Define the following class varriable:
private BasicStroke lineStroke;

public void setLineWidth(int inWidth) {
if (inWidth < 1) {
inWidth = 1;
}
lineStroke = new BasicStroke(
inWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
}

Put this in your paint method right after "super.paint(g);"
((Graphics2D)g).setStroke(lineStroke);

Now you can use the setLineWidth method to change the line width.  You can even have the user enter a line width value after they enter the digit to draw.
0

## Featured Post

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input: