Java Swing Shape

Java Swing Shape

Java Swing Shape has several classes that am going to explain.To start with is Point class.

Point class

Point class is used to represent a certain location in a two-dimensional space i.e., x and y values which are coordinates.This class  is contained in java.awt package.

Example

[
import java.awt.Point;

public class JavaSwingShape {
    JavaSwingShape (){

        //Creates a point with set location        Point p=new Point(20,30);
        //gets X coordinates        int x=(int)p.getX();
        //gets y coordinates        int y=(int)p.getY();
        //creates empty point         Point point=new Point();
        //sets point location        point.setLocation(20,30);

    }
}
]

Below codes creates a JButton Swing components and sets its location using Point.
[
import java.awt.Point;
import javax.swing.JButton;

public class JavaSwingShape {
    JavaSwingShape (){
        JButton button=new JButton("Save");
        button.setLocation(new Point(20,25));
        //below code does the same        button.setLocation(20,25);


    }
}
]

Dimension Class

Dimension class is used to specify component height and width.Its contained in package java.awt.
Example.
[
import javax.swing.JButton;
import java.awt.Dimension;

public class JavaSwingShape {
    JavaSwingShape (){
        JButton button=new JButton("Save");
        Dimension d=new Dimension(20,40);
        button.setSize(d);
        //below code does the same task       
         button.setSize(20, 40);


    }
}
]
We can now get the JButton size using Dimension class.
[

import javax.swing.JButton;
import java.awt.Dimension;

public class JavaSwingShape {
    JavaSwingShape ()
    {
        JButton button=new JButton("Save");
        button.setSize(20, 40);

        Dimension d=new Dimension();
        int width=button.getWidth();
        int height=button.getHeight();

        System.out.println("Width is "+width +"  Height is" + height);


    }
    public static void main(String args[])
    {
        new JavaSwingShape();
    }
}
]

Inset Class.

Inset class is used to represent spaces in between a component,i.e left,right ,top and bottom.This class is contained in java.awt package.
[Insets inset = new Insets(20, 5, 5, 5);]

Its pretty easy to get JFrame insets.
[
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;

public class JavaSwingShape {
    JavaSwingShape ()
    {
        JFrame frame=new JFrame();
        Insets insets=frame.getInsets();
        int top=insets.top;
        int bottom=insets.bottom;
        int left=insets.left;
        int right=insets.right;


    }
    public static void main(String args[])
    {
        new JavaSwingShape();
    }
}
]

Rectangle class

java.awt rectangle class is used to represent rectangle shape.A rectangle has four points i.e width,height,x and y coordinates.

Different Rectangle class constructors.

[
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Dimension;

public class JavaSwingShape {
    JavaSwingShape ()
    {

        //creates a rectangle with X and Y coordinates        
 //set at zero,height and width zero value     
   Rectangle r1  = new Rectangle();
        //creates rectangle object using Point object as x and y coordinates       
 //with zero height and width values       
Rectangle r2  = new Rectangle(new Point(15, 15));

        //creates rectangle object using Point object as x and y coordinates      
  //with height and width set by Dimension object     
   Rectangle r3  = new Rectangle(new Point(10,  10),  new Dimension(200, 100));
        //creates a rectangle with set values of x,y,width and height   
     Rectangle r4  = new Rectangle(10,10,200,100);


    }
    public static void main(String args[])
    {
        new JavaSwingShape();
    }
}
]







COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java Swing Shape
Java Swing Shape
Java Swing Shape
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-swing-shape.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-swing-shape.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy