Member Avatar for loserspearl

I'm trying to create a java applet that displays farhen and cels temperatures in a chart. Temps below freezing need to be displayed in blue, and about boiling displayed in red. It needs to use applet graphics to draw the strings in different colors but I'm fairly lost when it comes to using different methods together. I don't know how I can draw a string based on a farhen temp while also incrementing farhen at the same time in seperate method.

Trust me I know this is really wrong I just don't know how to base drawstring color on my farhen variable.

import java.awt.*;
import java.applet.*;

public class TempConv extends Applet
{   
    public static void main ( String[] args ) 
    {
	
	System.out.println("Temperature Conversion Chart");
    }
	
	
    public void paint( Graphics g )
    {

	this.setBackground( Color.lightGray );
	g.setColor ( Color.black);
	g.drawString( "Fahrenheit" + "   " + "Clesius", 10, 15 );
	g.setColor( Color.red );
	g.drawLine( 12, 30, 200, 30);
	
	int col = 15;
	int row = 20;
	float farhen = 0;
	float cels;

	for (int i = 0; i < 25; i++){
	    if (farhen < 32){
	        cels = ( 5 ÷ 9 ) × ( farhen - 32 );
	        g.setColor ( Color.blue );
		g.drawString( "   " + farhen + "\u2109" +  "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 14;
	    }
	    else if{
		cels = ( 5 ÷ 9 ) × ( farhen - 32 );
	        g.setColor ( Color.black );
		g.drawString( "   " + farhen + "\u2109" + "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 14;
	    }
	    else{
		cels = ( 5 ÷ 9 ) × ( farhen - 32 );
	        g.setColor ( Color.black );
		g.drawString( "   " + farhen + "\u2109" + "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 14;
   
    }
}

I also need to format the output to make them all whole number but I think keeping the temps as int will do that. Any help is appreciated.

Start with using valid math symbols so that your program will compile.

After you get it to compile, consider that the only part of your code that will be different in all of those if-else cases is the check for which color to use.

You don't need to repeat all that code in each case. Just check the current value of 'faren' and set your color.

Also note that Applets don't use a main() method. Just remove that.

Member Avatar for loserspearl

Ha didn't even notice the math symbols, pasted from report.

And I'm doing a loop that goes from 0 to 250 incrementing by 10 each time. I want to to increment after any time it draws a string to get the new farhen and the new cels from that farhen, thats why its repeated in each statement of the if. Fixed some other bugs I found too. Compiles fine, all works under 1 method. But my celsius isnt calculating so it prints all 0's. Also I'm curious how to align the numbers all to the right not the left.

Heres the new code

public class TempConv extends Applet
{   	
    public void paint( Graphics g )
    {
	this.setBackground( Color.lightGray );
	g.setColor( Color.black);
	g.drawString( "Fahrenheit" + "   " + "Clesius", 10, 20 );
	g.setColor( Color.red );
	g.drawLine( 12, 23, 200, 23);
	
	int col = 20;
	int row = 40;
	int farhen = 0;
	int cels;

	for (int i = 0; i < 25; i++){
	    if (farhen < 32){
	        cels = ( 5 / 9 ) * ( farhen - 32 );
	        g.setColor( Color.blue );
		g.drawString( "   " + farhen + "\u2109" +  "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 15;
	    }
	    else if (farhen < 212){
		cels = ( 5 / 9 ) * ( farhen - 32 );
	        g.setColor( Color.black );
		g.drawString( "   " + farhen + "\u2109" + "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 15;
	    }
	    else{
		cels = ( 5 / 9 ) * ( farhen - 32 );
	        g.setColor( Color.red );
		g.drawString( "   " + farhen + "\u2109" + "   " + cels + "\u2103", col, row );
		farhen += 10;
		row += 15;
	    }
	}
	
	g.setColor( Color.black);
	g.drawString( "\u00A9 McGalliger Manufacturing", col, row );
 	
    }
}

Look at all of the identical code in your if-else cases. The only part of those code blocks that varies is the setColor() call. The rest is executed for every case, so it doesn't need to be in your if() blocks. Don't repeat more code than you need to.

For your 'cel' calc, be careful about mixing integers with float division. 5 and 9 are ints, so consider how that impacts your formula result.

To change the alignment, you'll have to change where you draw the strings. That may involve some calculations on the string lengths as well. You can check this entry in the Java 2D graphics tutorial about measuring text: http://download.oracle.com/javase/tutorial/2d/text/measuringtext.html

Member Avatar for loserspearl

Ahh, I understand. It's working, but I still don't know how to format the output of celsius, so it doesn't end in repetend, just round.

import java.awt.*;
import java.applet.*;

public class TempConv extends Applet
{   	
    public void paint( Graphics g )
    {
	this.setBackground( Color.lightGray );
	g.setColor( Color.black);
	g.drawString( "Fahrenheit" + "   " + "Clesius", 10, 20 );
	g.setColor( Color.red );
	g.drawLine( 10, 23, 150, 23);
	
	int col = 20;
	int row = 40;
	double farhen = 0;
	double cels;

	for (int i = 0; i <= 25; i++){
	cels = ( 5.0 / 9.0 ) * ( farhen - 32 );
	    if (farhen < 32){
	        g.setColor( Color.blue );
	    }
	    else if (farhen < 212){
		g.setColor( Color.black );
	    }
	    else{
		g.setColor( Color.red );
	    }
	g.drawString( "   " + farhen + "\u2109" + "   " + cels + "\u2103", col, row );
	farhen += 10;
	row += 15;
	}
	
	g.setColor( Color.black);
	g.drawString( "\u00A9 McGalliger Manufacturing", col, row );
    }
}

You can truncate the decimal portion by simply casting the variable to an int

(int)cels

If you want to round, you can use DecimalFormat or the String.format() method.

Member Avatar for loserspearl

Thanks for the help, I got it working. Aligned (mostly) using a crude if statement with more spaces, here is the final code for others.

import java.awt.*;
import java.applet.*;

public class TempConv extends Applet
{   	
    public void paint( Graphics g )
    {
	this.setBackground( Color.lightGray );
	g.setColor( Color.black);
	g.drawString( "Fahrenheit" + "   " + "Clesius", 10, 20 );
	g.setColor( Color.red );
	g.drawLine( 10, 23, 150, 23);
	
	int col = 20;
	int row = 40;
	double farhen = 0;
	double cels;

	for (int i = 0; i <= 25; i++){
	cels = ( 5.0 / 9.0 ) * ( farhen - 32 );
	    if (farhen < 32){
	        g.setColor( Color.blue );
	    }
	    else if (farhen < 212){
		g.setColor( Color.black );
	    }
	    else{
		g.setColor( Color.red );
	    }
	    if (farhen == 0)
		g.drawString( "     " + (int) farhen + "\u2109" + "    " + (int) cels + "\u2103", col , row );
	    else if (farhen < 100 )
		g.drawString( "   " + (int) farhen + "\u2109" + "   " + (int) cels + "\u2103", col, row );
	    else
		g.drawString( " " + (int) farhen + "\u2109" + "   " + (int) cels + "\u2103", col, row );
	    
	farhen += 10;
	row += 15;
	}
	
	g.setColor( Color.black);
	g.drawString( "\u00A9 McGalliger Manufacturing", col, row );
    }
}

Glad you got it working.

For future reference, you can right align drawn strings like this

int fahrCol = 40;
String fahrString = (int) farhen + "\u2109";
int fahrStringWidth = g.getFontMetrics().stringWidth(fahrString);
g.drawString(fahrString, fahrCol-fahrStringWidth, row);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.