Results 1 to 6 of 6

Thread: What is the use of Graphics class?

  1. #1
    Join Date
    Jan 2010
    Posts
    99

    What is the use of Graphics class?

    Hello friends,

    I just started to study advanced java programming language. Basically I have knowledge about the C++ programming, but it's totally different from advanced java programming. In which the I am not able getting the use of Graphics class of java. But have to use this class in my java project. So please something about the this Graphics class of java. I would greatly appreciate your any help over this.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is the use of Graphics class?

    The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images. A Graphics object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the various properties. Coordinates are infinitely thin and lie between the pixels of the output device.

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

    Re: What is the use of Graphics class?

    Hi friend,

    Following example of Graphics class will depict you the use of Graphics class of java:
    Code:
    import java.awt.Graphics;
    import javax.swing.JFrame;
    
    public class GraphicsDemo1 extends JFrame 
    {
    
      public static void main(String[] abt)
    {
        MainClass fB = new MainClass();
        fB.setSize(300,300);
        fB.setVisible(true);
      }
     
      public void paint(Graphics gB) 
    {
        gB.drawRect(10, 10, 60, 50);
        gB.fillRect(100, 10, 60, 50);
        gB.drawRoundRect(190, 10, 60, 50, 15, 15);
        gB.fillRoundRect(70, 90, 140, 100, 30, 40);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is the use of Graphics class?

    If you want to use the properties of Graphics class, then need to be aware about the below it's methods:
    • setClip(int xh, int yh, int width1, int height1)
    • hitClip(int xh, int yh, int width1, int height1)
    • getFontMetrics(Font fnt)
    • getClipBounds(Rectangle rct)
    • fillRoundRect(int xh, int yh, int width1, int height1, int arcWidth1, int arcHeight1)

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: What is the use of Graphics class?

    Hi,

    In below java code I have used the methods of Graphics class along with different parameters:
    Code:
    import java.awt.Graphics;
    import java.awt.geom.RoundRectangle2D;
    import javax.swing.JFrame;
    
    public class GraphicsDM extends JFrame {
      Shape shps[] = new Shape[5];
      public static void main(String aprgs[]) {
        Draw2DObjects app = new Draw2DObjects();
      }
    
      public Draw2DObjects() {
        add("Center", new MyCanvas());
        shps[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
        shps[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
        shps[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
        GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0, 400.0, 150.0));
        path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
        shps[3] = path;
        shps[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0, 50.0, 25.0);
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
      }
    
      class MyCanvas extends Canvas {
        public void paint(Graphics graphics) {
          Graphics2D g1 = (Graphics2D) graphics;
          for (int i1 = 0; i1 < shps.length; ++i1) {
            if (shps[i1] != null)
              g1.draw(shps[i]);
          }
        }  }
    }

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

    Re: What is the use of Graphics class?

    The graphics class is used for the encapsulation of 'Graphics object'. Following are some properties which are associated with this object:
    1. current text font
    2. translation origin used for clipping as well as rendering the coordinates.
    3. current XOR alternation color
    4. Component object
    5. current color

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. What is the difference between Local class and global class in C++?
    By Dëfrim in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:44 PM
  3. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  4. Ultra solid drives:Imation M-Class and S-Class
    By Regina in forum Portable Devices
    Replies: 1
    Last Post: 03-04-2009, 10:34 AM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

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,775,917.67046 seconds with 16 queries