使用 java.awt.Graphics2D 画线
2018-07-20 来源:open-open
使用 java.awt.Graphics2D 画线,需要使用Line2D.Float方法,该方法的四个参数分别为起点的坐标x,y和终点的坐标x,y
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; /** * Displays a JFrame and draws a line on it using the Java 2D Graphics API * * @author cn.outofmemory */ public class Java2DFrame extends javax.swing.JFrame { /** * Creates a new instance of Java2DFrame */ public Java2DFrame() { initComponents(); } /** * This is the method where the line is drawn. * * @param g The graphics object */ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Line2D lin = new Line2D.Float(100, 100, 250, 260); g2.draw(lin); } // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * Starts the program * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Java2DFrame().setVisible(true); } }); } }
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:java冒泡排序算法
最新资讯
热门推荐