Results 1 to 5 of 5

Thread: Confused about GradientPaint class

  1. #1
    Join Date
    Jan 2010
    Posts
    76

    Confused about GradientPaint class

    Hello friend,

    I am Bsc(IT) student with lost of confusion about the java classes. I am getting troubled while referring the GradientPaint class of java. I want to be aware about the constructors as well as the methods of GradientPaint class of java. If you posses the sound knowledge about the GradientPaint class, then please try to share it with me. I wonder if you provide any example for GradientPaint class.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Confused about GradientPaint class

    The GradientPaint class provides a way to fill a Shape with a linear color gradient pattern. If Point P1 with Color C1 and Point P2 with Color C2 are specified in user space, the Color on the P1, P2 connecting line is proportionally changed from C1 to C2. Any point P not on the extended P1, P2 connecting line has the color of the point P' that is the perpendicular projection of P on the extended P1, P2 connecting line. Points on the extended line outside of the P1, P2 segment can be colored in one of two ways.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Confused about GradientPaint class

    Please refer following example of GradientPaint class:
    Code:
    import java.awt.GradientPaint;
    import javax.swing.JPanel;
    
    public class GradientPaintDM extends JPanel {
    
      public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d1 = (Graphics2D) g;
        GradientPaint gp01 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);
        g2d1.setPaint(gp01);
        g2d1.fillRect(30, 30, 300, 40);
    
      }
    
      public static void main(String[] args) {
    
        JFrame frame1 = new JFrame("GradientsRedYellow");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.add(new Main());
        frame1.setSize(300, 350);
        frame1.setLocationRelativeTo(null);
        frame1.setVisible(true);
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Confused about GradientPaint class

    I suggest you to go through the following methods of GradientPaint class:
    • GradientPaint(Point2D pnt, Color color01, Point2D pnt2, Color color2, boolean bl)
    • GradientPaint(Point2D pnt, Color color01, Point2D pnt2, Color color2)
    • GradientPaint(float x1, float y1, Color color01, float x2, float y2, Color color2, boolean bl)
    • GradientPaint(float x1, float y1, Color color01, float x2, float y2, Color color2)

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Confused about GradientPaint class

    Hi friend,

    I hope something below code will solve your confusion about the GradientPaint class:
    Code:
    import java.awt.GradientPaint;
    public class GradientPainMain extends JPanel {
    
      public GradientPainMain() { 
     }
      public void paint(Graphics g) {
        Graphics2D zdv = (Graphics2D) g;
    
        Point2D.Float p1m = new Point2D.Float(150.f, 75.f);
        Point2D.Float p2m = new Point2D.Float(250.f, 75.f);
        float width = 300;
        float height = 50;
        GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY,
            true);
        Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25,
            width, height);
        zdv.setPaint(g1);
        zdv.fill(rect1);
        zdv.setPaint(Color.BLACK);
        zdv.draw(rect1);
        zdv.draw(new Line2D.Float(p1, p2));
        zdv.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
        zdv.drawString("p1", p1.x - 20, p1.y);
        p2m.setLocation(250, 200);
     
      }
      public static void main(String[] ahrgs) {
        JFrame frameB = new JFrame();
        frameB.getContentPane().add(new MainClass());
    
        frameB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameB.setSize(300, 200);
        frameB.setVisible(true);
      }
    }

Similar Threads

  1. Confused about Scrollbar class
    By Jagruti23 in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:18 PM
  2. Confused with JInternalFrame class
    By Truster in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 09:58 AM
  3. Confused with JSpinner class
    By Bottlenecked in forum Software Development
    Replies: 4
    Last Post: 15-02-2010, 09:51 AM
  4. Confused with OverlayLayout class
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 09:37 AM
  5. Confused with 'Class SpinnerNumberModel'
    By Khan Baba in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 11:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,673,211.38960 seconds with 16 queries