Java simple graphics drawing and description of common problems

Foreword:

In the process of learning java, I recently came into contact with content related to graphics drawing. In order to complete the homework in my studies, I read many articles and programs from big guys. Now that the homework is done, in order to make what I have learned recently belong to me, I decided to sort it out. If what I wrote is wrong, welcome to criticize and correct me.

Regarding the choice of graphics container for simple drawing:

First of all, there is a premise to be clear. When we draw graphics, we naturally call the Graphics class to draw. But we should find a container to hold the drawn graphics. Therefore, the available containers are the subclasses Panel and Canvas of awt. Canvas can be understood as canvas, which is a kind of container. The Panel class is also a container, both of which can accommodate Graphics objects, but the Panel class can accommodate more types of objects. Of course, when we simply draw simple graphics, there is no problem using the Canvas class. But using the Panel class, the obvious program plasticity and maintainability will become stronger. In java, there is also Javax.swing.JPanel to expand the Panel. When we first started learning, Panel was actually enough.

import javax.swing.JFrame;//框架
import javax.swing.JPanel;//容器
import java.awt.Canvas;

 About the relationship between Graphics and Graphics2D:

Java has extended Graphics, that is to say, Graphics2D is an extension of Graphics. When we use Graphics, we often see their forced conversion. Convert Graphics to Graphics2D type for use, and I often do this when writing programs.

 Graphics2D gx=(Graphics2D) g;

 About commonly used Graphics graphics drawing tools:

Draw straight lines:

abstract void drawLine(int x1,int y1,int x2,int y2) Call this function to draw a straight line from (x1,y1) to (x2,y2)

	gk.drawLine(gw*i,0,gw*i,180);

Draw an ellipse (circle):

abstract void drawOval(int x, int y, int width, int height) Call this function to draw a hollow ellipse. The formal parameter represents the information of the bounding rectangle of this ellipse. x, y represent the coordinates of the upper left corner of the bounding rectangle, and width and height are the width and height of the bounding rectangle. On this basis, change the line, change the outer rectangle into a square, and then draw a circle.

public abstract void fillOval(int x, int y, int width, int height) Call this function to draw a filled ellipse. Other parameter information is consistent with the function of drawing a hollow ellipse.

gk.drawOval(20,20,20,20);//空心圆
gk.fillOval(80,80,20,20);//实心圆

Draw a rectangle:

abstract void drawRect(int x, int y, int width, int height ) Call this function to draw a rectangle. x, y are the coordinates of the point in the upper left corner of the rectangle, width and height are the width and height of the rectangle

abstract void fillRect(int ,x, int y, int width, int height ) Call this function to draw a filled rectangle. Similarly x, y is a point in the upper left corner of the rectangle, while width and height are the width and height of the rectangle

	gx.fillRect(x,y,20,20);//绘制方形
    gx.drawRect(x,y,20,20);//绘制空心矩形

Of course, we also have other functions for drawing images. From this point of view, java is still very convenient. For other functions, you can check the jdk manual, which is simple and convenient.

Set line color:

We often encounter the need to set the color of the drawn graphics. Without color, graphics will inevitably appear monotonous when drawn. Our graphics just happen to have a function to set the line color.

abstract void setColor( Color c ) Calling this function can set the line color, which is actually equivalent to setting the color of the brush when painting. The default brush color, which can be considered black. The formal parameter is actually the object of Color. And we set the encapsulated color objects in Color, such as Color.RED, Color.BLUE. We can also manually set the RGB color object. Having said that, we have to mention that when using the Color object, we need to reference the Color library. Color is a library belonging to awt. Its reference code can be written like this.

import java.awt.Color;//色彩

 We pre-set the color of the brush before drawing, and then draw the image to get the image of the color we want. As for the drawing of solid graphics, the same is true. We just need to understand the brush as paint.

Graphics2D gx=(Graphics2D) g;
gx.setColor(Color.GREEN);//改变画笔颜色
gx.fillOval(x,y,20,20);//绘制圆形

 A drawing example:

I personally feel that this example may not be very good for this module. Because this question involves the interaction with the mouse, as an example of simply explaining image drawing, there may be interference. But I don't have a good example at hand for now. To put it bluntly, the author is too lazy to pre-write the code suitable for the example. Readers, please pay attention.

Write a Java program, create a form, and achieve interaction. "Click the left mouse button" in the form, draw a "green circle" at the cursor; "click the right mouse button" in the form, draw a "red box" at the cursor; key” to clear all the drawn “circles” and “squares”.

I won’t take it out in other places, I will take out the part where I set the graphics and share it

class MyJpanel extends JPanel{
	int x,y;
	int num=0;
	public void paint(Graphics g){
		Graphics2D gx=(Graphics2D) g;
		if(num==1){
			gx.setColor(Color.GREEN);//改变画笔颜色
			gx.fillOval(x,y,20,20);//绘制圆形
		}//单击左键
		else if(num==3){
			gx.setColor(Color.WHITE);
			super.paint(gx);//清除画面内容
		}//双击左键
		else if(num==2){
			gx.setColor(Color.RED);
			gx.fillRect(x,y,20,20);//绘制方形
		}//单击右键
	}
}//容器

Guess you like

Origin blog.csdn.net/qq_58754996/article/details/120678554