19 - Screen Coordinates CS133: Developing Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Screen Coordinates CS133: Developing Programming Principles Lecture 19 Graphics: JPanels, JFrames, drawing primitives, the Board Class (1) (0, 0) x 100 pixels 150 pixels y CS 133 Course Notes Lecture 19, Slide 2 1 2 Example Example import javax.swing.*; import java.awt.*; public class HappyPanel extends JPanel { public void paintComponent(Graphics canvas) { canvas.drawOval(100,50,200,200); canvas.fillOval(155,100,10,20); canvas.fillOval(230,100,10,20); canvas.drawArc(150,160,100,50,180,180); } } CS 133 Course Notes Lecture 19, Slide 3 CS 133 Course Notes Lecture 19, Slide 4 3 4 Swing Library Swing is a special class library for creating graphical user interface (GUI) components, including windows, buttons, text, and mouse support. note the ‘x’ Class JPanel A JPanel is a simple GUI component that provides a surface for drawing, as well as a container to group together other GUI components, such as buttons, labels or even other JPanels public class HappyPanel extends JPanel import javax.swing.*; import java.awt.*; CS 133 Course Notes Lecture 19, Slide 5 CS 133 Course Notes Lecture 19, Slide 6 5 6 Class Graphics The Graphics class provides a simple interface that allows the application to draw onto JPanels and other components. public void paintComponent(Graphics canvas) Graphics Methods drawOval(int x, int y, int width, int height) fillOval(int x, int y, int width, int height) drawArc(int x, int y, int width, int height, int angle, int degrees) CS 133 Course Notes Lecture 19, Slide 8 CS 133 Course Notes Lecture 19, Slide 7 7 8 Getting Panels on the Screen: JFrame import javax.swing.*; import java.awt.*; public class HappyFrame extends JFrame { public HappyFrame() { HappyPanel happyPanel = new HappyPanel(); getContentPane().add(happyPanel); setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } } 133 CS Course Notes Lecture 19, Slide 9 Class JFrame To appear onscreen, every component must be contained in a JFrame or other top-level container class. public class HappyFrame extends JFrame CS 133 Course Notes Lecture 19, Slide 10 9 10 Example Simple Version of Board public class BoardPanel extends JPanel { private int rows, cols; private boolean grid; // etc. } public class TestHappy { public static void main(String args) { HappyFrame happy = new HappyFrame(); } } CS 133 Course Notes Lecture 19, Slide 11 CS 133 Course Notes Lecture 19, Slide 12 11 12 Simple Version of Board X_OFFSET Constructor public BoardPanel(int rows, int cols) { int xSize = 2*X_OFFSET + cols*SIZE; int ySize = 2*Y_OFFSET + rows*SIZE; this.rows = rows; this.cols = cols; emptyGrid(); setPreferredSize( new Dimension(xSize, ySize)); } Y_OFFSET SIZE CS 133 Course Notes Lecture 19, Slide 13 CS 133 Course Notes Lecture 19, Slide 14 13 14 Initializing the grid private void emptyGrid() { grid = new boolean[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { grid[i][j] = false; } } } CS 133 Course Notes Lecture 19, Slide 15 CS 133 Drawing the Board public void paintComponent(Graphic canvas) { paintGrid(canvas); paintPegs(canvas); } Course Notes Lecture 19, Slide 16 15 16 Drawing the Grid private void paintGrid(Graphics canvas) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if ((i+j)%2 == 1) { canvas.setColor(LIGHT); } else { canvas.setColor(DARK); } canvas.fillRect(X_OFFSET + SIZE*j, Y_OFFSET + SIZE*i, SIZE, SIZE); } } } CS 133 Course Notes Lecture 19, Slide 17 Drawing a Peg (x, y) height width CS 133 Course Notes Lecture 19, Slide 18 17 18 Drawing the Pegs private void paintPegs(Graphics canvas) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (grid[i][j]) { int x = X_OFFSET + j*SIZE + SIZE/4; int y = Y_OFFSET + i*SIZE + SIZE/4; int size = SIZE/2; canvas.setColor(Color.YELLOW); canvas.fillOval(x, y, size, size); } } } } CS 133 Course Notes Lecture 19, Slide 19 Adding Pegs public void setPeg(int row, int col, boolean on) { if (row >= 0 && row < rows && cols >= 0 && col < cols) { grid[row][col] = on; } } CS 133 Course Notes Lecture 19, Slide 20 19 20 Board extends JFrame public class Board extends JFrame { private BoardPanel board; public Board (int rows, int cols) { Container content = getContentPane(); board = new BoardPanel(rows, cols); content.add(board); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } public void setPeg(int row, int col, boolean on) { board.setPeg(row, col, on); } CS 133 } Course Notes Lecture 19, Slide 21 Example public class TestBoard { public static void main(String args) { Board board = new Board(12, 12); board.setPeg(1, 1, true); board.setPeg(3, 8, true); } } CS 133 Course Notes Lecture 19, Slide 22 21 22 The Result CS 133 Course Notes Lecture 19, Slide 23 23 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online