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

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

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

Solved

Posted on 2006-05-17

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;

}

}

*/

}

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

("Enter number to draw: ");

inputNumber = Integer.parseInt(inpStr);

/*

{

int y;

y = 10;

while (y <= 210)

{

g.drawLine(10, y, 100, y);

y = y + 25;

}

}

*/

}

5 Comments

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

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

public void askInput()

{

String inpStr = JOptionPane.showInputDialo

inputNumber = Integer.parseInt(inpStr);

}

public void paint( Graphics g)

{

super.paint(g);

//g.drawString("N="+inputN

...

}

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,

}

private void drawLine2 (Graphics g, int inX, int inY) {

g.drawLine(inX+20,inY,inX+

}

private void drawLine3 (Graphics g, int inX, int inY) {

g.drawLine(inX+20,inY+20,i

}

private void drawLine4 (Graphics g, int inX, int inY) {

g.drawLine(inX,inY+40,inX,

}

private void drawLine5 (Graphics g, int inX, int inY) {

g.drawLine(inX,inY+20,inX,

}

private void drawLine6 (Graphics g, int inX, int inY) {

g.drawLine(inX,inY,inX,inY

}

private void drawLine7 (Graphics g, int inX, int inY) {

g.drawLine(inX,inY+20,inX+

}

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.

Define the following class varriable:

private BasicStroke lineStroke;

Add this method:

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(

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.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**16** Experts available now in Live!