The Artima Developer Community
Sponsored Link

Java Answers Forum
What is Graphics g?

2 replies on 1 page. Most recent reply: Aug 14, 2002 11:18 AM by thomas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Leo

Posts: 6
Nickname: dev313
Registered: Aug, 2002

What is Graphics g? Posted: Aug 13, 2002 10:38 PM
Reply to this message Reply
Advertisement
How do you pass this object, from the paint method, to another method? How do you know what data type it is?

I tried passing a String from the paint method to a method that parses the string into an integer and then uses a nested For Loop to create an array of asterisks. Doesn't an applet support "\n"????? I can post code if needed. All I get in a nested for loop of <=4 for each loop is a straight line of 16 asterisks, not the four lines of four asterisks that I need. I am concatenating my "\n" in the outer for loop.

Thanks everyone,


Leo

Posts: 6
Nickname: dev313
Registered: Aug, 2002

Re: What is Graphics g? Posted: Aug 14, 2002 10:44 AM
Reply to this message Reply
Ok, so this was part of an assingment. But I figured it out.

package homework9;

// Java core packages
import java.awt.Graphics; // import class Graphics

// Java extension packages
import javax.swing.*; // import package javax.swing

public class Homework9 extends JApplet {

// These are our A's...

int Side; // number of sides as an integer
String strSide; // number of sides as a string

// initialize applet by obtaining values from user
public void init()
{

// obtain number of sides from user
strSide = JOptionPane.showInputDialog(
"Enter the number of sides" );

} // end method init

// draw results
public void paint( Graphics g )
{
String result; // the asterisk that we will paint

// call inherited version of method paint
super.paint( g );

result = squareOfAsterisks( strSide );

for ( int i = 1; i <= Side; i++ ){

g.drawString( result, 25, i * 25 );

} // end of for loop

//JOptionPane.showMessageDialog(null, numSides, "Title",

JOptionPane.INFORMATION_MESSAGE);

} // end method paint


// our squareOfAsterisks Method...
public String squareOfAsterisks( String x )
{
int row = 1;
int column = 1;

String strOutPut; // our groups of asterisks
strOutPut = ""; // initialize our output variable

// convert numbers from type String to type double that the user entered
Side = Integer.parseInt( x );

// print a row of 'Side' number of Asterisks...
for ( row = 1; row <= Side; row++ ) {

strOutPut += "* ";

} // end for structure

return strOutPut;

} // end method squareOfAsterisks

} // end class AdditionApplet

thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: What is Graphics g? Posted: Aug 14, 2002 11:18 AM
Reply to this message Reply
Graphics g is an object of the graphics class. It is abstract meaning it is never created by the constructor it is always assumed to be created. \n deals with print() not used in applets. applets use g.drawString("",x corridnate,y corrodnate); to get a new line you change the y corridnate you don't use \n.
Graphics g=getGraphics(); is the usual way to set up a graphics object.

Flat View: This topic has 2 replies on 1 page
Topic: designing classes Previous Topic   Next Topic Topic: jdk 1.4 swing gui

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use