line.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 24 行
JAVA
24 行
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 + =
减小字号Ctrl + -
显示快捷键?