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!

drawstring 1

Status
Not open for further replies.

steve981

MIS
Apr 2, 2004
2
CA
hi, i'm looking for a way to draw a string in a canvas, but not horizontally.
 
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);
               }  
}
 
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
public class StillLife2 extends Canvas {
    public StillLife2() {
	setBackground(Color.white);
    }
    public void paint(Graphics g) {
        Graphics2D g2;
        g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                            RenderingHints.VALUE_RENDER_QUALITY);
        int w = getSize().width;
        int h = getSize().height;
	AffineTransform at = new AffineTransform();
	at.setToTranslation(30, 50);
        AffineTransform fontAT = new AffineTransform();
	//fontAT.shear(0.2, 0.0);
        fontAT.rotate(3.14/2.0);
        FontRenderContext frc = g2.getFontRenderContext();
        Font theFont = new Font("Times", Font.BOLD, w/25);
        String s = new String("Still Life with Text");
        TextLayout tstring = new TextLayout(s, theFont, frc);
        Font theDerivedFont = theFont.deriveFont(fontAT);
	String str = new String("Still Life with Slanted Text");
	TextLayout tstring2 = new TextLayout(str, theDerivedFont, frc);
//Draw regular string
	g2.setColor(Color.blue);
        g2.transform(at);
        tstring.draw(g2, (float)0, (float)0);
//Draw string with shear transformation on the font
	g2.setColor(Color.green);
        g2.transform(at);
	tstring2.draw(g2, (float)0, (float)0);
    }
    public static void main(String s[]) {
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowClosed(WindowEvent e) {System.exit(0);}
        };
        Frame f = new Frame("2D Text");
        f.addWindowListener(l);
        f.add("Center", new StillLife2());
        f.pack();
        f.setSize(new Dimension(600, 175));
        f.show();
    }
}
// look at this link for more details
[URL unfurl="true"]http://java.sun.com/developer/onlineTraining/Media/2DText/style.html[/URL]
// please describe what you want, drawString vetically? then draw other graphics without rotate?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top