Ejemplo n.º 1
0
def rotate(surface, angle):
    """
    Return Surface rotated by the given angle.
    """
    if not angle:
        return surface.copy()
    theta = angle * _deg_rad
    width_i = surface.getWidth()
    height_i = surface.getHeight()
    cos_theta = _fabs(_cos(theta))
    sin_theta = _fabs(_sin(theta))
    width_f = int((width_i * cos_theta) + (height_i * sin_theta))
    height_f = int((width_i * sin_theta) + (height_i * cos_theta))
    surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB)
    at = AffineTransform()
    at.translate(width_f / 2.0, height_f / 2.0)
    at.rotate(-theta)
    g2d = surf.createGraphics()
    ot = g2d.getTransform()
    g2d.setTransform(at)
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                         RenderingHints.VALUE_INTERPOLATION_BILINEAR)
    g2d.drawImage(surface, -width_i // 2, -height_i // 2, None)
    g2d.setTransform(ot)
    g2d.dispose()
    return surf
Ejemplo n.º 2
0
 def rotozoom(self, surface, angle, size):
     """
     Return Surface rotated and resized by the given angle and size.
     """
     if not angle:
         width = int(surface.getWidth()*size)
         height = int(surface.getHeight()*size)
         return self.scale(surface, (width, height))
     theta = angle*self.deg_rad
     width_i = int(surface.getWidth()*size)
     height_i = int(surface.getHeight()*size)
     cos_theta = _fabs( _cos(theta) )
     sin_theta = _fabs( _sin(theta) )
     width_f = int( _ceil((width_i*cos_theta)+(height_i*sin_theta)) )
     if width_f % 2:
         width_f += 1
     height_f = int( _ceil((width_i*sin_theta)+(height_i*cos_theta)) )
     if height_f % 2:
         height_f += 1
     surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB)
     at = AffineTransform()
     at.translate(width_f/2, height_f/2)
     at.rotate(-theta)
     g2d = surf.createGraphics()
     ot = g2d.getTransform()
     g2d.setTransform(at)
     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
     g2d.drawImage(surface, -width_i//2, -height_i//2, width_i, height_i, None)
     g2d.setTransform(ot)
     g2d.dispose()
     return surf
Ejemplo n.º 3
0
Archivo: font.py Proyecto: jggatc/pyj2d
 def render(self, text, antialias, color, background=None):
     """
     Render text onto surface.
     Arguments:
     text to render (string)
     antialias of text (bool)
     color of text (R,G,B)
     background color (R,G,B)
     """
     w,h = self.size(text)
     surf = Surface((w,h), BufferedImage.TYPE_INT_ARGB)
     g2d = surf.createGraphics()
     if background:
         g2d.setColor(Color(background))
         g2d.fillRect(0,0,w,h)
     g2d.setFont(self.font)
     if antialias:
         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
     g2d.setColor(Color(color))
     y = (h//2)-((self.fontMetrics.getAscent()+self.fontMetrics.getDescent())//2)+self.fontMetrics.getAscent()
     if not self.underline:
         g2d.drawString(text,0,y)
     else:
         try:
             text = AttributedString(text)
             text.addAttribute(TextAttribute.FONT,self.font)
             text.addAttribute(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON)
             g2d.drawString(text.getIterator(),0,y)
         except IllegalArgumentException:
             pass
     g2d.dispose()
     return surf
Ejemplo n.º 4
0
 def rotate(self, surface, angle):
     """
     Return Surface rotated by the given angle.
     """
     if not angle:
         return surface.copy()
     theta = angle*self.deg_rad
     width_i = surface.getWidth()
     height_i = surface.getHeight()
     cos_theta = _fabs( _cos(theta) )
     sin_theta = _fabs( _sin(theta) )
     width_f = int( (width_i*cos_theta)+(height_i*sin_theta) )
     height_f = int( (width_i*sin_theta)+(height_i*cos_theta) )
     surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB)
     at = AffineTransform()
     at.translate(width_f/2, height_f/2)
     at.rotate(-theta)
     g2d = surf.createGraphics()
     ot = g2d.getTransform()
     g2d.setTransform(at)
     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
     g2d.drawImage(surface, -width_i//2, -height_i//2, None)
     g2d.setTransform(ot)
     g2d.dispose()
     return surf
Ejemplo n.º 5
0
def blit_array(surface, array):
    """
    Generates image pixels from a JNumeric array.
    Arguments include destination Surface and array of integer colors.
    JNumeric required as specified in numeric module.
    """
    if not _initialized:
        _init()
    if len(array.shape) == 2:
        data = numeric.transpose(array, (1,0))
        data = numeric.ravel(data)
    else:
        data = array[:,:,0]*0x10000 | array[:,:,1]*0x100 | array[:,:,2]
        data = numeric.transpose(data, (1,0))
        data = numeric.ravel(data)
    if not surface.getColorModel().hasAlpha():
        surface.setRGB(0, 0, surface.width, surface.height,
                       data, 0, surface.width)
    else:
        surf = Surface((surface.width, surface.height),
                       BufferedImage.TYPE_INT_RGB)
        surf.setRGB(0, 0, surface.width, surface.height,
                    data, 0, surface.width)
        g2d = surface.createGraphics()
        g2d.drawImage(surf, 0, 0, None)
        g2d.dispose()
    return None
Ejemplo n.º 6
0
 def render(self, text, antialias, color, background=None):
     """
     Render text onto surface.
     Arguments:
     text to render (string)
     antialias of text (bool)
     color of text (R,G,B)
     background color (R,G,B)
     """
     w,h = self.size(text)
     surf = Surface((w,h), BufferedImage.TYPE_INT_ARGB)
     g2d = surf.createGraphics()
     if background:
         g2d.setColor(Color(background))
         g2d.fillRect(0,0,w,h)
     g2d.setFont(self.font)
     if antialias:
         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
     g2d.setColor(Color(color))
     g2d.drawString(text,0,(h//2)+(self.fontMetrics.getAscent()//2))
     if self.underline:
         g2d.setStroke(BasicStroke(1))
         g2d.drawLine(0,h-1,w-1,h-1)
     g2d.dispose()
     return surf
Ejemplo n.º 7
0
def rotozoom(surface, angle, size):
    """
    Return Surface rotated and resized by the given angle and size.
    """
    if not angle:
        width = int(surface.getWidth() * size)
        height = int(surface.getHeight() * size)
        return scale(surface, (width, height))
    theta = angle * _deg_rad
    width_i = int(surface.getWidth() * size)
    height_i = int(surface.getHeight() * size)
    cos_theta = _fabs(_cos(theta))
    sin_theta = _fabs(_sin(theta))
    width_f = int(_ceil((width_i * cos_theta) + (height_i * sin_theta)))
    if width_f % 2:
        width_f += 1
    height_f = int(_ceil((width_i * sin_theta) + (height_i * cos_theta)))
    if height_f % 2:
        height_f += 1
    surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB)
    at = AffineTransform()
    at.translate(width_f / 2.0, height_f / 2.0)
    at.rotate(-theta)
    g2d = surf.createGraphics()
    ot = g2d.getTransform()
    g2d.setTransform(at)
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                         RenderingHints.VALUE_INTERPOLATION_BILINEAR)
    g2d.drawImage(surface, -width_i // 2, -height_i // 2, width_i, height_i,
                  None)
    g2d.setTransform(ot)
    g2d.dispose()
    return surf
Ejemplo n.º 8
0
def init():
    """
    **pyj2d.font.init**
    
    Initialize font module.
    """
    global _surf, _g2d, _initialized, match_font
    _surf = Surface((1, 1), BufferedImage.TYPE_INT_RGB)
    _g2d = _surf.createGraphics()
    _initialized = True
Ejemplo n.º 9
0
Archivo: font.py Proyecto: jggatc/pyj2d
def init():
    """
    **pyj2d.font.init**
    
    Initialize font module.
    """
    global _surf, _g2d, _initialized, match_font
    _surf = Surface((1,1), BufferedImage.TYPE_INT_RGB)
    _g2d = _surf.createGraphics()
    _initialized = True
Ejemplo n.º 10
0
 def scale(self, surface, size, dest=None):
     """
     Return Surface resized by the given size.
     An optional destination surface can be provided.
     """
     if not dest:
         surf = Surface(size, BufferedImage.TYPE_INT_ARGB)
     else:
         surf = dest
     g2d = surf.createGraphics()
     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
     g2d.drawImage(surface, 0, 0, size[0], size[1], None)
     g2d.dispose()
     return surf
Ejemplo n.º 11
0
def scale(surface, size, dest=None):
    """
    Return Surface resized by the given size.
    An optional destination surface can be provided.
    """
    if not dest:
        surf = Surface(size, BufferedImage.TYPE_INT_ARGB)
    else:
        surf = dest
    g2d = surf.createGraphics()
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                         RenderingHints.VALUE_INTERPOLATION_BILINEAR)
    g2d.drawImage(surface, 0, 0, size[0], size[1], None)
    g2d.dispose()
    return surf
Ejemplo n.º 12
0
def make_surface(array):
    """
    Generates image pixels from array data.
    Argument array containing image data.
    Return Surface generated from array.
    JNumeric required as specified in numeric module.
    """
    if not _initialized:
        _init()
    surface = Surface((array.shape[0],array.shape[1]))
    blit_array(surface, array)
    return surface
Ejemplo n.º 13
0
 def blit_array(self, surface, array):
     """
     Generates image pixels from a JNumeric array.
     Arguments include surface to generate the image, and array of integer colors.
     """
     if not self.initialized:
         self._init()
     if len(array.shape) == 2:
         data = numeric.transpose(array, (1,0))
         data = numeric.ravel(data)
     else:
         data = array[:,:,0]*0x10000 | array[:,:,1]*0x100 | array[:,:,2]
         data = numeric.transpose(data, (1,0))
         data = numeric.ravel(data)
     if not surface.getColorModel().hasAlpha():
         surface.setRGB(0, 0, surface.width, surface.height, data, 0, surface.width)
     else:
         surf = Surface((surface.width,surface.height), BufferedImage.TYPE_INT_RGB)
         surf.setRGB(0, 0, surface.width, surface.height, data, 0, surface.width)
         g2d = surface.createGraphics()
         g2d.drawImage(surf, 0, 0, None)
         g2d.dispose()
     return None
Ejemplo n.º 14
0
def create_cursor(size, data, mask):
    """
    Create cursor image from binary data.
    Arguments cursor size and its binary data and mask.
    Return surface, can be used with mouse.set_cursor.
    """
    surface = Surface(size, Const.SRCALPHA)
    black = Color(0,0,0,255).getRGB()
    white = Color(255,255,255,255).getRGB()
    x = y = 0
    rang = range(8)
    for i in range(len(data)):
        if data[i] or mask[i]:
            for j in rang:
                if data[i] & 0x01<<7-j:
                    surface.setRGB(x+j, y, black)
                elif mask[i] & 0x01<<7-j:
                    surface.setRGB(x+j, y, white)
        x += 8
        if x >= size[0]:
            x = 0
            y += 1
    return surface
Ejemplo n.º 15
0
 def render(self, text, antialias, color, background=None):
     """
     Render text onto surface.
     Arguments:
     text to render (string)
     antialias of text (bool)
     color of text (R,G,B)
     background color (R,G,B)
     """
     w, h = self.size(text)
     surf = Surface((w, h), BufferedImage.TYPE_INT_ARGB)
     g2d = surf.createGraphics()
     if background:
         g2d.setColor(Color(background))
         g2d.fillRect(0, 0, w, h)
     g2d.setFont(self.font)
     if antialias:
         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                              RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
     g2d.setColor(Color(color))
     y = ((h // 2) -
          ((self.fontMetrics.getAscent() + self.fontMetrics.getDescent()) //
           2) + self.fontMetrics.getAscent())
     if not self.underline:
         g2d.drawString(text, 0, y)
     else:
         try:
             text = AttributedString(text)
             text.addAttribute(TextAttribute.FONT, self.font)
             text.addAttribute(TextAttribute.UNDERLINE,
                               TextAttribute.UNDERLINE_ON)
             g2d.drawString(text.getIterator(), 0, y)
         except IllegalArgumentException:
             pass
     g2d.dispose()
     return surf
Ejemplo n.º 16
0
 def __init__(self, size):
     JPanel.__init__(self)
     self.setPreferredSize(Dimension(size[0], size[1]))
     self.surface = Surface(size, BufferedImage.TYPE_INT_RGB)
     self.setBackground(Color.BLACK)
     self.addMouseListener(self)
     self.addMouseMotionListener(self)
     self.addMouseWheelListener(self)
     self.addKeyListener(self)
     self.setFocusable(True)
     self.requestFocusInWindow()
     self.event = env.event
     self.modKey = self.event.modKey
     self.keyRepeat = self.event.keyRepeat
     self.keyHeld = self.event.keyHeld
     self._repainting = Clock._repaint_sync
Ejemplo n.º 17
0
 def _warmup(self):
     surface = [Surface(size) for size in ((5, 5), (5, 5), (3, 3))]
     for i, color in enumerate([(0, 0, 0), (0, 0, 0), (100, 100, 100)]):
         surface[i].fill(color)
     for i in range(500):
         surface[0].blit(surface[2], (1, 1))
     sprite = [Sprite() for i in range(3)]
     group = [Grp() for Grp in (Group, RenderUpdates, OrderedUpdates)]
     for i, grp in enumerate(group):
         sprite[i].image = surface[2]
         sprite[i].rect = sprite[i].image.get_rect(center=(2, 2))
         grp.add(sprite[i])
     for grp in group:
         for i in range(500):
             grp.clear(surface[0], surface[1])
             grp.draw(surface[0])
Ejemplo n.º 18
0
def flip(surface, xbool=True, ybool=False):
    """
    Return Surface that is flipped horizontally, vertically, or both.
    """
    if xbool and ybool:
        at = AffineTransform.getScaleInstance(-1, -1)
        at.translate(-surface.getHeight(), -surface.getHeight())
    elif xbool:
        at = AffineTransform.getScaleInstance(-1, 1)
        at.translate(-surface.getWidth(), 0)
    elif ybool:
        at = AffineTransform.getScaleInstance(1, -1)
        at.translate(0, -surface.getHeight())
    else:
        return surface
    op = AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR)
    bimage = op.filter(surface, None)
    surf = Surface(bimage)
    return surf
Ejemplo n.º 19
0
 def load(self, img_file, namehint=None):
     """
     Load image from file as a java.awt.image.BufferedImage.
     The img_file can be a filename or file-like object.
     Return the bufferedimage as a Surface.
     """
     if isinstance(img_file, str):
         try:
             f = env.japplet.getClass().getResource(
                 img_file.replace('\\', '/'))
             if not f:
                 raise
         except:
             f = File(img_file)
         bimage = ImageIO.read(f)
     else:
         bimage = ImageIO.read(ByteArrayInputStream(img_file.getvalue()))
         img_file.close()
     surf = Surface(bimage)
     return surf
Ejemplo n.º 20
0
def create_cursor(size, data, mask):
    """
    Create cursor image from binary data.
    Arguments cursor size and its binary data and mask.
    Return surface, can be used with mouse.set_cursor.
    """
    surface = Surface(size, Const.SRCALPHA)
    black = Color(0, 0, 0, 255).getRGB()
    white = Color(255, 255, 255, 255).getRGB()
    x = y = 0
    for i in range(len(data)):
        if data[i] or mask[i]:
            for j in range(8):
                if data[i] & 0x01 << 7 - j:
                    surface.setRGB(x + j, y, black)
                elif mask[i] & 0x01 << 7 - j:
                    surface.setRGB(x + j, y, white)
        x += 8
        if x >= size[0]:
            x = 0
            y += 1
    return surface