Graphics Util: draw shapes and text : Paint « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » PaintScreenshots 
Graphics Util: draw shapes and text
     
/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 

import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class GraphicsUtil {
    public static final int H_LEFT = -1;
    public static final int H_CENTER = 0;
    public static final int H_RIGHT = 1;
    public static final int V_TOP = -1;
    public static final int V_CENTER = 0;
    public static final int V_BASELINE = 1;
    public static final int V_BOTTOM = 2;

    static public void switchToWidth(Graphics g, int width) {
        if(instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2Dg;
            g2.setStroke(new BasicStroke((floatwidth));
        }
    }

    static public void drawCenteredArc(Graphics g, int x, int y,
            int r, int start, int dist) {
        g.drawArc(x - r, y - r, * r, * r, start, dist);
    }

    static public Rectangle getTextBounds(Graphics g, Font font,
            String text, int x, int y, int halign, int valign) {
        if(g == nullreturn new Rectangle(x, y, 00);
        Font oldfont = g.getFont();
        if(font != nullg.setFont(font);
        Rectangle ret = getTextBounds(g, text, x, y, halign, valign);
        if(font != nullg.setFont(oldfont);
        return ret;
    }
    static public Rectangle getTextBounds(Graphics g, String text,
            int x, int y, int halign, int valign) {
        if(g == nullreturn new Rectangle(x, y, 00);
        FontMetrics mets = g.getFontMetrics();
        int width = mets.stringWidth(text);
        int ascent = mets.getAscent();
        int height = ascent + mets.getDescent();
        Rectangle ret = new Rectangle(x, y, width, height);
        
        switch(halign) {
            case H_CENTER: ret.translate(-(width / 2)0)break;
            case H_RIGHT:  ret.translate(-width, 0)break;
            default: ;
        }
        switch(valign) {
            case V_TOP:      break;
            case V_CENTER:   ret.translate(0, -(ascent / 2))break;
            case V_BASELINE: ret.translate(0, -ascent)break;
            case V_BOTTOM:   ret.translate(0, -height)break;
            default: ;
        }
        return ret;
    }

    static public void drawText(Graphics g, Font font,
            String text, int x, int y, int halign, int valign) {
        Font oldfont = g.getFont();
        if(font != nullg.setFont(font);
        drawText(g, text, x, y, halign, valign);
        if(font != nullg.setFont(oldfont);
    }
    static public void drawText(Graphics g, String text,
            int x, int y, int halign, int valign) {
        if(text.length() == 0return;
        Rectangle bd = getTextBounds(g, text, x, y, halign, valign);
        g.drawString(text, bd.x, bd.y + g.getFontMetrics().getAscent());
    }
    static public void drawCenteredText(Graphics g, String text,
            int x, int y) {
        drawText(g, text, x, y, H_CENTER, V_CENTER);
    }

    static public void drawArrow(Graphics g, int x0, int y0, int x1, int y1,
            int headLength, int headAngle) {
        double offs = headAngle * Math.PI / 180.0;
        double angle = Math.atan2(y0 - y1, x0 - x1);
        int[] xs = x1 + (int) (headLength * Math.cos(angle + offs)), x1,
            x1 + (int) (headLength * Math.cos(angle - offs)) };
        int[] ys = y1 + (int) (headLength * Math.sin(angle + offs)), y1,
            y1 + (int) (headLength * Math.sin(angle - offs)) };
        g.drawLine(x0, y0, x1, y1);
        g.drawPolyline(xs, ys, 3);
    }
}

   
    
    
    
    
  
Related examples in the same category
1.Draw stringDraw string
2.Draw canvas with color and textDraw canvas with color and text
3.Anti AliasAnti Alias
4.PaintsPaints
5.Program to draw gridsProgram to draw grids
6.PlotPlot
7.Picture Scaler
8.Radial GradientRadial Gradient
9.Rotation About Center
10.Safe Repaint
11.Scale Test
12.Scaling Methods
13.Simple Attributes of painting
14.Two Stops GradientTwo Stops Gradient
15.Bad vs. Good Primitive Rendering
16.Static methods for some common painting functions
17.Set Text Anti Aliasing
18.Utilties for painting visual effects
19.Your own Graphics2D
20.Reads a Paint object that has been serialised
21.A panel that displays a paint sample.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.