java drawing principle ------ draw one or more pictures on the window interface (or panel) problem solving method

/**

 *@author blovedr

 * Function: java drawing principle ------ draw one or more pictures on the window interface (or panel) problem solution

 * Date: April 28, 2018 16:20

 * Note: The little records of learning java, welcome all the great gods to criticize, guide and communicate.

 */

 

package com.test1;

import javax.swing.*;

import java.awt.*;

 

 

public class Demo_1 extends JFrame{

      

       MyPanel mp = null; //Self Note: Define MyPanel

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Demo_1 demo_1 = new Demo_1();

 

       }

      

       public Demo_1()

       {

           mp = new MyPanel(); //Self note: Create an object of MyPanel class mp

          

           this.add(mp); //Self note: Add mp to this(JFrame)

          

           this.setSize(400, 300);

           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Self note: To prevent memory leaks, set the default operation of the window close button (exit the process when you click close)

           this.setVisible(true); //Self note: set the form to be visible

          

       }

 

}

 

//Define a MyPanel (my own panel, which is the area for drawing and displaying drawing)

class MyPanel extends JPanel

{

    //Override the paint method of JPanel

       //Graphics is an important class for drawing, you can understand it as a brush

       public void paint(Graphics g)

       {

         //1. Call the parent class function to complete the initialization (task)

              //This sentence, can not be less

              super.paint(g);

              System.out.println("paint is called");

              // first draw a circle

              //g.drawOval(10, 10, 30, 30);

             

              // draw a straight line

              //g.drawLine(10, 10, 40, 40);

             

              // draw a rectangle border

              //g.drawRect(10, 10, 40, 60); //Self note: g.drawRect(x, y, width, height); ---x and y represent the coordinates of the upper left corner of the rectangle

             

           // fill rectangle

              //set color

//           g.setColor(Color.blue);

//           g.fillRect(10, 10, 40, 60);

//           g.setColor(Color.red);

//           g.fillRect(70, 70, 40, 60);

 

             

              // draw the picture on the panel --- problem

              //Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/hua.jpg"));  //error

              //Self note: Toolkit is a class, it has a static method called getDefaultToolkit() (to get the set of its tools), and then it has a getImage

              // Panel.class.getResource() ---Use the getResource() method in the Panel class to load the image into the object m.

              // Solution: Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/hua.jpg"));--- put " Panel.class.getResource("/hua.jpg")" Replace it with the path of the image , //                    as in the following method 1_ example, you can draw one or more images on the panel .

             

              // Draw a picture on the panel Problem solution 1_Example 1---Three flowers belong to different paths (hua, hua2, hua3 belong to a single picture in different folders)

              //Image im = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua\\hua.jpg");  //正确   2018.4.28  11:29

             

              //Draw a picture on the panel Problem solution 1_Example 2---hua2 and hua belong to different paths

              Image im2 = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua2\\hua2.jpg");  //正确   2018.4.28  15:18

             

              //Draw a picture on the panel Problem solution 1_Example 3---hua3 and (hua and hua2) belong to different paths

              //Image im3 = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua3\\hua3.jpg");  //正确   2018.4.28  15:29

             

             

              // Draw a picture on the panel Problem solution 2_Example---Three flowers belong to the same folder (hua, hua2, hua3 belong to a single picture in the same folder)

              //Image im = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua\\hua.jpg");

              //Image im2 = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua\\hua2.jpg"); //error Self Note: If the three flowers belong to the same file, the program is in this program The picture alone cannot be displayed properly.

              //Image im3 = Toolkit.getDefaultToolkit().getImage("D:\\picture\\hua\\hua3.jpg");

             

              //show

              //g.drawImage(im, 90, 90, 100, 50, this); //Example 1 //Correct 2018.4.28 11:29

             

              g.drawImage(im2, 90, 90, 100, 50, this); //Example 2 //Correct 2018.4.28 15:18

             

              //g.drawImage(im3, 90, 90, 100, 50, this); //Example 3 //Correct 2018.4.28 15:29

             

              //自注: g.drawImage(im, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer)

              // dx1, dy1 represent the upper left corner of the picture you draw, dx2, dy2 represent the width and height of the picture you use (dx2, dy2 --- width, height), this represents the JPanel itself.

             

       }

 

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325082159&siteId=291194637