It's easy to get started with a Graphic-User-Interface in java using Java Swing. Specifically, you will see JFrame in the starter code below. If you look at the constructor, you will find the majority of the code necessary to get a JFrame panel displayed on your screen. The use of the JPanel is necessary to override the paint method which this setup uses to give us access to drawing tools.

The other parts of this project will have you work with the Graphics class which gives us access to simple drawing tools such as drawing ovals and rectangles. You will spend the majority of your time in the paint method which has 1 parameter (argument), a Graphics object.

This singular file project contains Frame.java which also has a main method to start the program. This is done to simplify the project. If you are not using eclipse as your IDE, you should be able to easily create a project with one class (Frame.java), and you would only need to copy-paste the code below to get going.

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JPanel {
public void paint(Graphics g) {
g.fillOval(0, 0,50,50);
 }

public Frame(String title) {
JFrame j = new JFrame(title);
j.setSize(800,600);
j.add(this);

//make sure the x button on the JFrame exits the program
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
public static void main(String[] args) {
Frame f = new Frame("Simple Drawing");
 }
}

With the way the project has been set up, the "drawing" is happening in the paint method. When you need to draw or change the color, you will be in this method.

If you wish to use a different color, you must set the color before calling the drawing method. The default color is black. The example below changes the color of the drawing tool for each oval drawn on the screen. The example uses fillRect for drawing a solid rectangle on the screen. The first two arguments are the (x, y ) location of the top-left corner of the rectangle followed by the width and height.

fillRect(x, y, width, height) -> Method for drawing a solid rectangle with its top-left corner at x, y with a given width and height
drawRect(x, y, width, height) -> Draw the outline of a rectangle with its top-left corner at x, y with a given width and height

 public void paint(Graphics g) {
g.setColor(Color.black); //change drawing color
g.fillRect(0, 0, 50, 50); //draw a filled-in rectangle with the top-left corner at 0 and width and height of 50.

g.setColor(Color.blue); //change drawing color
g.fillRect(100, 0, 50, 50);//draw a solid rectangle with the top-left corner located at x = 100 and y = 0.
 }

The drawOval and fillOval methods will draw a circle or an oval that fits within the rectangle specified by x, y, width, and height. This is the reason I introduce drawRect and fillRect first in the previous section!

drawOval( int x, int y, int width, int height )
fillOval( int x, int y, int width, int height )

Syntax:

drawLine(int x1, int y1, int x2, int y2)

Parameters: The drawLine method takes four arguments:

  • x1 – It takes the first point’s x coordinate.
  • y1 – It takes first point’s y coordinate.
  • x2 – It takes second point’s x coordinate.
  • y2 – It takes second point’s y coordinate

Result: This method will draw a line starting from (x1, y1) coordinates to (x2, y2) coordinates.

Copyright © StudyCS.org 2021-2022 Twitter