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.
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!