Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotating the text in a JLabel

Status
Not open for further replies.

phimlob

Programmer
Apr 5, 2000
9
SE
Hullo all- While Im at it, here is another problem:<br>What I want is a JLabel with the text rotated pi/2 anti-clockwise. I took a shot at the problem but no matter how I try to tackle it, I find myself either with a NullPointerException or some other problem.&lt;br&gt;<br>Can someone help me?<br><br>/Fimblo<br><br>Here is the code as of now.<br><br><br>import javax.swing.*;<br>import java.awt.*;<br>import java.awt.image.*;<br><br>public class RotatedLabel extends JLabel {<br>&nbsp;&nbsp;&nbsp;&nbsp; public static Canvas canvas = new Canvas();<br>&nbsp;&nbsp;&nbsp;&nbsp; public RotatedLabel(String name) {<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; super();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; setIcon(createImage(name));<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp;&nbsp; private ImageIcon createImage(String name) {<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ImageIcon icon = new ImageIcon();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Image i = icon.getImage();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Graphics2D g = (Graphics2D) icon.getGraphics();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; g.rotate((-0.5)*Math.PI, 50, 50); <br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; g.drawString(name, 50, 50);<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; return icon;<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br>&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] in) {<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; JFrame world = new JFrame(&quot;test&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Container cp = world.getContentPane();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; cp.add(new RotatedLabel(&quot;one&quot;));<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; cp.add(new RotatedLabel(&quot;two&quot;));<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; world.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; world.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp; } <br>}<br>
 
Just a guess, but, shouldn't this:<br><br>&gt; Graphics2D g = (Graphics2D) icon.getGraphics();<br><br>be this:<br><br>Graphics2D g = (Graphics2D)i.getGraphics();<br><br>-pete
 
heh yeah. I put up the file I had been fiddling with, and I forgot to make everything back to what it had been... But it doesnt really change anything. I still get a NPE...<br><br>But thanks for pointing it out!<br>/Mattias
 
Do you know where the NPE occurs? I mean do you know which object is null?<br><br>If not check each one after creation against null so you can find the problem. Use a debugger if you have one, otherwise use code to tell you like this:<br><br>Graphics2D g = (Graphics2D)i.getGraphics();<br>if ( null == g)<br>&nbsp;&nbsp;System.out.println(&quot;Graphics2D creation failed&quot;);<br>else{<br>...<br>}<br><br>-pete<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top