If anyone sees this by Midnight EST (2 hours) of Sun 2/19

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tasselfoot
    Retired BOSS
    FFR Simfile Author
    • Jul 2003
    • 25185

    #1

    If anyone sees this by Midnight EST (2 hours) of Sun 2/19

    I could use some java help working with Applets and the Graphics class. Assignment is due by Midnight.


    If you can help, send me an IM... Tasselfoot.


    Thanks.

    -Tass


    PS... the code:

    Code:
    import javax.swing.JOptionPane;
    import java.applet.*;
    import java.awt.*;
    
    public class BarNumber extends Applet
    /*{
    	public static void main(String[] boob)    //this won't run in JCreator w/o the main, but regardless I can't get the
    	{										  //program to call the paint() method.  :(
    		BarNum fifteen = new BarNum();
    		fifteen.init();
    	}
    }
    
    
    class BarNum extends Applet  */
    {
    	String num;//instance variable
    	int sum = 0;
    	Graphics g;
    	
    	
    	public void init()
    	{
    		num = JOptionPane.showInputDialog("Type a 15-digit number");
    		checkLength(num);
    	}
    	
    	public void checkLength(String s)//added this extra, to verify the length of the inputted number
    	{
    		int len = s.length();
    		if(len != 15)
    		{
    			System.out.println("This number is not 15 digits");
    		}
    	}
    	
    	public void paint(Graphics g)//no idea what to write here
    	{
    		processAllLines(g);
    		line(g);
    		processSum(g, sum);
    		//System.out.println("I got to paint");
    	}
    	
    	public void drawBars(Graphics g, int xStart, int yStart, boolean upperLeft, boolean top, boolean upperRight, boolean lowerRight, boolean bottom, boolean lowerLeft, boolean middle)
    	{
    		g.drawLine(xStart, yStart, 25, 0);//upperLeft
    		g.drawLine(xStart + 25, yStart, 0, 25);//top
    		g.drawLine(xStart + 25, yStart + 25, -25, 0);//upperRight
    		g.drawLine(xStart, yStart + 25, -25, 0);//lowerRight
    		g.drawLine(xStart - 25, yStart + 25, 0, -25);//bottom
    		g.drawLine(xStart - 25, yStart, 25, 0);//lowerLeft
    		g.drawLine(xStart, yStart, 0, 25);//middle
    		//System.out.println("I got to drawBars");
    	}
    	
    	public void processDigit(Graphics g, int xStart, int yStart, int digit)
    	{
    		switch(digit)//am I missing more to this method?
    		{
    			case 0: drawBars(g, xStart, yStart, true, true, true, true, true, true, false); break;
    			case 1: drawBars(g, xStart, yStart, false, false, true, true, false, false, false); break;
    			case 2: drawBars(g, xStart, yStart, false, true, true, false, true, true, true); break;
    			case 3: drawBars(g, xStart, yStart, false, true, true, true, true, false, true); break;
    			case 4: drawBars(g, xStart, yStart, true, false, true, true, false, false, true); break;
    			case 5: drawBars(g, xStart, yStart, true, true, false, true, true, false, true); break;
    			case 6: drawBars(g, xStart, yStart, true, false, false, true, true, true, true); break;
    			case 7: drawBars(g, xStart, yStart, false, true, true, true, false, false, false); break;
    			case 8: drawBars(g, xStart, yStart, true, true, true, true, true, true, true); break;
    			case 9: drawBars(g, xStart, yStart, true, true, true, true, false, false, true); break;
    		}
    		//System.out.println("I got to processDigit");
    	}
    		
    	public void processLine(Graphics g, int yStart, String subString)
    	{
    		for&#40;int j = 0; j < 5; j++&#41;//i THINK this method is correct...
    		&#123;
    			char a = subString.charAt&#40;j&#41;;
    			int adigit = a - 48;
    			int xStart = 30 + &#40;30*j&#41;;
    			processDigit&#40;g, xStart, yStart, adigit&#41;;
    		&#125;
    		//System.out.println&#40;"I got to processLine"&#41;;
    	&#125;
    	
    	public void processAllLines&#40;Graphics g&#41; // think this is right too
    	&#123;
    		String t = "";
    		for&#40;int j = 0; j < 3; j++&#41;
    		&#123;
    			t = num.substring&#40;5*j, 5*j + 5&#41;;
    			int yStart = 30 + &#40;60*j&#41;;
    			sum = sum + Integer.parseInt&#40;t&#41;;
    			processLine&#40;g, yStart, t&#41;;
    		&#125;
    		//System.out.println&#40;"I got processAllLines"&#41;;
    	&#125;
    	
    	public void line&#40;Graphics g&#41;
    	&#123;
    		int xStart = 0;
    		int yStart = 180;
    		g.drawLine&#40;xStart, yStart, 0, 180&#41;;
    		//System.out.println&#40;"I got to line"&#41;;
    		//need to code something to draw a straight line across from &#40;0,180&#41; to &#40;180?,180&#41;
    	&#125;
    	
    	public void processSum&#40;Graphics g, int total&#41;
    	&#123;
    		int var = 0;
    		if&#40;&#40;total / 100000&#41; >= 1&#41;// if/else depending on whether the sum is 6 or 5 digits
    		&#123;
    			for&#40;int j = 5; j >= 0; j--&#41;
    			&#123;
    				var = &#40;total / &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				total = &#40;total % &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				int yStart = 210;
    				int xStart = 30*&#40;5-j&#41;;
    				processDigit&#40;g, xStart, yStart, var&#41;;
    			&#125;
    		&#125;
    		else
    		&#123;
    			for&#40;int j = 4; j >= 0; j--&#41;
    			&#123;
    				var = &#40;total / &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				total = &#40;total % &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				int yStart = 210;
    				int xStart = 30 + &#40;30*&#40;4-j&#41;&#41;;
    				processDigit&#40;g, xStart, yStart, var&#41;;
    			&#125;
    		&#125;
    		//System.out.println&#40;"I got to processSum"&#41;;
    	&#125;
    &#125;
    code updated...
    RIP
  • Tasselfoot
    Retired BOSS
    FFR Simfile Author
    • Jul 2003
    • 25185

    #2
    RE: If anyone sees this by Midnight EST (2 hours) of Sun 2/1

    FINISHED! It works, if anyone wants to play with it... need to run it as an applet:


    Code:
    import javax.swing.JOptionPane;
    import java.applet.*;
    import java.awt.*;
    
    public class BarNumber extends Applet
    &#123;
    	
    	String num;//instance variable
    	int sum = 0;
    	
    	
    	public void init&#40;&#41;
    	&#123;
    		num = JOptionPane.showInputDialog&#40;"Type a 15-digit number"&#41;;
    	&#125;
    	
    	public void paint&#40;Graphics g&#41;//no idea what to write here
    	&#123;
    		processAllLines&#40;g&#41;;
    		line&#40;g&#41;;
    		processSum&#40;g, sum&#41;;
    	&#125;
    	
    	public void drawBars&#40;Graphics g, int xStart, int yStart, boolean upperLeft, boolean top, boolean upperRight, boolean lowerRight, boolean bottom, boolean lowerLeft, boolean middle&#41;
    	&#123;
    		if&#40;upperLeft == true&#41;
    		&#123;
    			g.drawLine&#40;xStart, yStart, xStart, yStart + 5&#41;;//upperLeft
    		&#125;
    		if&#40;top == true&#41;
    		&#123;
    			g.drawLine&#40;xStart, yStart + 5, xStart + 5, yStart + 5&#41;;//top
    		&#125;
    		if&#40;upperRight == true&#41;
    		&#123;
    			g.drawLine&#40;xStart + 5, yStart + 5, xStart + 5, yStart&#41;;//upperRight
    		&#125;
    		if&#40;lowerRight == true&#41;
    		&#123;
    			g.drawLine&#40;xStart + 5, yStart, xStart + 5, yStart - 5&#41;;//lowerRight
    		&#125;
    		if&#40;bottom == true&#41;
    		&#123;
    			g.drawLine&#40;xStart + 5, yStart - 5, xStart, yStart - 5&#41;;//bottom
    		&#125;
    		if&#40;lowerLeft == true&#41;
    		&#123;
    			g.drawLine&#40;xStart, yStart - 5, xStart, yStart&#41;;//lowerLeft
    		&#125;
    		if&#40;middle == true&#41;
    		&#123;
    			g.drawLine&#40;xStart, yStart, xStart + 5, yStart&#41;;//middle
    		&#125;
    	&#125;
    	
    	public void processDigit&#40;Graphics g, int xStart, int yStart, int digit&#41;
    	&#123;
    		//System.out.println&#40;digit&#41;;
    		switch&#40;digit&#41;//am I missing more to this method?
    		&#123;
    			case 0&#58; drawBars&#40;g, xStart, yStart, true, true, true, true, true, true, false&#41;; break;
    			case 1&#58; drawBars&#40;g, xStart, yStart, false, false, true, true, false, false, false&#41;; break;
    			case 2&#58; drawBars&#40;g, xStart, yStart, true, true, false, true, true, false, true&#41;; break;
    			case 3&#58; drawBars&#40;g, xStart, yStart, false, true, true, true, true, false, true&#41;; break;
    			case 4&#58; drawBars&#40;g, xStart, yStart, false, false, true, true, false, true, true&#41;; break;
    			case 5&#58; drawBars&#40;g, xStart, yStart, false, true, true, false, true, true, true&#41;; break;
    			case 6&#58; drawBars&#40;g, xStart, yStart, true, true, true, false, false, true, true&#41;; break;
    			case 7&#58; drawBars&#40;g, xStart, yStart, false, false, true, true, true, false, false&#41;; break;
    			case 8&#58; drawBars&#40;g, xStart, yStart, true, true, true, true, true, true, true&#41;; break;
    			case 9&#58; drawBars&#40;g, xStart, yStart, false, false, true, true, true, true, true&#41;; break;
    		&#125;
    	&#125;
    		
    	public void processLine&#40;Graphics g, int yStart, String subString&#41;
    	&#123;
    		for&#40;int j = 0; j < 5; j++&#41;//i THINK this method is correct...
    		&#123;
    			char a = subString.charAt&#40;j&#41;;
    			int adigit = a - 48;
    			int xStart = 30 + &#40;30*j&#41;;
    			processDigit&#40;g, xStart, yStart, adigit&#41;;
    		&#125;
    	&#125;
    	
    	public void processAllLines&#40;Graphics g&#41; // think this is right too
    	&#123;
    		String t = "";
    		for&#40;int j = 0; j < 3; j++&#41;
    		&#123;
    			//System.out.println&#40;num&#41;;
    			t = num.substring&#40;5*j, 5*j + 5&#41;;
    			int yStart = 30 + &#40;60*j&#41;;
    			sum = sum + Integer.parseInt&#40;t&#41;;
    			//System.out.println&#40;sum&#41;;
    			processLine&#40;g, yStart, t&#41;;
    		&#125;
    	&#125;
    	
    	public void line&#40;Graphics g&#41;
    	&#123;
    		int xStart = 0;
    		int yStart = 180;
    		g.drawLine&#40;xStart, yStart, xStart + 180, yStart&#41;;
    		//need to code something to draw a straight line across from &#40;0,180&#41; to &#40;180?,180&#41;
    	&#125;
    	
    	public void processSum&#40;Graphics g, int total&#41;
    	&#123;
    		int var = 0;
    		if&#40;&#40;total / 100000&#41; >= 1&#41;// if/else depending on whether the sum is 6 or 5 digits
    		&#123;
    			for&#40;int j = 5; j >= 0; j--&#41;
    			&#123;
    				var = &#40;total / &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				total = &#40;total % &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				int yStart = 210;
    				int xStart = 30*&#40;5-j&#41;;
    				//System.out.println&#40;var&#41;;
    				processDigit&#40;g, xStart, yStart, var&#41;;
    			&#125;
    		&#125;
    		else
    		&#123;
    			for&#40;int j = 4; j >= 0; j--&#41;
    			&#123;
    				var = &#40;total / &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				total = &#40;total % &#40;&#40;int&#41;Math.pow&#40;10,j&#41;&#41;&#41;;
    				int yStart = 210;
    				int xStart = 30 + &#40;30*&#40;4-j&#41;&#41;;
    				processDigit&#40;g, xStart, yStart, var&#41;;
    			&#125;
    		&#125;
    	&#125;
    &#125;
    RIP

    Comment

    Working...