清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
java画矩形需要使用Rectangle2D.Float静态方法,指定四个参数即可,四个参数分别为左上角的xy坐标,以及矩形的宽度和高度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; /** * Displays a JFrame and draws a ractangle 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 rectangle is drawn. * * @param g The graphics object */ public void paint(Graphics g) { Graphics2D graphics2 = (Graphics2D) g; Rectangle2D rectangle = new Rectangle2D.Float( 100 , 100 , 240 , 160 ); graphics2.draw(rectangle); } // <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 ); } }); } } |