📄 line.java
字号:
import javax.swing.*;import java.awt.*;public class Line extends JComponent { private boolean isMajorDiagonal; public Line(int x1, int y1, int x2, int y2) { super(); setBounds(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2)+1, Math.abs(y1-y2)+1); setBackground(Color.black); isMajorDiagonal = (x1==Math.min(x1,x2) && y1==Math.min(y1,y2)) || (x2==Math.min(x1,x2) && y2==Math.min(y1,y2)); } /** postconditon this method draws a line segment from point (getX(),getY()) to point (x2, y2) in the background coordinate system and with color from getBackground() */ public void paint(Graphics g) { g.setColor( getBackground() ); if (isMajorDiagonal) g.drawLine(0, 0, getWidth()-1, getHeight()-1); else g.drawLine(0, getHeight()-1, getWidth()-1, 0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -