本篇文章的主要內容是關於在Java裡用paint方法畫圖,具有一定參考價值,有興趣的朋友可以了解一下,希望能對你有幫助。
java使用paint方法繪圖
需要繼承JFrame類別來畫視窗=> public class Game extends JFrame {} setTitle(String s); / /設定視窗標題 setLocation(int x, int y); //設定視窗位置 setSize(int width, int height); //設定視窗寬高 setVisible(true); //設定視窗可見,預設為flase,這個方法放在setLocation()和setSize後面較好,我放在前面視窗為黑色,本來預設為白色的
paint方法畫圖 定義後自動呼叫
1
2
3
4
5
6
7
8
9
10
11
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));
g.drawString(str, int x, int y);
g.setColor(c);
g.setFont(f);
登入後複製
GameUtil工具類別導入圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}}
登入後複製
在Game類別裡面呼叫GameUtil Image imag = GameUtil .getImage(“images/picture.png”); //我建立的一個images包,用來儲存圖片,引號裡面為圖片的路徑 g.drawImage(imag, x, y, width, height, null ); //imag圖片,位置,寬高,觀察者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public
static
void main(String args[]) {
MyGame game =
new
MyGame();
game.launchJFrame();
}}
登入後複製
#設定圖片的大小 public Image getScaledInstance(int width, int height, int hints) //hints - 指示用於圖像重新取樣的演算法類型的標誌(這句話不知道是什麼意思,照著下面的寫就對了)
1
Image img = GameUtil.getImage(
"images/text1.jpg"
);img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
登入後複製
如果是要獲取圖片的大小,直接使用getWidth()和getHeight()方法就可以了
1
width = img.getWidth();height = img.getheight();
登入後複製
#雙緩衝技術解決閃爍 原理大概是:先將所需要畫的東西載入到緩衝區,然後將緩衝區中的內容全部畫到螢幕上,這樣就可以避免因為螢幕載入的東西太多導致螢幕瘋狂閃爍
1
2
3
4
5
6
7
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);
登入後複製
相關教學:Java影片教學
以上是java如何使用paint方法畫圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!