How to use the paint method to draw pictures in java
The main content of this article is about using the paint method to draw pictures in Java. It has certain reference value. Interested friends can learn about it. hope it is of help to you.
java uses the paint method to draw pictures
You need to inherit the JFrame class to draw the window=> public class Game extends JFrame {}
setTitle(String s); / /Set window title
setLocation(int x, int y); //Set window position
setSize(int width, int height); //Set window width and height
setVisible(true); // The setting window is visible, and the default is flase. It is better to put this method after setLocation() and setSize. I put it in front and the window is black. The default is white.
paint method drawing
Automatically call after definition
public class paint(Graphics g) { Color c = g.getColor(); //记录原来的颜色 Font f = g.getFont(); //记录原来的字体 g.setColor(Color.BLACK); //设置画线的颜色 g.drawLine(int x1, int y1, int x2, int y2); //两点画直线 g.drawRect(int x, int y, int width, int height); //左上角顶点加宽高画矩形 g.fillRect(int x, int y, int width, int height); //画填充矩形 g.setFont(new Font("楷体", Font.BOLD, 40)); //设置字体为楷体,粗体,大小为40 g.drawString(str, int x, int y); //画出str字符串 g.setColor(c); //变回原来的颜色 g.setFont(f); //变回原来的字体}
GameUtil tool class import image
import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; public class GameUtil { // 工具类最好将构造器私有化。 private GameUtil() { } public static Image getImage(String path) { BufferedImage bi = null; try { URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi; }}
Call GameUtil in Game class
Image imag = GameUtil .getImage("images/picture.png"); //I created an images package to store pictures. The path of the picture is in the quotation marks
g.drawImage(imag, x, y, width, height, null ); //imag picture, position, width and height, observer
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import javax.swing.JFrame; public class MyGame extends JFrame{ Image imag = GameUtil.getImage("images/text1.png"); //指定图片 @Override public void paint(Graphics g) { Color c = g.getColor(); Font f = g.getFont(); g.setColor(Color.BLUE); //设置线体颜色 g.drawLine(100, 100, 650, 100); //直线 g.drawRect(50, 150, 200, 200); //空心矩形 g.fillRect(550, 150, 200, 200); //实体矩形 g.drawOval(300, 150, 200, 200); //圆形 g.setFont(new Font("楷体", Font.BOLD, 90)); //设置字体 g.drawString("How are you?", 100, 100); //写字 g.drawImage(imag, 250, 400, 300, 300, null); //插入图片 g.setColor(c); //线条颜色变为原来的样子 g.setFont(f); //字体变为原来的样子 } public void launchJFrame() { this.setTitle("我的游戏"); //设置窗口标题 this.setSize(800, 800); //设置窗口大小 this.setLocation(100, 100); //设置窗口位置 this.setVisible(true); //设置窗口可见 /*this.addWindowListener(new WindowAdapter() { //叉掉窗口后,结束窗口所在的应用程序 @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); */ this.setDefaultCloseOperation(EXIT_ON_CLOSE); //叉掉窗口后,结束窗口所在的应用程序 } public static void main(String args[]) { MyGame game = new MyGame(); game.launchJFrame(); }}
Set the size of the picture
public Image getScaledInstance(int width, int height, int hints) //hints - a flag indicating the type of algorithm used for image resampling (I don’t know what this sentence means, just write it as follows)
Image img = GameUtil.getImage("images/text1.jpg");img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
If you want to get the picture Size, just use the getWidth() and getHeight() methods
width = img.getWidth();height = img.getheight();
Double buffering technology to solve flickering
The principle is probably: first transfer the required Load the drawn things into the buffer, and then draw all the contents in the buffer to the screen. This can avoid the screen from flashing like crazy because too many things are loaded on the screen.
public void paint(Graphics g){ BufferedImage imag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //构建缓冲区 Graphics g2 = imag.creatGraphics(); //新建一支画笔,使用这支画笔来将内容画到缓冲区中 g2.drawRect(...); //括号里面的参数就不写了,此处用来说明一些画图操作 g2.drawImag(...); g2.fillOval(...); g.drawImage(imag, x, y, width, height, null); //将内容画到屏幕上}
Related tutorials: Java Video tutorial
The above is the detailed content of How to use the paint method to draw pictures in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
