Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and.

Similar presentations


Presentation on theme: "CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and."— Presentation transcript:

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

2 CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1.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 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 Print a calendar for a given year and month Detect illegal month input Detect illegal year input  Not an int  Negative The difference between this one and the previous print calendar is in the preparation step

4 Previous preparation Problem 4 on text book page 335 // Preparation GregorianCalendar date = new GregorianCalendar(); 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 int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; // Determine the day of the week int calendarWidth = 20; // the width of the calendar New preparation // Preparation GregorianCalendar date = new GregorianCalendar(); int month = 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 int dayOfWeek = date.get(Calendar.DAY_OF_WEEK) - 1; int calendarWidth = 20; Create 2 methods to get the inputs: int readMonth() Int readYear()

5 Problem 4 on text book page 335 private static int readMonth() { final String[] 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 (int i = 0; i < MONTH_NAMES.length; i++) if (monthName.equalsIgnoreCase(MONTH_NAMES[i])) return i; System.out.println("Illegal month name; try again."); }

6 Problem 4 on text book page 335 private static int readYear() { Scanner s = new Scanner(System.in); while (true) { System.out.print("Enter a year: "); String userInput = s.nextLine(); try { int year = Integer.parseInt(userInput); if (year >= 0) return year; 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 http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

8 public void drawField(){ DrawableFrame df = new DrawableFrame(TeamName); df.setVisible(true); df.setSize (700, 300); Graphics g = df.getGraphicsContext(); …. Create a DrawableFrame and get the graphics object

9 int paddingX = 10; int paddingY = 40; int fieldWidth = 525; int fieldHeight = 280; int leftFieldX = paddingX*2; int rightFieldX = fieldWidth; int topFieldY = paddingY+paddingX; int bottomFieldY = 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(int i = 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(int i=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); FontMetrics fm = g.getFontMetrics(f); int strlen = 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); int strHigh = 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 (int i=0;i<sPlayerArray.length;i++) { g.drawString(sPlayerArray[i], 560, 135+(3+strHigh*i)); } Draw end zone and player number and names

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

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


Download ppt "CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and."

Similar presentations


Ads by Google