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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotating a graphic

Status
Not open for further replies.

mluken

Programmer
Dec 31, 2003
54
0
0
US
I am using a java servlet to dynamically generate an image file (a bar chart). I have been asked to place labels on the X and Y coords. The X is easy because you just use the getGraphics().drawString(). However, I can't seem to figure out how to do the Y label as I will need to have the label rotated 90 degrees counter-clockwise. This seems like way too common of a thing to not be able to do though. Has anybody encountered such a thing?

Thanks!!!

Mike
 
Code:
//copy from my post
// positive value or negative value will determine to rotate 90 degrees clockwisely or counter-clockwisely.
// g2.rotate(Math.PI*0.25,250.0,250.0);
// g2.rotate(Math.PI*(-0.25),250.0,250.0);

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.util.*;
class WholeFrame3 extends JFrame implements ActionListener
      {
       CenterCanvas3 cc;
       JButton jb,jb2;
       JPanel ccPanel,jbPanel;
       double dividedBy = 36.0;
       public WholeFrame3()
              {
               //getContentPane().setBackground(Color.green);
               getContentPane().setLayout(null);
               ccPanel = new JPanel();
               ccPanel.setBounds(0,0,500,500);
               cc = new CenterCanvas3();
               cc.setBounds(0,0,500,500);
               getContentPane().add(ccPanel);
               ccPanel.add(cc);

               jbPanel = new JPanel();
               jbPanel.setBackground(Color.blue);
               jbPanel.setBounds(0,600,500,100);
               getContentPane().add(jbPanel);

               jb = new JButton("click");
               jb.addActionListener(this);
               jbPanel.add(jb);
              }
       public static void main(String args[])
              {
               WholeFrame3 wf = new WholeFrame3();
               wf.setSize(600,700);
               wf.setVisible(true);
              }
       public void actionPerformed(ActionEvent e)
              {       
               if(e.getSource()==jb)
                 {
                     dividedBy-=1.0;

                  cc.setRadian(Math.PI*dividedBy/36.0);
                 }
              }
}

class CenterCanvas3 extends Canvas
      {

       double rad = Math.PI*36.0/36.0;
       public void setRadian(double tempRad)
              {
               rad = tempRad;
               repaint();
              }
       public double getRadian()
              {
               System.out.println(rad);
               return rad;
              }
       public void update()
              {}

       public void paint(Graphics g)
              {
    Graphics2D g2 = (Graphics2D)g;
setBackground(Color.green);
    //Update transform to provide rotation and display, 
    // the transform values.    
g2.rotate(getRadian(),250.0,250.0);
g2.setColor(Color.black);
g2.drawRect(120, 120, 20, 200);
g2.drawString ("Material", 30, 220);
g2.drawString ("Ratings", 30, 240);
g2.drawString ("+5", 102, 130);
g2.drawString ("0", 107, 220);
g2.drawString ("-5", 102, 315);
               }  
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top