📄 grids.java
字号:
/*
public class Canvasextends Component implements Accessible
A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.
An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.
*/
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Program to draw grids.
*
* @author Ian Darwin, http://www.darwinsys.com/
*/
class GridsCanvas extends Canvas {
int width, height;
int rows;
int cols;
GridsCanvas(int w, int h, int r, int c) {
setSize(width = w, height = h);
rows = r;
cols = c;
}
public void paint(Graphics g) {
int i;
width = getSize().width;
height = getSize().height;
// draw the rows
int rowHt = height / (rows);
for (i = 0; i < rows; i++)
g.drawLine(0, i * rowHt, width, i * rowHt);
// draw the columns
int rowWid = width / (cols);
for (i = 0; i < cols; i++)
g.drawLine(i * rowWid, 0, i * rowWid, height);
}
}
/** This is the demo class. */
public class Grids extends Frame {
/*
* Construct a GfxDemo2 given its title, width and height. Uses a
* GridBagLayout to make the Canvas resize properly.
*/
Grids(String title, int w, int h, int rows, int cols) {
setTitle(title);
// Now create a Canvas and add it to the Frame.
GridsCanvas xyz = new GridsCanvas(w, h, rows, cols);
add(xyz);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
// Normal end ... pack it up!
pack();
}
public static void main(String[] a) {
new Grids("Test", 300, 300, 5, 10).setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -