Java Swing Shape
Java Swing Shape has several classes that am going to explain.To start with is Point class.
import javax.swing.JButton;
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(); } }
]