tiled Map Case Study: Rendering with JPanel © Allan C. Milne v14.12.26.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Web Design & Development Lecture 19. Java Graphics 2.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
© A+ Computer Science - Chicken yeller = new Chicken();
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Creating Your Own Widgets CompSci 230 S Software Construction.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Java Dynamic Graphics.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
The Strategy Design Pattern © Allan C. Milne v
Object-Oriented Software Engineering Using Threads and simple Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Creating Your Own Widgets
Java Graphics.
Java Graphics CS 2511.
Computer Science 209 Graphics and GUIs.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Java Programming: Guided Learning with Early Objects
Scaling of Eclipse on High Density Displays
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
A+ Computer Science METHODS.
A+ Computer Science METHODS.
Introduction to Graphical Interfaces in Java: AWT and Swing
Presentation transcript:

tiled Map Case Study: Rendering with JPanel © Allan C. Milne v

Agenda.  Accessing a JPanel.  Map & tile representation.  Drawing the map.  Using the renderer.

Where are we now? we use a strategy pattern to include rendering behaviour; the MapRenderer interface; composition of tiledMap with a concrete MapRenderer object; the render() method mechanism. We have a concrete rendering class; ConsoleRenderer.

Where do we want to be? Add more concrete rendering implementations; –different ways of displaying on the console; –using different display technologies. In particular, rendering in a windows user interface; –for example, using a JPanel to draw some representation of the map.

A problem with JPanel. There is only one console and any code can access it; –thus any code writing to the console to render a map simply uses System.out … but a client application’s user interface may have many JPanel objects making it up; –which JPanel object will the map renderering code draw on?

Our approach. Make the concrete rendering class –implement the MapRenderer interface; and –inherit from JPanel; This means that our class can both –act as a concrete rendering class; and –be used as a JPanel within a client’s user interface.

class JPanelRenderer extends JPanel implements MapRenderer public void render (TiledMap aMap) { … … … } // end render protected void paintComponent (Graphics g) { … … … } // end paintcomponent method. … … … } // end JPanelRenderer class. The concrete renderer.

paintComponent(Graphics g) Renders a map by drawing a representation on the JPanel through its supplied Graphics object. Called by the panel’s redraw() method when this is called by a client. We require to have –a map object to draw; and –a representation to use for the drawing.

class JPanelRenderer extends JPanel implements MapRenderer { private TiledMap public void render (TiledMap aMap) { mMap = aMap; redraw(); } // end render protected void paintComponent (Graphics g) { … … … } // end paintcomponent method. … … … } // end JPanelRenderer class. So we have …

Constructor method(s). Since our rendering class is also a JPanel any constructor methods must reflect the requirements of a JPanel. In particular, we can supply the size of the panel in pixels. We should also initialize the mMap field.

public JPanelRenderer (int aWidth, int aHeight) { //TODO validate the arguments are >0 and set to 0 if not. setPreferredSize (new Dimension (aWidth, aHeight)); mMap = null; } // end constructor method. A constructor …

Map and tile representation. Consider the panel to be made up of a grid of squares: –size of grid = size of map; –Each rid square is drawn in a colour representing the terrain tile type of the corresponding map tile; –each grid square will have a size (resolution) in pixels.

Panel size (in pixels) and therefore shape will be determined by the client and Java layout manager when the JPanel object is created and added to the user interface. For a map to be rendered we must have –panel width >= map width; and –panel height >= map height. If dimension sizes are equal then the grid squares will be only 1 pixel in size. the shape of the grid is defined by the shape of the map and may not fill the panel if it has a different shape. Map and grid size.

Map tile colours. java.awt.Color can represent colours. The colour to draw a square on the grid is determined by the terrain tile type of the corresponding tile on the map. Use a switch statement; or adopt a similar approach to that used for determining the character to display for a tile in the console renderer.

TerrainType enumeration. Add a private java.awt.Colour attribute. Amend the constructor to initialize this attribute with a supplied colour. Amend the value definitions to include a specific colour parameter. Expose the asColour() method to return the colour attribute for an value.

Drawing a map. paintComponent (Graphics g) –super.paintComponent(g); –for each map tile: use g.setColor(…) to set colour; to draw the square use g.fillRect ( –x-coord of top left corner, –y-coord of top left corner, –x-size, –y-size )

We know … the size of the panel; –getWidth() and getHeight() panel methods. the map; –mMap field. the size of the map; –height() and width() methods of mMap. the colours to use for tiles; –asColour() method of TerrainType.

Outstanding problems. The resolution (pixels per grid square) must be determined. the coordinate system of a JPanel is different from that of the map.

Calculating resolution. Is panel big enough to represent the map even at 1 pixel per tile? –If not, then draw a message on the panel and return. Resolution can only be an integer. Is calculated as the smallest resolution of the width and height dimensions; –Maintains the square aspect ratio of the grid elements; –May result in unused areas of the panel if it is a different shape than the map.

Coordinate systems. In the map, –(0,0) is at the bottom left; –X coordinates go from left to right; –Y coordinates go from bottom to top. On the JPanel, –(0,0) is at the top left; –X coordinates go from left to right; –Y coordinates go from top to bottom (but still positive). We must apply a transform from map to JPanel coordinates.

Coordinate transformation. Need to transform tile position (x m,y m ) on the map to (x p,y p ) on the JPanel. Requires us to consider –the resolution of the panel grid squares; –the different origin positions; –the different Y coordinate directions. x p = x m * resolution y p = (map-height * resolution) – ( (y m + 1) * resolution)

Client usage. public static void main (…) { final TiledMap map = new TiledMap (…); … … … SwingUtilities.invokeLater(new Runnable() public void run() { createWindowAndRender (map); } }); … … … } // end main method.

… and the renderer … private static final void createWindowAndRender (TiledMap aMap) { JPanelRenderer panelRenderer = new JPanelRenderer(…); final JFrame frame = new JFrame(“ title "); … … … frame.add (panelRenderer, BorderLayout.CENTER); … … … frame.pack(); aMap.setRenderer (panelRenderer); aMap.render(); } // end createWindowAndRender method.