Java interface Programming-Create a simple framework that can draw graphics

Source: Internet
Author: User

Introduction: A total of 3. Java files are used to create a simple interface programming framework.

1th file: Nothelloworldcomponent.java

//nothelloworldcomponent.java
1 Importjava.awt.*;2 Importjava.awt.geom.*;3 Importjavax.swing.*;4 5 Public classNothelloworldcomponentextendsJComponent {6 Public Static Final intmessage_x = 75; Top left corner of message coordinates7 Public Static Final intMessage_y = 75;8
Define the default dimensions for this interface9 Private Static Final intDefault_width = 300; Ten Private Static Final intDefault_height = 300; One
Paintcomponent () is automatically called by the system, and the system automatically passes a graphics object to it A Public voidpaintcomponent (Graphics g) { -Transforms G into a Graphics2D object, used as a brush to draw 2D graphics -graphics2d g2 =(graphics2d) G; the
Create a Rectangle2D Rectangle object
If there are other shapes that need to be drawn, you can define them here first, and then draw them in the next section with G2 as brushes. - DoubleLEFTX = 50; - DoubleTopy = 50; - Doublewidth = 200; + DoubleHeight = 150; -Rectangle2D rect =Newrectangle2d.double (LEFTX, topy, width, height); +
Draw text and rectangular shapes with brush G2 AG2.drawstring ("Not a Hello, the World program", message_x, message_y); at G2.draw (rect); - - } -
Override this function to set the preferred size - @Override - PublicDimension getpreferredsize () { in return NewDimension (Default_width, default_height); - } to}

2nd. java File: Nothelloworldframe.java

 1  import   Javax.swing.JFrame;  2  3  public  class  nothelloworldframe extends  Span style= "COLOR: #000000" > JFrame { 4   Public   Nothelloworldframe () { 5  add (new   nothelloworldcomponent ());     6  pack (); //  7  }  8  }

Description: JFrame is a framework class that is used to include JPanel and JComponent.

3rd. java File: Nothelloworld.java

1 ImportJava.awt.EventQueue;2 3 ImportJavax.swing.JFrame;4 5  Public classNothelloworld {6      Public Static voidMain (string[] args) {7Eventqueue.invokelater (NewRunnable () {///using Anonymous internal class technology, create a thread that uses threads to create a window program8              Public voidrun () {9JFrame frame =Newnothelloworldframe ();TenFrame.settitle ("Nothelloworldframe");  One frame.setdefaultcloseoperation (jframe.exit_on_close); Close the window while the program ends, avoiding the memory garbage AFrame.setvisible (true); Open Window Visible -             } -         }); the     } -}

Description: For simple use of anonymous inner classes, see this blog: http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html

Java interface Programming-Create a simple framework that can draw graphics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.