JAVA learning AWT plotting, javaawt plotting

Source: Internet
Author: User

JAVA learning AWT plotting, javaawt plotting

Package com. graphics; import java. awt. graphics; import javax. swing. JFrame; import javax. swing. JPanel;/*** 1: Graphics class is the abstract base class of all graphic contexts. ** 2: Graphics2D inherits the Graphics class and implements a set of more powerful drawing operations. * Because the Graphics2D class is an extension of the Graphics class and a recommended java drawing class * This chapter mainly introduces how to use the Graphics2D class to implement JAVA plotting. ** 3: the Graphics class uses different methods to implement different plotting * @ author biexiansheng **/public class DrawCircle extends JFrame {private final int OVAL_WIDTH = 80; // The width of the circular private final int OVAL_HEIGHT = 80; // The height of the circular public DrawCircle () {super (); initialize (); // call the initialization method} // The initialization method private void initialize () {this. setSize (300,200); // set the size of the form setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // set the form closing method setContentPane (new DrawPanel (); // set the Form Panel as the drawing panel object this. setTitle ("Drawing instance 2"); // set the Form title} class DrawPanel extends JPanel {public void paint (Graphics g) {super. paint (g); g. drawOval (10, 10, OVAL_WIDTH, OVAL_HEIGHT); // draw 1st circular g. drawOval (80, 10, OVAL_WIDTH, OVAL_HEIGHT); // draw 2nd circular g. drawOval (150, 10, OVAL_WIDTH, OVAL_HEIGHT); // draw 3rd circular g. drawOval (50, 70, OVAL_WIDTH, OVAL_HEIGHT); // draw 4th circular g. drawOval (120, 70, OVAL_WIDTH, OVAL_HEIGHT); // draw 5th circles} public static void main (String [] args) {// TODO Auto-generated method stub DrawCircle dc = new DrawCircle (); // initialize the object and call the constructor dc. setVisible (true); // form visualization }}

The running result of the case is as follows:

 

Package com. graphics; import java. awt. graphics; import java. awt. graphics2D; import java. awt. shape; import java. awt. geom. ellipse2D; import java. awt. geom. rectangle2D; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. windowConstants;/*** 1: Graphics2D is compiled based on the Graphics class. It contains the drawing method of the Graphics class and adds stronger functions. * It is a recommended drawing class, * Graphics2D can use different classes to represent different shapes, such as Line2D and Rectangle2D. ** 2: to draw a graph of the specified Shape, you must first create and initialize an object of the graph type. These graphics classes must be the implementation class of the Shape interface, then use the draw () method of Graphics2D class to draw the image object, or use the fill () method to fill in * The graphic object * syntax is as follows * draw (Shape form) or fill (Shape form) * form indicates the object that implements the Shape interface. * @ Author biexiansheng **/public class DrawFrame extends JFrame {public DrawFrame () {super (); initialize (); // call the initialization method} // The initialization method public void initialize () {this. setSize (300,200); // set the form size // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); add (new CanvasPanel (); // set the Form Panel as the drawing panel object this. setTitle ("Drawing instance 2"); // set the Form title} // create an internal class CanvasPanel extends JPanel {public void paint (Graphics g) {super. paint (g); Graphics2D g2 = (Graphics2D) g; // The Graphics2D type of the forced type conversion bit. // Graphics2D is a recommended drawing class, however, most of the drawing objects provided in the program design are examples of the // Graphics class. In this case, use forced type conversion to convert them to Graphics Shape [] shapes = new Shape [4]; // declare the Image array shapes [0] = new Ellipse2D. double (5, 5,100,100); // create a circular object // create a rectangular object shapes [1] = new Rectangle2D. double (100,100,); // shapes [2] = new Rectangle2D. double (15, 15, 80, 80); // create a rectangle object shapes [3] = new Ellipse2D. double (120, 15, 80, 80); // create a circular object for (Shape shape: shapes) {// traverse the graph array Rectangle2D bounds = shape. getBounds2D (); if (bounds. getWidth () = 80) {g2.fill (shape); // fill image} else {g2.draw (shape ); // Drawing Graphics }}} public static void main (String [] args) {// TODO Auto-generated method stub DrawFrame df = new DrawFrame (); df. setVisible (true );}}

The running result of the case is as follows,

You can set the Color or use g2.setColor (Color. RED) directly );

 

 

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.