Issue1337592

classification
Title: Graphics2D properties erratic
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: cgroves, mlh, rluse
Priority: low Keywords:

Created on 2005-10-25.15:38:46 by mlh, last changed 2008-01-15.09:28:56 by mlh.

Messages
msg1067 (view) Author: Magnus Lie Hetland (mlh) Date: 2005-10-25.15:38:46
The following code is a "literal" translation from a
Java 2D API example (from a book on the subject):

class Drawing(JComponent):

    def paint(self, g):
        shape = Rectangle(40, 40, 200, 100)

        g.paint = Color.orange
        g.fill(shape)

        g.stroke = BasicStroke(10)
        g.paint = Color(128, 0, 0)
        g.draw(shape)

It does not work as it should. When the shape is drawn
(using g.draw) the stroke and color have reverted to
what they were at some earlier point. I.e., the color
reverts to Color.orange, and the stroke reverts to the
default (1 point wide). If, however, the line

        g.stroke = BasicStroke(10)

is replaced with

        g.setStroke(BasicStroke(10))

the example works. As far as I can tell from the Jython
docs, these two statements are supposed to be
equivalent. Also, the fact that it's only necessary to
change *one* of these statements (and not, say, the
assignment to g.paint) makes this seem like a bug to me...
msg1068 (view) Author: Magnus Lie Hetland (mlh) Date: 2005-10-25.15:43:15
Logged In: YES 
user_id=20535

Dang -- I knew I forgot something. Here's some extra context
info ;)

$ jython --version
Jython 2.2a1 on java (JIT: null)
$ java -version
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build
1.5.0_02-56)
Java HotSpot(TM) Client VM (build 1.5.0_02-36, mixed mode,
sharing)
$ uname -v
Darwin Kernel Version 8.2.0: Fri Jun 24 17:46:54 PDT 2005;
root:xnu-792.2.4.obj~3/RELEASE~PPC
msg1069 (view) Author: Magnus Lie Hetland (mlh) Date: 2005-11-01.13:27:15
Logged In: YES 
user_id=20535

I'm seeing some possibly related behavior in the yFiles
package (www.yworks.com/en/products_yfiles_about.htm).

Mostly, the bean accessor mechanism (x.getFoo/x.setFoo ->
x.foo) works nicely, but in some cases it does not... As an
example (the context may not be all that informative -- the
yFiles sources isn't available AFAIK), the following works...

class MyMode(ViewMode):

    def mouseClicked(self, event):
	hit = self.getHitInfo(event)
	if hit.hasHitEdges():
	    e = hit.hitEdge
	    w = self.getGraph2D().getDataProvider('weight')
	    print w.get(e)

... while the following does not:

class MyMode(ViewMode):

    def mouseClicked(self, event):
	hit = self.getHitInfo(event)
	if hit.hasHitEdges():
	    e = hit.hitEdge
	    w = self.graph2D.getDataProvider('weight')
	    print w.get(e)

The error is:

    AttributeError: 'instance' object has no attribute 'graph2D'

(Note that hit.hitEdge is derived from hit.getHitEdge() --
and this works.)

Even though this *might* not be the same bug, the
error/behavior is certainly the same. I've tried accessing
ViewMode().graph2D too (without subclassing) -- doesn't seem
to make any difference.
msg1070 (view) Author: Charlie Groves (cgroves) Date: 2006-09-07.04:18:35
Logged In: YES 
user_id=1174327

The implementing class for Graphics2D is
sun.java2d.SunGraphics2D.  That class has a public stroke
field which is set by 'g.stroke =' in Jython but not by the
g.setStroke method.  We'll have to figure out how to handle
when a bean accessor maps to the same name as an existing
field.  This is somewhat similar to bug #1509095 which is
about how to access a method with the same name as a field
in a java class from jython.  

I think the yFiles stuff is a separate bug.  Would you mind
opening a new entry for it with a test case I can reproduce?
 Something like your initial entry for this bug would be
great.  I can't do anything without the source for yFiles to
make it happen.
msg1071 (view) Author: Bob Luse (rluse) Date: 2007-12-24.12:38:02
OK, its a little tricky to do Graphics 2D.  But here is an example pretty similar to yours in Jython and awt Graphics and Jython with awt Graphics2D.  You have to actually do a translation from Graphics to Graphics 2D.


### AWT Graphics ####

import java.awt as awt
import javax.swing as swing


#import java.awt.Graphics2D as G2D

class Drawing(swing.JFrame):
    
    def __init__(self):
        swing.JFrame.__init__(self, size=(400, 400), title="Test Frame")
        self.setDefaultCloseOperation(swing.JFrame.EXIT_ON_CLOSE)
        self.setResizable(False)
        self.setLayout(None)
        self.validate()
        self.setVisible(True)

    def paint(self, g):
        
        swing.JFrame.paint(self, g)
        
        g.setColor(awt.Color.orange)
        shape = awt.Rectangle(40, 40, 200, 100)
        g.fill(shape)

        g.stroke = awt.BasicStroke(10)
        g.setColor(awt.Color(128, 0, 0))
        g.draw(shape)


if __name__ == "__main__":
    Drawing()


### AWTGraphics2D #######

import java.awt as awt
import javax.swing as swing

import java.awt.Graphics2D as G2D

class Drawing(swing.JFrame):
    
    def __init__(self):
        swing.JFrame.__init__(self, size=(400, 400), title="Test Frame")
        self.setDefaultCloseOperation(swing.JFrame.EXIT_ON_CLOSE)
        self.setResizable(False)
        self.setLayout(None)
        self.validate()
        self.setVisible(True)

    def paint(self, g):
        
        G2D.setPaint( g, swing.JFrame.paint(self, g))
        
        G2D.setColor(g, awt.Color.orange)
        G2D.fillRect(g, 40, 40, 200, 100)

        G2D.setStroke(g, awt.BasicStroke(1))
        G2D.setColor(g, awt.Color(128, 0, 0))
        G2D.draw(g, awt.Rectangle(40, 40, 200, 100))


if __name__ == "__main__":
    Drawing()
msg1072 (view) Author: Magnus Lie Hetland (mlh) Date: 2008-01-15.09:28:56
Regarding yfiles: I don't have a license (and don't use it) anymore, so I guess I won't be able to open a bug on that.
History
Date User Action Args
2005-10-25 15:38:46mlhcreate