Drawing in a window
Drawing panel
Swing components in Java don't have to be rendered in just one color. Using them, it is possible to draw both simple geometric shapes (points, lines, circles, ellipses, rectangles) and complex geometric figures (polylines, complex open and closed curved lines). In other words, you can draw a drawing by any type of component (JPanel, JButton, etc.).
In order to get a component from a component, which is in one color, e.g. a panel with a drawing, that component (the panel in this example) must belong to the class that inherits the JComponent or one of the classes that previously inherited the JComponent class, such as the JPanel class. Also in this class the legacy paint method (found in the JComponent class) must be modified.This method is called automatically when drawing the graphic for the first time, or when resizing the window, and can also be called from code:
repaint ();
The paint method gets a Graphics class object, which contains the methods needed for drawing. By calling the appropriate method, a particular shape is drawn. For example. To draw an ellipse inscribed in a rectangle 400 in width and 300 in height, which from the beginning is 200 along the X axis and 100 along the Y axis, the paint method would be:
repaint ();
The paint method gets a Graphics class object, which contains the methods needed for drawing. By calling the appropriate method, a particular shape is drawn. For example. To draw an ellipse inscribed in a rectangle 400 in width and 300 in height, which from the beginning is 200 along the X axis and 100 along the Y axis, the paint method would be:
@overriding
public void paint(Graphics g)
{
public void paint(Graphics g)
{
g.drawOval(200,100,400,300);
}Here g represents a drawing tool, i. an object that contains drawing methods. This paint method is an overriding method of one of the inherited classes, in this case the JComponent class. The JPanel class inherits the JComponent, and this repainted paint method must be in the class that inherits the JPanel class:
public class Drawing extends JPanel
{
{
@overriding
public void paint(Graphics g)
{
}public void paint(Graphics g)
{
// Some code
}EXAMPLE: Geometric figures:
Assignment task:
- Draw a square 400x400 at distances 200 along the X and 200 along the Y axis ..
- Draw a diagonal of the square
- Insert a circle in the square
Solution:
The object by which the drawing will be drawn belongs to the class Drawing that inherits the JPanel class. If we right-click in the body of such an empty class and select Insert Code -> Override method on the context menu we will get inherited classes and in them methods that can be modified:
The paint drawing method is in the JComponent class, so it should also be selected by clicking the Generate button in the class:
After entering, the following method is inserted:
public class Drawing extends JPanel
{
{
@Override
public void paint(Graphics g)
{
}public void paint(Graphics g)
{
super.paint(g);
}Figure 3: Drawing in a window. Overriding the paint method
Inside the code is inserted:
super.paint (g);
which invokes the inherited paint method. This can be used to draw the background color of the inherited panel.
The drawing to be obtained looks like the image below:
To draw this code in the paint method it looks like:
public class Drawing extends JPanel
{
{
@Override
public void paint(Graphics g)
{
}public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.blue);//determines the color to draw
/*
* draws a 400x400 square on the coordinate (100,100)
*/
g.drawRect(100, 100, 400, 400);
g.drawLine(100, 500, 500, 100);//diagonal squares
g.drawOval(100, 100, 400, 400);//draws a circle
}g.setColor(Color.blue);//determines the color to draw
/*
* draws a 400x400 square on the coordinate (100,100)
*/
g.drawRect(100, 100, 400, 400);
g.drawLine(100, 500, 500, 100);//diagonal squares
g.drawOval(100, 100, 400, 400);//draws a circle
Figure 5: Drawing in a window. Geometric forms-code.
First, the color for drawing to blue is set using the setColor method of the Graphics class object obtained as a method parameter. The square is then drawn using the drawRect method that looks for 4 parameters. The first two represent the X and Y coordinates of the upper left corner of the rectangle relative to the upper left corner of the panel, where the origin is located, while the third and fourth parameters represent the width and height of the rectangle. Since it is a rectangle of equal width and height, it is actually a square.
The drawLine method draws a line between points (100,500) and (500,100).
The drawOval method draws an ellipse (circle) that is written in a rectangle (square) whose parameters are identical to the parameters of the drawRect method.
These figures can be drawn in another way by creating a particular geometric shape as an object of a particular class from the API that implemented the Shape interface. The draw method is then invoked and forwarded to the created drawing form. This method does not exist in the Graphics class but in the inherited Graphics2D class from the java.awt package.
The drawLine method draws a line between points (100,500) and (500,100).
The drawOval method draws an ellipse (circle) that is written in a rectangle (square) whose parameters are identical to the parameters of the drawRect method.
These figures can be drawn in another way by creating a particular geometric shape as an object of a particular class from the API that implemented the Shape interface. The draw method is then invoked and forwarded to the created drawing form. This method does not exist in the Graphics class but in the inherited Graphics2D class from the java.awt package.
Graphics2D class in Java
The classes that can be created by various drawing objects implemented by the Shape interface are in the java.awt.geom package. We use Ellipse2D, Line2D and Rectangle2D classes to implement circle, diagonal, and square objects. Within a class, these objects are first declared as attributes of the class Drawing1, while objects are implemented in the constructor:
public class Drawing1 extends JPanel
{
{
/*
* Shapes
*/
Ellipse2D circle;
Rectangle2D square;
Line2D diagonals;
public Drawing1() {
}* Shapes
*/
Ellipse2D circle;
Rectangle2D square;
Line2D diagonals;
public Drawing1() {
//creating circle
circle = new Ellipse2D.Double(100, 100, 400, 400);
//creating diagonals
diagonals = new Line2D.Double(100, 500, 500, 100);
//creating squares
square = new Rectangle2D.Double(100, 100, 400, 400);
}circle = new Ellipse2D.Double(100, 100, 400, 400);
//creating diagonals
diagonals = new Line2D.Double(100, 500, 500, 100);
//creating squares
square = new Rectangle2D.Double(100, 100, 400, 400);
Figure 6: Drawing in a window. Graphics2D. Creating geometric shape-codes.
All three classes have the Double and Float variants and it is obvious that the Double variant was used in this case. All 4 parameters of circle and square are identical to those of drawOval methods. drawRect of the Graphics object in the first variant of the task.
The paint method now looks like:
The paint method now looks like:
public class Drawing1 extends JPanel
{
{
/*
* Shapes
*/
Ellipse2D circle;
Rectangle2D square;
Line2D diagonals;
public Drawing1() {
@Override
public void paint(Graphics g)
{
}* Shapes
*/
Ellipse2D circle;
Rectangle2D square;
Line2D diagonals;
public Drawing1() {
//creating circle
circle = new Ellipse2D.Double(100, 100, 400, 400);
//creating diagonals
diagonals = new Line2D.Double(100, 500, 500, 100);
//creating squares
square = new Rectangle2D.Double(100, 100, 400, 400);
}circle = new Ellipse2D.Double(100, 100, 400, 400);
//creating diagonals
diagonals = new Line2D.Double(100, 500, 500, 100);
//creating squares
square = new Rectangle2D.Double(100, 100, 400, 400);
@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D) g;//kastovanje
g2.setColor(Color.BLUE); //sets the drawing color to blue
g2.fill(circle); //draws circle
g2.draw(diagonals); //draws a diagonal
g2.draw(square); //draws a square
}Graphics2D g2=(Graphics2D) g;//kastovanje
g2.setColor(Color.BLUE); //sets the drawing color to blue
g2.fill(circle); //draws circle
g2.draw(diagonals); //draws a diagonal
g2.draw(square); //draws a square
Figure 7: Drawing in a window. Geometric forms-code. Drawing using the Paint method
Objects g2 and g differ in type, so casting must be performed. This class has the same graphics as the Drawing class. If we wanted to fill the circle with color, for example, we would use the fill method instead of draw:
g2.fill(krug);
After launch, the application would look like:
g2.fill(krug);
After launch, the application would look like:
We associate the panel class Drawing or Drawing1 with the main window in the main class:
public class DrawingInWindow extends JFrame
@Override
public DrawingInWindow()
{
}
@Override
public static void main(String[]args)
{
public DrawingInWindow()
{
@Override
public static void main(String[]args)
{
DrawingInWindow win = new DrawingInWindow();
win.setSize(600, 600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Drawing1 drawing = new Drawing1();
win.setContentPane(drawing);
win.setVisible(true);
}win.setSize(600, 600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Drawing1 drawing = new Drawing1();
win.setContentPane(drawing);
win.setVisible(true);
Figure 9: Drawing in a window. Geometric shapes. Complete graphic code
The main class (DrawingInWindow) inherits the JFrame, so the win object, which is actually the main window of the application, is the DrawingInWindow class.
Class | Metods | Description |
---|---|---|
java.awt.Graphics | drawRect(int x,int y, int w, int h) | Draws a rectangle whose upper left corner is in coordinates (x,y), w- width h-height |
java.awt.Graphics | drawLine(int x1,int y1, int x2, int y2) | Draws a line from the point (x1,y1), to the point (x2,y2) |
java.awt.Graphics | drawOval(int x,int y, int w, int h) | Draws an ellipse whose upper left corner is the rectangle described around the ellipse in coordinates (x,y), w- width h-height |
java.awt.Graphics | drawRoundRect(int x,int y, int w, int h,int arcW, int arcH) | Draws a rectangle with rounded edges whose upper left corner is in coordinates (x,y), w- width h-height arcW-horizontal radius diameter all 4 angles arcH-vertical radius diameter all 4 angles |
java.awt.Graphics | drawArc(int x,int y, int w, int h, int pocU, int krajU) | Draws a circular arc whose upper left corner is at coordinates(x,y), w- width h-height pocU-the initial angle of the arch krajU-the far corner of the arch |
java.awt.Graphics | drawString(String str, int x,int y) | Draws a String str inside a rectangle whose upper left corner is in coordinates (x,y), |
java.awt.Graphics2D | draw(Shape shape) | Draws an object whose upper left corner is a rectangle described around an ellipse in coordinates (x,y), w- širina h-visina |
java.awt.Graphics2D | fill(Shape shape) | Colors an object whose upper left corner is a rectangle described around an ellipse in coordinates(x,y), w- širina h-visina |
java.awt.Graphics | fillRect(int x,int y, int w, int h) | Colors a rectangle whose upper left corner is in coordinates (x,y), w- širina h-visina |
java.awt.Graphics | fillOval(int x,int y, int w, int h) | Paints an ellipse whose upper left corner is a rectangle described around an ellipse in coordinates (x,y), w- širina h-visina |
Previous
|<Events in Java |
Next
Creating a GUI example>| |