aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with j2d.drawString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with j2d.drawString() " Watch "Problem with j2d.drawString() " New topic
Author

Problem with j2d.drawString()

Viswanath Thulluru
Greenhorn

Joined: Mar 17, 2014
Posts: 8
I have used the following code and I am trying to render a String in the JPanel. The text is getting displayed in the mirror image format.
I understand that there is a problem with .scale(), not able to get an idea about the corrective step. Please help.
Please run the below class.

class Surface extends JPanel {

final int PAD = 40;

private void doDrawing(Graphics g)
{
Graphics2D g2d = (Graphics2D) g.create();

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();
int h = getHeight();

// Traslate the Origin and flip the co-ordinate space
g2d.translate(PAD, h - PAD);
g2d.scale(1, -1);

// Draw X and Y axes
g2d.setStroke(new BasicStroke(2));
g2d.draw(new Line2D.Double(0, 0, 0, h-PAD));
g2d.draw(new Line2D.Double(0, 0, w-PAD, 0));


g2d.drawString("Test String", 100, 100);

}

@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class Example2 extends JFrame{

public Example2() {

initUI();
}

private void initUI() {

setTitle("Simple Java 2D example");
add(new Surface());
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// For displaying the frame to the Center of the screen.
setLocationRelativeTo(null);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {

Example2 sk = new Example2();
sk.setVisible(true);
}
});
}
}






[Thumbnail for Image.jpg]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Yes, you are setting the Graphics object to rightȁto‑left mode with that scale() call.
Read about affine transforms here and here. Now you know how to use create() to make a Graphics object for right‑to‑left mode and keep the original for left‑to‑right mode.
Viswanath Thulluru
Greenhorn

Joined: Mar 17, 2014
Posts: 8
Hi Campbell,

Thanks for your response. The text and values displayed are not printing from right to left, they are in Upside down style.
I am very new to Java2D. Can you please help me with a code snippet for corrective step?

- Viswanath T
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Exactly the same problem, only with (1, -1) rather than (-1, 1). And exactly the same solution.

You did actually say mirror image not upside down, and screenshots are awkward to read, so we don't bother looking at them.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Campbell Ritchie wrote:You did actually say mirror image


Maybe mirrors work differently where he lives -- like the way draining water rotates differently in the southern hemisphere.
Viswanath Thulluru
Greenhorn

Joined: Mar 17, 2014
Posts: 8
Good one Brett!

Hi Campbell, sorry for incorrect words. Here is the entire code. Please check and provide your suggestions.



Thanks,
Viswanath T
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You have got the idea of using a copy of the Graphics object for the affine transforms. Spot on You would do well to use its dispose method, too.
What you do is use the copy of the Graphics object to apply the transforms to, and keep the original object for the writing. So rather than g2.drawString(...) try g.drawString(...).

And I never knew that mirrors rotated different directions in the North and the South.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with j2d.drawString()