This discussion is archived
5 Replies Latest reply: Jun 14, 2010 7:27 AM by 800414 RSS

Graphics Draw a curce arrow icon

800414 Newbie
Currently Being Moderated
can i help me locate the arrow head in to top right ARC
this is what i have so far

thank you
package org.jtpc.gui.icons;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;



public class IconRotateRight implements Icon{

  private int m_size = 20;

  public IconRotateRight(int size){
     m_size = size;
  }
  
  public int getIconHeight(){
    return m_size;
  }

  public int getIconWidth(){
    return m_size;
  }

  public void paintIcon(Component c, Graphics g, int x, int y){
    Graphics2D g2 = (Graphics2D)g;
    Object hintOriginal = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    
    int strokeSize = m_size/6;
    
    Stroke strokeOriginal = g2.getStroke();
    g2.setStroke(new BasicStroke(strokeSize ,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER));
    
    g2.drawArc(x+(strokeSize/2), y+(strokeSize/2), m_size-(strokeSize*2), m_size-(strokeSize*2), 45, 180);

    g2.setStroke(strokeOriginal);

    
    
    int arrSize = (m_size/4) + strokeSize;
    int[] xArr = new int[3];
    int[] yArr = new int[3];

    xArr[0] = x+(strokeSize/2);
    xArr[1] = x +(strokeSize/2)+ arrSize;
    xArr[2] = x +(strokeSize/2)+ (arrSize/2);

    yArr[0] = y+(strokeSize/2);
    yArr[1] = y+(strokeSize/2);
    yArr[2] = y+(strokeSize/2) - (arrSize/2);


    AffineTransform  origXform = g2.getTransform();
    AffineTransform newXform  = (AffineTransform)(origXform.clone());
    
    int anchorX = (x+(strokeSize/2))  + (m_size-(strokeSize*2))/2;
    int anchorY = (y+(strokeSize/2))  + (m_size-(strokeSize*2))/2;

    anchorX = anchorX + (m_size/8);
    anchorY = anchorY - (m_size/8);
    
    newXform.rotate(Math.toRadians(135) ,anchorX ,anchorY);
    g2.setTransform(newXform);

    g2.fillPolygon(xArr ,yArr ,3);
    
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hintOriginal);
  }
  
}
  • 1. Re: Graphics Draw a curce arrow icon
    800346 Explorer
    Currently Being Moderated
    Try this:
    public void paintIcon(Component c, Graphics g, int x, int y) {
        int startAngle = 45;
        Graphics2D g2 = (Graphics2D) g;
        Object hintOriginal = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int strokeSize = m_size / 6;
        Stroke strokeOriginal = g2.getStroke();
        g2.setStroke(new BasicStroke(strokeSize, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        int x1 = x + (strokeSize / 2) + 1;
        int y1 = y + (strokeSize / 2) + 1;
        int w1 = m_size - (strokeSize * 2) - 2;
        int h1 = m_size - (strokeSize * 2) - 2;
        g2.drawArc(x1, y1, w1, h1, startAngle, 180);
        g2.setStroke(strokeOriginal);
        int arrSize = w1 / 2 - strokeSize / 2;
        int[] xArr = new int[3];
        int[] yArr = new int[3];
        xArr[0] = x1 + w1 - arrSize;
        xArr[1] = x1 + w1 + arrSize;
        xArr[2] = x1 + w1;
        yArr[0] = y1 + h1 / 2;
        yArr[1] = y1 + h1 / 2;
        yArr[2] = y1 + h1 / 2 + arrSize;
        AffineTransform origXform = g2.getTransform();
        AffineTransform newXform = (AffineTransform) (origXform.clone());
        int anchorX = x1 + w1 / 2;
        int anchorY = y1 + h1 / 2;
        newXform.rotate(Math.toRadians(-startAngle), anchorX, anchorY);
        g2.setTransform(newXform);
        g2.fillPolygon(xArr, yArr, 3);
        g2.setTransform(origXform);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hintOriginal);
    }
  • 2. Re: Graphics Draw a curce arrow icon
    800414 Newbie
    Currently Being Moderated
    working like charm :)
  • 3. Re: Graphics Draw a curce arrow icon
    800414 Newbie
    Currently Being Moderated
    thank you
  • 4. Re: Graphics Draw a curce arrow icon
    800414 Newbie
    Currently Being Moderated
    well almost :)

    its not on the correct size ,
    see this example
    import java.awt.BasicStroke;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Stroke;
    import java.awt.geom.AffineTransform;
    
    
    
    public class IconRotateRight implements SizableIcon{
    
      private int m_size = 20;
      
      public int getIconHeight(){
        return m_size;
      }
    
      public int getIconWidth(){
        return m_size;
      }
    
      public void paintIcon(Component c, Graphics g, int x, int y){
        
        
        
        Graphics2D g2 = (Graphics2D) g;
        Object hintOriginal = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        int startAngle = 45;
        int strokeSize = m_size / 6;
        
        Stroke strokeOriginal = g2.getStroke();
        g2.setStroke(new BasicStroke(strokeSize, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        
        
        int x1 = x + (strokeSize / 2) + 1;
        int y1 = y + (strokeSize / 2) + 1;
        int w1 = m_size - (strokeSize * 2) - 2;
        int h1 = m_size - (strokeSize * 2) - 2;
        
        g2.drawArc(x1, y1, w1, h1, startAngle, 180);
        
        g2.setStroke(strokeOriginal);
        
        
        int arrSize = w1 / 2 - strokeSize / 2;
        int[] xArr = new int[3];
        int[] yArr = new int[3];
        
        xArr[0] = x1 + w1 - arrSize;
        xArr[1] = x1 + w1 + arrSize;
        xArr[2] = x1 + w1;
        
        yArr[0] = y1 + h1 / 2;
        yArr[1] = y1 + h1 / 2;
        yArr[2] = y1 + h1 / 2 + arrSize;
        
        AffineTransform origXform = g2.getTransform();
        AffineTransform newXform = (AffineTransform) (origXform.clone());
        
        int anchorX = x1 + w1 / 2;
        int anchorY = y1 + h1 / 2;
        
        newXform.rotate(Math.toRadians(-startAngle), anchorX, anchorY);
        
        g2.setTransform(newXform);
        g2.fillPolygon(xArr, yArr, 3);
        
        g2.setTransform(origXform);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hintOriginal);
      }
    
      public void setSize(int size){
        m_size = size;
      }
      
    }
    package test;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    
    import org.jtpc.gui.icons.IconRotateRight;
    
    
    public class IconTest extends JFrame{
      public IconTest(){
        IconRotateRight icon = new IconRotateRight();
        icon.setSize(500);
        JButton btn = new JButton(icon);
        btn.setBorder(new CompoundBorder(new LineBorder(Color.GREEN ,8 ,true) ,new EmptyBorder(new Insets(0,0,0,0))));
        setLayout(new BorderLayout());
        add(btn ,BorderLayout.CENTER);
      }
      public static void main(String[] args){
        IconTest t = new IconTest();
        t.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        t.setSize(600 ,600);
        t.setVisible(true);
      }
    }
  • 5. Re: Graphics Draw a curce arrow icon
    800414 Newbie
    Currently Being Moderated
    ok got it

        int strokeSize = m_size / 5;
        
        Stroke strokeOriginal = g2.getStroke();
        g2.setStroke(new BasicStroke(strokeSize, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        
        
        int x1 = x + (int)(strokeSize * 0.5);
        int y1 = y + (int)(strokeSize * 0.8);
        int w1 = m_size - (int)(strokeSize * 1.1);
        int h1 = m_size - (int)(strokeSize * 1.1);
    thank you