Tuesday, December 20, 2011

Drawing 2D graphics in java

In this post, i show how to create 2D graphics in java step-by-step.

Create a class MyPaint.java and extend it from JFrame class.

Paint.java
import javax.swing.*;
public class MyPaint extends JFrame
{
 public static void main(String args[])
{

}
}

Create an object for MyPaint class

Paint.java

import javax.swing.*;
public class MyPaint extends JFrame
{
 public static void main(String args[])
{
      MyPaint paint=new MyPaint();
}

}

Now initialize the class by creating constructor .


 MyPaint.java

public class MyPaint extends JFrame
{

public MyPaint()
{
      // to visible the frame
      setVisible(true);

        //to set the size        
        setSize(400,400);

        //to set the title
        setTitle("My Paint");
       
        //close the window when click on the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 public static void main(String args[])
{
      MyPaint paint=new MyPaint();

}

}

If you compile and run the program, you will see the output as follows.



Now create a class MyPanel.java extends JPanel class. We draw all the graphics in this class.

MyPanel.java

import javax.swing.*; //for JPanel class
import java.awt.*;   // for Color class
public class MyPanel extends JPanel
{
 public MyPanel()
{
 // set the Panel's background color
setBackground(Color.white);
}
}

Now add this class to our MyPaint class.

MyPaint.java


public class MyPaint extends JFrame
{

public MyPaint()
{
      // to visible the frame
      setVisible(true);


        //to set the size        
        setSize(400,400);

        //to set the title
        setTitle("My Paint");
       
        //close the window when click on the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

 public static void main(String args[])
{
      MyPaint paint=new MyPaint();

// creating MyPanel class object
    MyPanel panel=new MyPanel();


// adding panel to MyPaint class
paint.setContentPane(panel);



}

}

Now we are ready to draw graphics. To draw graphics we use paint() method in our MyPanel class.

MyPanel.java


import javax.swing.*; //for JPanel class

import java.awt.*;   // for Color class

public class MyPanel extends JPanel
{
 public MyPanel()
{
 // set the Panel's background color
setBackground(Color.white);

}

public void paint(Graphics g)
{
       // to set the brush color.
        g.setColor(Color.green);
      // to draw a Rectangle
       g.drawRect(10,50,70,50);
      
        g.setColor(Color.red);
       // to draw an Oval or circle
        g.drawOval(100,80,50,50);

        g.setColor(Color.blue);
        //to draw a filled Rectangle
        g.fillRect(200,150,50,50);

        g.setColor(Color.yellow);
        // to draw a filled Oval
        g.fillOval(160,80,70,70);
      
        g.setColor(Color.black);
        // to draw a filled Arc
        g.fillArc(90,190,150,150,360,160);

}
}

Now compile and run the program,you can see the output as follows.

 
 

1 comment:

  1. After I draw the graphics the background color changes to grey. Why is that?

    ReplyDelete