Java图形界面——绘图技术演示
分类:
文章
•
2024-02-10 14:45:28
-
package com.test.tank;
-
-
import javax.swing.*;
-
import java.awt.*;
-
-
public class Test1 extends JFrame{
-
MyPanel mp = null;
-
-
public static void main(String[] args) {
-
Test1 test = new Test1();
-
}
-
-
public Test1(){
-
mp = new MyPanel();
-
-
this.add(mp);
-
-
this.setSize(400, 300);
-
this.setTitle("画图测试");
-
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
this.setVisible(true);
-
}
-
-
}
-
-
class MyPanel extends Panel{ //定义我的面板
-
-
public void paint(Graphics g){
-
super.paint(g);
-
g.drawLine(10, 10, 30, 10); //画直线
-
g.drawRect(50, 10, 30, 30); //画矩形
-
g.setColor(Color.red); //将画笔设置成红色
-
g.fill3DRect(100, 10, 50, 30, true); //画凸出3D矩形
-
g.drawOval(10, 50, 20, 60); //画椭圆
-
g.setFont(new Font("宋体", Font.BOLD, 30));//设置字体属性
-
g.setColor(Color.blue);
-
g.drawString("YuJar-演示", 10, 150);//画字符串
-
//得到图片
-
Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/20131002_111255.jpg"));
-
//将图片画到200,50的坐标位置,大小为180*160
-
g.drawImage(im, 200, 50, 180, 160, this); //this代词指明将图片放置到JPanel面板上
-
}
-
}