A little help with a Java applet pls.

Posted:
in Genius Bar edited January 2014
Ok, I'm taking a Java class(not by choice mind you) and one of the assignments was to create a thermometer applet that converts degrees celcius using a slider to adjust the temperature to degrees fahrenheit. I accomplished that, but the problems I'm having are associated with slider position and the lack of a grabable slider (the blue bar doesn't show up on my Mac but the arrows work), and the slider length. Here' my code:



Code:




import java.awt.*;

import java.awt.event.*;

import java.applet.*;



public class Lab4 extends Applet implements AdjustmentListener

{

private Scrollbar slider;

private int celciusValue=50;



public void init()

{

slider = new Scrollbar(Scrollbar.VERTICAL, celciusValue, 1, 0, 101);

add(slider);

slider.addAdjustmentListener(this);

}



public void paint (Graphics g)

{

int lowTemp=-20,

highTemp=120,

xPos=30,

yPos=30,

width=30,

height=150;



thermometer(g,

100-celciusValue,

lowTemp,

highTemp,

xPos,

yPos,

width,

height);



g.setColor(Color.white);

g.drawString(""+((float)(100-celciusValue)*9/5+32), 30,205);

g.setColor(Color.black);

g.drawString(""+(100-celciusValue), 80,205);

}



public void adjustmentValueChanged(AdjustmentEvent e)

{

celciusValue = slider.getValue();

repaint();

}



private void thermometer(Graphics g,

int temperature,

int lowTemp,

int highTemp,

int xPos,

int yPos,

int width,

int height)

{

int tempBand=highTemp-lowTemp;

int numTicMarks, ticSpaces, offset;

float pixPerDegree;



numTicMarks = (tempBand-tempBand%10)/10;

ticSpaces = (height-height%numTicMarks)/numTicMarks;

pixPerDegree = (float) ticSpaces/10;

offset = (int)((highTemp-temperature)*pixPerDegree);



//The following are used to define the black thermometer outline

g.drawLine(xPos-1,

yPos,

xPos-1,

yPos+height+width/2);

g.drawLine(xPos+width,

yPos,

xPos+width,

yPos+height+width/2);

g.drawOval(xPos-width/4-1,

yPos+height-1,

3*width/2+2,

3*width/2+2);

g.drawArc((xPos)-1,

yPos-width/2,

width+1,

width,

0,180);



//The following are used to fill the thermometer with a red 'liquid'.

g.setColor(Color.red);

g.fillOval(xPos-width/4,

yPos+height,

3*width/2,

3*width/2);

g.fillRect(xPos,

yPos+offset,

width,

(height-offset)+width/2);

g.setColor(Color.black);



//Will draw the major and minor tic-marks and label from high to low

//every ten degrees for the major and five degrees for the minor.

//Every twenty degrees the major tic will be labeled.

for(int i=0; i<=numTicMarks; i++)

{



if (i%2 == 0)

{

g.drawString(""+(highTemp-i*10),

xPos+width+5,

yPos+ i*ticSpaces+6);

g.drawLine(xPos+width/2,

yPos+ i*ticSpaces,

xPos+width,

yPos+ i*ticSpaces);

}

else

{

g.drawLine(xPos+width/2,

yPos+ i*ticSpaces,

xPos+(3*width/4),

yPos+ i*ticSpaces);}

}

}

}









I know the event method is outdated but we were told to use it?!?



So, how do I lock the position of the slider and stretch the slider out so the blue grabber works? Any help would be greatly appreciated.



Thank you.
Sign In or Register to comment.