NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
gmseed
Joined: 01 Dec 2008 Posts: 38
|
Posted: Thu Nov 18, 2010 2:31 pm Post subject: paintComponent() Graphics2D versus panel Graphics2D |
|
|
Hi
If I place the panel below onto a frame using the argument Graphics2D object then I see the centred circle.
However, if I use the Graphics2D object of the panel [via this.getGraphics()] then I do not see the circle.
I'm trying to draw several objects on a panel accurately and found that the argument Graphics2D object refers to the overall frame graphics object and performing any affine transform operations are with respect to the frame and not the panel. Thus, if I wanted to use the frame Graphics2D I would to calculate offsets to add to panel object transforms. With the frame having a menubar, toolbar, status bar etc, calculating such offsets is error prone.
Clearly, it is much simpler to use the Graphics2D object of the panel itself. However, it's refusing to make the panel circle visible?
Does anyone know a solution to this problem?
Graham
| Code: |
public class TestJPanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//Graphics2D graphics = g2;
Graphics2D graphics = (Graphics2D)this.getGraphics();
Rectangle panelBounds = this.getBounds();
int panelTopLeftX = panelBounds.x;
int panelTopLeftY = panelBounds.y;
int panelWidth = this.getWidth();
int panelHeight = this.getHeight();
// circle
double circleRadius = 250.0;
double circleDiameter = 2.0 * circleRadius;
java.awt.geom.Ellipse2D.Double jcircle = new java.awt.geom.Ellipse2D.Double(0,0,circleDiameter,circleDiameter);
// save original transform
AffineTransform origTransform = graphics.getTransform();
AffineTransform tx = AffineTransform.getTranslateInstance(panelWidth/2-circleRadius, panelHeight/2-circleRadius); // alternative static method of creating transform
graphics.setTransform(tx);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setColor(Color.BLACK);
graphics.draw(jcircle);
graphics.fill(jcircle);
// restore original transform
graphics.setTransform( origTransform );
}
} // class TestJPanel
|
|
|
| Back to top |
|
 |
gmseed
Joined: 01 Dec 2008 Posts: 38
|
Posted: Thu Nov 18, 2010 3:57 pm Post subject: |
|
|
Hi
Gotya!! The affine transform of the argument Graphics2D has to be concatenated with the panel affine transform - see below.
The tutorials never tell you that!
Graham
| Code: |
private void paintCircle_Test(Graphics2D g2)
{
// panel properties
Rectangle panelBounds = this.getBounds();
int panelTopLeftX = panelBounds.x;
int panelTopLeftY = panelBounds.y;
int panelWidth = panelBounds.width;
int panelHeight = panelBounds.height;
// circle
double circleRadius = 250.0;
double circleDiameter = 2.0 * circleRadius;
java.awt.geom.Ellipse2D.Double jcircle = new java.awt.geom.Ellipse2D.Double(0,0,circleDiameter,circleDiameter);
// save original transform
AffineTransform originalTransform = g2.getTransform();
// total affine - initialising with g2 affine
AffineTransform affineTotal = new AffineTransform(originalTransform);
// panel affine
AffineTransform affinePanel = AffineTransform.getTranslateInstance(panelWidth/2-circleRadius, panelHeight/2-circleRadius);
// concat
affineTotal.concatenate(affinePanel);
g2.setTransform(affineTotal);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setColor(Color.BLACK);
g2.draw(jcircle);
g2.fill(jcircle);
// restore original transform
g2.setTransform(originalTransform);
}
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|