This article describes the example code that implements the functions of drawing lines, rectangles, ellipses, and strings in Java. Share it for your reference, as follows:
import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; /** * Add window drawing lines, rectangles, ellipses, and strings* @author Lixiangjian * */ public class BallGame extends Frame{ //Image sun represents the obtained image path Image sun =Toolkit.getDefaultToolkit().getImage("images/3.jpg");//This method is not well written, but the simplest/** * When painting the window, it will be automatically called (non-Javadoc) * Graphics g is equivalent to a pen* g.drawImage Draws an image: Draw a sun image, with respect to the window position x=100, y=100, null always be null*/ public void paint(Graphics g){ g.drawImage(sun, 100, 100, null); //Draw a line<span style="color:#FF0000;">g.drawLine(100, 100, 200, 200); g.setColor(Color.BLUE);</span> //Draw rectangle g.drawRect(50, 50, 100, 90); //Draw ellipse g.drawOval(50, 50, 100, 100); g.setColor(Color.yellow); //Draw string on the window g.drawString("Java self-study time start", 50, 80); } void launchFrame(){ //Set the window size setSize(300,300); //SetLocation() is a method in JFrame, setting the position in the initialization of the control in the form setLocation(150, 150); //Set the background color of the window // setBackground(Color.blue); //Set the window title setTitle("Li Moumou"); //Set whether to display setVisible(true); } public static void main(String[] agrs){ System.out.println("Li Moumou Java Exercise"); //Create a class in the main function and call the startup window new BallGame().launchFrame(); } } The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.