''' TranscentrationRunGameV01.py GUI skeleton for the program that will run a game of Transcentration. Version 01: Simply reads in all the images and displays them in a reasonable array. Thus it illustrates the basics of the GUI and how to deal with images in a Jython application. It also detects mouse clicks, using a class called TranscentrationMouseAdapter, but it doesn't do anything more than report the clicks in the console. This version DOES NOT organize the images according to the game specification. It DOES NOT handle the playing. ''' import javax.swing as swing import javax.imageio as imageio from java.awt.event import MouseAdapter import math # The PATH should lead from the default PixelMath user directory # to the folder where the Transcentration files and folders are. PATH = '../Transcentration/' # The following class specializes a Swing JPanel to represent # the game board. class Board(swing.JPanel): # Constructor for the class. def __init__(self, title): self.originalImages = [] self.transformedImages = [] # The following 3 members get created by other methods: #self.nr = 0 #self.nc = 0 #self.num_originals = 0 # The next call determines how big to paint the images. self.setCardSize(200,170) # Next, associate a mouse handler with the board. self.addMouseListener(Board.BoardMouseAdapter()) # Set the width and height for the "cards" of the game. def setCardSize(self, w, h): self.w = w self.h = h # Determine with board's overall width and height. def getDimensions(self): return (int(self.w*self.nc), int(self.h*self.nr)) # Here we override the JPanel paintComponent method so that # we control how the board is rendered. def paintComponent(self, g): i = 0; j = 0 k = 0 for image in self.originalImages + self.transformedImages: g.drawImage(image, j*self.w, i*self.h, self.w, self.h, self) k += 1 j += 1 if j== self.nc: i += 1; j = 0 # To load one image from a file, we use the Java ImageIO class. def loadImage(self, filename, whichList): try: f = open(filename,'r') image = imageio.ImageIO.read(f) whichList.append(image) #w = image.getWidth(b) #h = image.getHeight(b) # Note: we don't currently make use of the actual # width and height of each image. Instead we force # all the widths to be the same when we paint the images. # A more sophisticated way to show the images might involve # scaling and padding to maintain the original aspect ratios. except: print "Could not read an image named: "+filename print " There could be a path problem, "+\ "or the image may need to be created." # Read in all the images we need for a game of Transcentration. def loadAllImages(self): # Read in two files, and figure out the names of # all the images needed. # First read in a file containing a list of image originals. global PATH image_list_file =\ open(PATH+'ORIGINALS/active-images-list.txt', 'r') image_files = image_list_file.readlines() formula_list_file =\ open(PATH+'TRANSFORMED/active-formulas-list.txt', 'r') formulas = formula_list_file.readlines() i = 0 # index of current line in the formulas file. k = 0 # count of the number of originals for image_line in image_files: image_fn = image_line[:-1] # remove trailing newline character formula_line = formulas[i][:-1] # get one line and remove newline char. i += 1 # If this formula_line starts with '#' then # it is a comment and should be skipped. comment = True if formula_line[0]!='#': comment = False while comment: formula_line = formulas[i][:-1] i += 1 comment = True if formula_line[0]!='#': comment = False print 'Processing formula line: '+formula_line pos = formula_line.find(':') # Determine where the formula name ends. formula_name = formula_line[:pos] # Pull out the formula name. formula = formula_line[pos+1:] # Pull out the formula itself. new_name = image_fn[:-4]+'-'+formula_name+'.png' # Synthesize the file name. # Read both the original and the transformed version. self.loadImage(PATH+'ORIGINALS/'+image_fn, self.originalImages) k += 1 self.loadImage(PATH+'TRANSFORMED/'+new_name, self.transformedImages) self.num_originals = k # Save number of originals total = 2*k # otal number of images read in. if total > 16: self.nc = 6 # Choose number of columns for the board. else: self.nc = 4 self.nr = math.ceil((0.0+total)/self.nc) # Compute number of rows. # A convenience method that combines the functionality of getDimensions, # the Swing method setSize, and returning the dimenions to the caller. def setAndReturnDimensions(self): dims = self.getDimensions() self.setSize(dims[0], dims[1]) return dims # Here's a skeleton of a handler for mouse clicks. # It is an inner class of Board, with access to its members. class BoardMouseAdapter(MouseAdapter): def mouseClicked(self, event): print 'The mouse was clicked...' + str(event) # Here's most of the top-level executable code for this program: windowTitle = "The Game of Transcentration -- Version 0.1" j = swing.JFrame(windowTitle) # Create a window. b = Board(False) # Create the board. b.loadAllImages() # Load all the images. # Set the board size so all images show. boardDimensions = b.setAndReturnDimensions() j.getContentPane().add(b) # Add the board to the window. # Set the window size so the whole board shows. j.setSize(boardDimensions[0],boardDimensions[1]+20) j.show() # Show the window.