1 / 12

CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8 Fall, 2012. Dr Cao’s Assignment 5 i.e. Problem 4 on page 335 and drawing a football field using AWT. Haidong Xue. 5:30pm—8:30pm 11/13/2012 and 11/14/2012 . CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue

sarai
Télécharger la présentation

CSc2310 tutoring session, week 8 Fall, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSc2310 tutoring session, week 8Fall, 2012 • Dr Cao’s Assignment 5 • i.e. Problem 4 on page 335 and drawing a football field using AWT HaidongXue 5:30pm—8:30pm 11/13/2012 and 11/14/2012

  2. CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: • Review Dr Cao’s Assignment 5 • i.e. Problem 4 on page 335 and drawing a football field using AWT 2. Q&A • Answer your questions about java programming No Tutoring session in the thanksgiving week (next week)

  3. Problem 4 on text book page 335 • Print a calendar for a given year and month • Detect illegal month input • Detect illegal year input • Not an int • Negative October 2012 -------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 The difference between this one and the previous print calendar is in the preparation step

  4. Problem 4 on text book page 335 • Previous preparation // Preparation GregorianCalendar date = newGregorianCalendar(); date.set(Calendar.DATE, 1); int month = date.get(Calendar.MONTH); // Determine the current month int year = date.get(Calendar.YEAR); // Determine the current year intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; // Determine the day of the week intcalendarWidth = 20; // the width of the calendar • New preparation // Preparation GregorianCalendar date = newGregorianCalendar(); intmonth = readMonth(); // get a month int year = readYear(); // get a year date.set(Calendar.MONTH, month); // set to the month date.set(Calendar.YEAR, year); // set to the year date.set(Calendar.DATE, 1); // Adjust to first day of month intdayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; intcalendarWidth = 20; Create 2 methods to get the inputs: intreadMonth() IntreadYear()

  5. Problem 4 on text book page 335 privatestaticintreadMonth() { finalString[] MONTH_NAMES = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; Scanner s = new Scanner(System.in); while(true) { System.out.print("Enter a month name: "); String monthName = s.nextLine(); for(inti = 0; i < MONTH_NAMES.length; i++) if(monthName.equalsIgnoreCase(MONTH_NAMES[i])) returni; System.out.println("Illegal month name; try again."); } }

  6. Problem 4 on text book page 335 privatestaticintreadYear() { Scanner s = new Scanner(System.in); while(true) { System.out.print("Enter a year: "); String userInput = s.nextLine(); try { intyear = Integer.parseInt(userInput); if(year >= 0) returnyear; System.out.println("Year cannot be negative; " + "try again"); } catch(NumberFormatException e) { System.out.println("Not an integer; try again."); } } } Using exceptions (try-catch) to detect if it is an integer

  7. Draw a football field A sequence of drawing actions on Graphics All those methods can be found at: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

  8. publicvoiddrawField(){ DrawableFramedf = newDrawableFrame(TeamName); df.setVisible(true); df.setSize(700, 300); Graphics g = df.getGraphicsContext(); …. Create a DrawableFrame and get the graphics object

  9. intpaddingX = 10; intpaddingY = 40; intfieldWidth = 525; intfieldHeight = 280; intleftFieldX = paddingX*2; intrightFieldX = fieldWidth; inttopFieldY = paddingY+paddingX; intbottomFieldY = 250; //field g.setColor(Color.green); g.fillRect(paddingX, paddingX, fieldWidth, fieldHeight); g.setColor(Color.white); //outline of field g.drawRect(leftFieldX, topFieldY, fieldWidth-(paddingX*2), fieldHeight-(paddingY*2)); //yardlines for(inti = 1;i < 12;i++) { g.drawLine((leftFieldX+40)+(35*i) , topFieldY, (leftFieldX+40)+(35*i), bottomFieldY); } //yardline numbers Font f = new Font("SansSerif", Font.BOLD, 14); g.setFont(f); g.drawString("Goal", leftFieldX+60, topFieldY-3); g.drawString("Goal", leftFieldX+60, bottomFieldY+14); g.drawString("Goal", 430, topFieldY-3); g.drawString("Goal", 430, bottomFieldY+14); for(inti=1; i<6;i++) { if(i<5) { g.drawString(i+ "0", 87+(35*i), topFieldY-3); g.drawString(i+ "0", 87+(35*i), bottomFieldY+14); } else{ for(int k=1;k<6; k++) { g.drawString(k + "0", 439-(35*k), topFieldY-3); g.drawString(k + "0", 439-(35*k), bottomFieldY+14); } } } Draw field, yard lines and numbers

  10. //endzone f = new Font("SansSerif", Font.BOLD, 14); g.setFont(f); FontMetricsfm = g.getFontMetrics(f); intstrlen = fm.stringWidth("ENDZONE"); g.drawString("ENDZONE", leftFieldX+4, topFieldY+fm.getAscent()); g.drawString("ENDZONE", leftFieldX+4, bottomFieldY-4); g.drawString("ENDZONE", rightFieldX-strlen-4, topFieldY+fm.getAscent()); g.drawString("ENDZONE", rightFieldX-strlen-4, bottomFieldY-4); //Number of players g.setColor(Color.BLACK); int buffer = 5; int[] xOuter = {560,590,635,665,665,635,590,560}; int[] yOuter = {30,0,0,30,75,105,105,75}; int[] xInner = {560+buffer,590,635,665-buffer,665-buffer,635,590,560+buffer}; int[] yInner = {30,buffer,buffer,30,75,105-buffer,105-buffer,75}; g.drawPolygon(xOuter, yOuter, xOuter.length); g.setColor(Color.RED); g.fillPolygon(xInner, yInner, xOuter.length); //number of Players g.setColor(Color.WHITE); f = new Font("SansSerif", Font.BOLD, 80); g.setFont(f); String strNumPlayers = nTotalNumPlayers+""; fm = g.getFontMetrics(f); strlen = fm.stringWidth(strNumPlayers); intstrHigh = fm.getAscent(); g.drawString(strNumPlayers, 612-strlen/2, 52+(strHigh/3)); //player names g.setColor(Color.BLACK); f = new Font("SansSerif", Font.BOLD, 14); g.setFont(f); fm = g.getFontMetrics(f); strHigh = fm.getAscent(); for (inti=0;i<sPlayerArray.length;i++) { g.drawString(sPlayerArray[i], 560, 135+(3+strHigh*i)); } Draw end zone and player number and names

  11. Repaint! …. df.repaint(); }

  12. Please let me know your questions. I will be here till 8:30pm

More Related