Saturday, February 9, 2013
Today I am going to post how you paint and stroke a shape in java using Graphics2D. But before going into the details of the program I would like to briefly describe about the painting and stroking operation.

•  Painting is the process of filling the interior of the shape with a color, color gradient, or texture. The process of filling the shape interior is done in two steps :
  1. First, tell the Graphics2D how to fill shapes with a call to setPaint(). This method accepts any object that implements the java.awt.Paint interface. The Graphics2D stores the Paint away as part of its state. When it comes time to fill a shape, Graphics2D will use the Paint to determine what colors should be used to fill the shape. The 2D API comes with three kinds of "canned" paints: solid colors, a linear color gradient, and a texture fill. You can add your own Paint implementations if you wish. 
  2. Now you can tell Graphics2Dto fill a shape by passing it to fill()
•  Stroking is the process of drawing the shape's outline. You can draw an outline using different line widths, line styles, and colors. The stroking process is completed in three steps :
  1. First, tell the Graphics2D how you want the outline to be drawn by calling setStroke(). This method accepts any object that implements the java.awt.Stroke interface. The 2D API comes with a class, java.awt.BasicStroke, that implements common stroking options. 
  2. Use setPaint() to tell the Graphics2D how the outline itself should be drawn. Outlines, like the interior of shapes, can be drawn using a color, a gradient, a texture, or anything else that implements the Paint interface. 
  3. Draw the outline of the shape using Graphics2D's draw() method. The Graphics2D uses the Stroke from step 1 to determine what the outline looks like. The Paint from step 2 is used to actually render the outline. 
Screenshot showing painting and stroking

--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.GradientPaint;
import javax.swing.JPanel;
import javax.swing.JFrame; 
import java.awt.geom.Ellipse2D;

public class Painting_Stroking extends JPanel{ 

  public static void main(String[] args) {
      JFrame f=new JFrame("PaintingAndStroking v1.0");
      Painting_Stroking obj= new Painting_Stroking();
      f.getContentPane().add(obj);
      f.setSize(320, 150);
      f.setVisible(true);
      f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
  }
  
  public void paintComponent(Graphics g) { 
      Graphics2D g2 = (Graphics2D)g; 
      double x = 15, y = 30, w = 70, h = 70;  //defining shape attributes
      Ellipse2D.Double e = new Ellipse2D.Double(x, y, w, h); 
      GradientPaint gp = new GradientPaint(75, 75, Color.white, 
                       95, 95, Color.gray, true); 
      // Fill with a gradient
      g2.setPaint(gp); 
      g2.fill(e);  //filling first ellipse

      // Stroke with a solid color
      e.setFrame(x + 100, y, w, h); 
      g2.setPaint(Color.black); 
      g2.setStroke(new BasicStroke(8)); 
      g2.draw(e);  //drawing second ellipse with stroke and solid color

      // Stroke with a gradient
      e.setFrame(x + 200, y, w, h); 
      g2.setPaint(gp); 
      g2.draw(e);  //drawing second ellipse with stroke and gradient
   } 
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters