Graphics2D and Math classes in an Applet
Douglas Braxton
Ranch Hand
Posts: 36
posted 13 years ago
i am working on a Java homework assignment that has me stumped. i am a Java newbie having taken a noncredit course and now am in the for credit version at my local CC.
i am getting compiler errors.
the homework bullet says
"Add one or more instances of the classes  Ellipse2D.Float, RoundRectangle.Double. Ellipse2D.Double, Line2D.Double or Arc2D.Double  to your paint method. Creativity is encouraged."
here is my Applet code:
public class Assign2Applet extends JApplet
{
int width = 700;//screen width
int height = 600;//screen height
int w;
int x;
int y;
int z;
int mx;
int mn;
public void init ()
{
String oneN; // first number entered from standard input
String twoN; // second number entered from standard input
String threeN; // third number entered from standard input
String fourN; // fourth number entered from standard input
// the display values defined as strings
String oneVal, twoVal, threeVal, fourVal;
// read in the four number entries
oneN = JOptionPane.showInputDialog("Enter first integer: ");
twoN = JOptionPane.showInputDialog("Enter second integer: ");
threeN = JOptionPane.showInputDialog("Enter third integer: ");
fourN = JOptionPane.showInputDialog("Enter fourth integer: ");
setForeground( Color.blue );
// convert entries from type String to type int & test for 0
try { w = Integer.parseInt( oneN ); }
catch ( NumberFormatException nfe) { w = 0; }
try { x = Integer.parseInt( twoN ); }
catch ( NumberFormatException nfe) { x = 0; }
try { y = Integer.parseInt( threeN ); }
catch ( NumberFormatException nfe) { y = 0; }
try { z = Integer.parseInt( fourN ); }
catch ( NumberFormatException nfe) { z = 0; }
// Determine the largest of the four entered numbers
return Math.max(w, Math.max(Math.max(x,y), Math.max(y,z)));
// Determine the smallest of the four entered numbers
return Math.min(w, Math.min(Math.min(x,y), Math.min(y,z)));
DecimalFormat mxFormatter = new DecimalFormat("###,###,###");
String mxVal = "The maximum is: " + mxFormatter.format(mx);
JOptionPane.showMessageDialog( null, mxVal );
DecimalFormat mnFormatter = new DecimalFormat("###,###,###");
String mnVal = "The minimum is: " + mnFormatter.format(mn);
JOptionPane.showMessageDialog( null, mnVal );
}
public void start ()
{
// called every time the user enters the webpage w/ the applet
}
public void stop ()
{
// stop running and unload resources
// called when user leaves or minimizes the webpage
}
public void destroy ()
{
// called once when browser or appletviewer exits
// quitting    final cleanup
}
public void paint ( Graphics g )
{ // paint called every time the applet needs to be repainted
this.setSize( width, height );
Font f = new Font("Serif", Font.BOLD, 18 );
super.paint(g);
// use Graphics class
// use the entered #'s to define the perimeter of the rectangle
g.drawRect( w, x, y, z );
// use the entered #'s to define the perimeter of the oval
g.drawOval( w+350, x+50, y20, z100 );
// use the entered #'s to define the perimeter of the oval
g.drawOval( w+10, x+50, y20, z100 );
// add 2D effect thru casting ( Graphics2D object )
Graphics2D g2 = ( Graphics2D ) g; // type cast
g2.setFont( f ); // set font
g2.setColor( Color.red ); // set color
g2.fillRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
g2.drawRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
Rectangle2D.Float r2Df = new Rectangle2D.Float( 80, 50, 75, 120 );
g2.draw( r2Df);
g2.draw3DRect(550, 110, 75, 120, false);// true = raised
g2.setColor( Color.red );
g2.fillOval( 180, 150, 200, 100);
} // end of paint
} // end of class
and here are the compiler errors
Assign2Applet.java [59:1] cannot return a value from method whose result type is void
return Math.max(w, Math.max(Math.max(x,y), Math.max(y,z)));
^
Assign2Applet.java [62:1] cannot return a value from method whose result type is void
return Math.min(w, Math.min(Math.min(x,y), Math.min(y,z)));
^
Assign2Applet.java [122:1] cannot resolve symbol
symbol : variable fillRoundRect2D
location: class java.awt.Graphics2D
g2.fillRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
^
Assign2Applet.java [123:1] cannot resolve symbol
symbol : variable drawRoundRect2D
location: class java.awt.Graphics2D
g2.drawRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
^
4 errors
Errors compiling Assign2Applet.
My questions are"
1: why does the compliler think Math.max() and Math.min() methods are type VOID?
2: what is needed to resolve the symbols g2.fillRoundRect2D.Double and g2.drawRoundRect2D.Double?
Thank you for your help!!!
i am getting compiler errors.
the homework bullet says
"Add one or more instances of the classes  Ellipse2D.Float, RoundRectangle.Double. Ellipse2D.Double, Line2D.Double or Arc2D.Double  to your paint method. Creativity is encouraged."
here is my Applet code:
public class Assign2Applet extends JApplet
{
int width = 700;//screen width
int height = 600;//screen height
int w;
int x;
int y;
int z;
int mx;
int mn;
public void init ()
{
String oneN; // first number entered from standard input
String twoN; // second number entered from standard input
String threeN; // third number entered from standard input
String fourN; // fourth number entered from standard input
// the display values defined as strings
String oneVal, twoVal, threeVal, fourVal;
// read in the four number entries
oneN = JOptionPane.showInputDialog("Enter first integer: ");
twoN = JOptionPane.showInputDialog("Enter second integer: ");
threeN = JOptionPane.showInputDialog("Enter third integer: ");
fourN = JOptionPane.showInputDialog("Enter fourth integer: ");
setForeground( Color.blue );
// convert entries from type String to type int & test for 0
try { w = Integer.parseInt( oneN ); }
catch ( NumberFormatException nfe) { w = 0; }
try { x = Integer.parseInt( twoN ); }
catch ( NumberFormatException nfe) { x = 0; }
try { y = Integer.parseInt( threeN ); }
catch ( NumberFormatException nfe) { y = 0; }
try { z = Integer.parseInt( fourN ); }
catch ( NumberFormatException nfe) { z = 0; }
// Determine the largest of the four entered numbers
return Math.max(w, Math.max(Math.max(x,y), Math.max(y,z)));
// Determine the smallest of the four entered numbers
return Math.min(w, Math.min(Math.min(x,y), Math.min(y,z)));
DecimalFormat mxFormatter = new DecimalFormat("###,###,###");
String mxVal = "The maximum is: " + mxFormatter.format(mx);
JOptionPane.showMessageDialog( null, mxVal );
DecimalFormat mnFormatter = new DecimalFormat("###,###,###");
String mnVal = "The minimum is: " + mnFormatter.format(mn);
JOptionPane.showMessageDialog( null, mnVal );
}
public void start ()
{
// called every time the user enters the webpage w/ the applet
}
public void stop ()
{
// stop running and unload resources
// called when user leaves or minimizes the webpage
}
public void destroy ()
{
// called once when browser or appletviewer exits
// quitting    final cleanup
}
public void paint ( Graphics g )
{ // paint called every time the applet needs to be repainted
this.setSize( width, height );
Font f = new Font("Serif", Font.BOLD, 18 );
super.paint(g);
// use Graphics class
// use the entered #'s to define the perimeter of the rectangle
g.drawRect( w, x, y, z );
// use the entered #'s to define the perimeter of the oval
g.drawOval( w+350, x+50, y20, z100 );
// use the entered #'s to define the perimeter of the oval
g.drawOval( w+10, x+50, y20, z100 );
// add 2D effect thru casting ( Graphics2D object )
Graphics2D g2 = ( Graphics2D ) g; // type cast
g2.setFont( f ); // set font
g2.setColor( Color.red ); // set color
g2.fillRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
g2.drawRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
Rectangle2D.Float r2Df = new Rectangle2D.Float( 80, 50, 75, 120 );
g2.draw( r2Df);
g2.draw3DRect(550, 110, 75, 120, false);// true = raised
g2.setColor( Color.red );
g2.fillOval( 180, 150, 200, 100);
} // end of paint
} // end of class
and here are the compiler errors
Assign2Applet.java [59:1] cannot return a value from method whose result type is void
return Math.max(w, Math.max(Math.max(x,y), Math.max(y,z)));
^
Assign2Applet.java [62:1] cannot return a value from method whose result type is void
return Math.min(w, Math.min(Math.min(x,y), Math.min(y,z)));
^
Assign2Applet.java [122:1] cannot resolve symbol
symbol : variable fillRoundRect2D
location: class java.awt.Graphics2D
g2.fillRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
^
Assign2Applet.java [123:1] cannot resolve symbol
symbol : variable drawRoundRect2D
location: class java.awt.Graphics2D
g2.drawRoundRect2D.Double( w+475, x+50, y20, z100, 20, 20 );
^
4 errors
Errors compiling Assign2Applet.
My questions are"
1: why does the compliler think Math.max() and Math.min() methods are type VOID?
2: what is needed to resolve the symbols g2.fillRoundRect2D.Double and g2.drawRoundRect2D.Double?
Thank you for your help!!!
posted 13 years ago
Hi Douglas,
Welcome to JavaRanch!
The message about "return from void" means that you're using the "return" statement with a value from inside the init() method, which returns void. I can't tell why you're using "return" here:
It looks as though what you really want to do is
Similar with "mn". Do you understand the problem now?
Now as far as the lines like
This is suggesting that there's a variable "fillRoundRect2D" inside the Graphics2D object, which there is not; and that the object contained by that imaginary variable has a function "Double" which you're calling with the given parameter list. There's nothing like this anywhere  not sure where the name "fillRoundRect2D" even came from. Graphics2D has a "fill" method that takes a "Shape" parameter, and the class RoundRectangle2D implements the Shape interface, so I think you were trying to do something like
Again, do you understand what I did here? I'm a little concerned by the appearance of the name "fillRoundRect2D" which appears to just be a figment of the imagination; better to use the Java platform API documentation and look things up.
Welcome to JavaRanch!
The message about "return from void" means that you're using the "return" statement with a value from inside the init() method, which returns void. I can't tell why you're using "return" here:
It looks as though what you really want to do is
Similar with "mn". Do you understand the problem now?
Now as far as the lines like
This is suggesting that there's a variable "fillRoundRect2D" inside the Graphics2D object, which there is not; and that the object contained by that imaginary variable has a function "Double" which you're calling with the given parameter list. There's nothing like this anywhere  not sure where the name "fillRoundRect2D" even came from. Graphics2D has a "fill" method that takes a "Shape" parameter, and the class RoundRectangle2D implements the Shape interface, so I think you were trying to do something like
Again, do you understand what I did here? I'm a little concerned by the appearance of the name "fillRoundRect2D" which appears to just be a figment of the imagination; better to use the Java platform API documentation and look things up.
Douglas Braxton
Ranch Hand
Posts: 36
posted 13 years ago
ernest
thanx for the review.
i understand what you are saying about the min/max. i lifted that out of another assignment where it was working but that was a MAIN and not an applet. don't know if that is why it fails or not but will try to code it as you suggested. the bullet for that part of the assignment said
"Print the largest and smallest integers supplied from the four integers."
the bullet that i listed in my previous post was where i got the method "RoundRectangle.Double". what i failed to state is that the assignments are notoriously for being filled with typos and incorrect verbiage which this assignment seems to be victim to.
will try suggestions and recompile.
thanx
db
thanx for the review.
i understand what you are saying about the min/max. i lifted that out of another assignment where it was working but that was a MAIN and not an applet. don't know if that is why it fails or not but will try to code it as you suggested. the bullet for that part of the assignment said
"Print the largest and smallest integers supplied from the four integers."
the bullet that i listed in my previous post was where i got the method "RoundRectangle.Double". what i failed to state is that the assignments are notoriously for being filled with typos and incorrect verbiage which this assignment seems to be victim to.
will try suggestions and recompile.
thanx
db
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
