zoukankan      html  css  js  c++  java
  • e575. The Quintessential Drawing Program

    To draw on the screen, it is first necessary to subclass a JComponent and override its paint() method. The paint() method is automatically called by the windowing system whenever component's area needs to be repainted.

    The paint() method is supplied a graphics context which is used to draw shapes and images. The coordinate system of a graphics context is such that the origin is at the northwest corner and x-axis increases toward the right while the y-axis increases toward the bottom.

    This example defines a component that draws an oval and installs an instance of this component in a frame. See also e586 Drawing Simple Shapes.

        import java.awt.*;
        import javax.swing.*;
        
        public class BasicDraw {
            public static void main(String[] args) {
                new BasicDraw();
            }
            BasicDraw() {
                // Create a frame
                JFrame frame = new JFrame();
        
                // Add a component with a custom paint method
                frame.getContentPane().add(new MyComponent());
        
                // Display the frame
                int frameWidth = 300;
                int frameHeight = 300;
                frame.setSize(frameWidth, frameHeight);
                frame.setVisible(true);
            }
        
            class MyComponent extends JComponent {
                // This method is called whenever the contents needs to be painted
                public void paint(Graphics g) {
                    // Retrieve the graphics context; this object is used to paint shapes
                    Graphics2D g2d = (Graphics2D)g;
        
                    // Draw an oval that fills the window
                    int x = 0;
                    int y = 0;
                    int width = getSize().width-1;
                    int height = getSize().height-1;
                    g2d.drawOval(x, y, width, height);
                }
            }
        }
    
    Related Examples
  • 相关阅读:
    OSCP Learning Notes Exploit(7)
    正则表达式中?=和?:和?!的理解
    提取日志中的ip
    ip地址的正则表达式
    linux内核tmpfs/shmem浅析
    记一个linux内核内存提权问题
    linux内存屏障浅析
    linux IPv4报文处理浅析
    linux会话浅析
    linux memory lock浅析
  • 原文地址:https://www.cnblogs.com/borter/p/9575642.html
Copyright © 2011-2022 走看看