一、Java绘图类
二、绘制图形
绘制奥运五环图案
package nineteentn;
import java.awt.*;
import javax.swing.*;
public class DrawCircle extends JFrame {
private final int OVAL_WIDTH = 80; // 圆形的宽
private final int OVAL_HEIGHT = 80; // 圆形的高
public DrawCircle() {
initialize(); // 调用初始化方法
}
private void initialize() {// 初始化方法
setSize(300, 200); // 设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
setContentPane(new DrawPanel()); // 设置窗体面板为绘图面板对象
setTitle("绘图实例1"); // 设置窗体标题
}
class DrawPanel extends JPanel {// 创建绘图面板
public void paint(Graphics g) {// 重写绘制方法
g.drawOval(10, 10, OVAL_WIDTH, OVAL_HEIGHT); // 绘制第1个圆形
g.drawOval(80, 10, OVAL_WIDTH, OVAL_HEIGHT); // 绘制第2个圆形
g.drawOval(150, 10, OVAL_WIDTH, OVAL_HEIGHT); // 绘制第3个圆形
g.drawOval(50, 70, OVAL_WIDTH, OVAL_HEIGHT); // 绘制第4个圆形
g.drawOval(120, 70, OVAL_WIDTH, OVAL_HEIGHT); // 绘制第5个圆形
}
}
public static void main(String[] args) {
new DrawCircle().setVisible(true);
}
绘制空心和实心
package nineteentn;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class DrawFrame extends JFrame {
public DrawFrame() {
setTitle("绘图实例2"); // 设置窗体标题
setSize(300, 200); // 设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
add(new CanvasPanel()); // 设置窗体面板为绘图面板对象
}
class CanvasPanel extends JPanel {// 绘图变慢
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Shape[] shapes = new Shape[4]; // 声明图形数组
shapes[0] = new Ellipse2D.Double(5, 5, 100, 100); // 创建圆形对象
shapes[1] = new Rectangle2D.Double(110, 5, 100, 100); // 创建矩形对象
shapes[2] = new Rectangle2D.Double(15, 15, 80, 80); // 创建矩形对象
shapes[3] = new Ellipse2D.Double(120, 15, 80, 80); // 创建圆形对象
for (Shape shape : shapes) { // 遍历图形数组
Rectangle2D bounds = shape.getBounds2D();
if (bounds.getWidth() == 80)
g2.fill(shape); // 填充图形
else
g2.draw(shape); // 绘制图形
}
}
}
public static void main(String[] args) {
new DrawFrame().setVisible(true);
}
}
改颜色
三、绘制颜色与画笔属性
1.设置颜色
创建Color对象的构造方法有如下俩种:
Color col=new Color(int r, int g,int b)
Color col=new Color(int rgb)
设置当前颜色:
setColor(Color color)
2.设置画笔
设置画笔属性:
setStroke(Stroke stroke)
四、绘制文本
1.设置字体
构造方法指定字体名称:
Font(String name,int style,int size)
2.显示文字
语法格式:
drawString(String str,int x,int y)
drawString(String str,float x,float y)
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Date;
import javax.swing.*;
public class DrawString extends JFrame {
public DrawString() {
setSize(230, 140); // 设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
add(new CanvasPanel()); // 设置窗体面板为绘图面板对象
setTitle("绘图文本"); // 设置窗体标题
}
class CanvasPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(10, 10, 200, 80);
Font font = new Font("宋体", Font.BOLD, 16);
Date date = new Date();
g2.setColor(Color.CYAN); // 设置当前绘图颜色
g2.fill(rect); // 填充矩形
g2.setColor(Color.BLUE); // 设置当前绘图颜色
g2.setFont(font); // 设置字体
g2.drawString("现在时间是", 20, 30); // 绘制文本
g2.drawString(String.format("%tr", date), 50, 60); // 绘制时间文本
}
}
public static void main(String[] args) {
new DrawString().setVisible(true);
}
}
五、显示图片
package nineteentn;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class DrawImage extends JFrame {
Image img;// 展示的图片
public DrawImage() {
try {
img = ImageIO.read(new File("src/海豚.png"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
setSize(440, 300); // 设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
add(new CanvasPanel()); // 设置窗体面板为绘图面板对象
setTitle("绘制图片"); // 设置窗体标题
}
class CanvasPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(img, 0, 0, this); // 显示图片
}
}
public static void main(String[] args) {
new DrawImage().setVisible(true);
}
}
六、图像处理
package nineteentn;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class ImageZoom extends JFrame {
Image img;
private int imgWidth, imgHeight;
private JSlider jSlider;
public ImageZoom() {
try {
img = ImageIO.read(new File("src/海豚.png"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
CanvasPanel canvas = new CanvasPanel();
jSlider = new JSlider();
jSlider.setMaximum(1000);
jSlider.setValue(100);
jSlider.setMinimum(1);
jSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
canvas.repaint();
}
});
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
center.add(jSlider, BorderLayout.SOUTH);
center.add(canvas, BorderLayout.CENTER);
setContentPane(center);
setBounds(100, 100, 800, 600); // 设置窗体大小和位置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
setTitle("绘制图片");
}
class CanvasPanel extends JPanel {
public void paint(Graphics g) {
int newW = 0, newH = 0;
imgWidth = img.getWidth(this); // 获取图片宽度
imgHeight = img.getHeight(this); // 获取图片高度
float value = jSlider.getValue();// 滑块组件的取值
newW = (int) (imgWidth * value / 100);// 计算图片放大后的宽度
newH = (int) (imgHeight * value / 100);// 计算图片放大后的高度
g.drawImage(img, 0, 0, newW, newH, this);// 绘制指定大小的图片
}
}
public static void main(String[] args) {
new ImageZoom().setVisible(true);
}
}
2.图像翻转
package nineteentn;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class PartImage extends JFrame {
private Image img;
private int dx1, dy1, dx2, dy2;
private int sx1, sy1, sx2, sy2;
private int width = 300, height = 200;// 图片宽高
private JButton vBtn = null;// 垂直翻转按钮
private JButton hBtn = null;// 水平翻转按钮
private CanvasPanel canvasPanel = null;
public PartImage() {
try {
img = ImageIO.read(new File("src/海豚.png"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
dx2 = sx2 = width; // 初始化图像大小
dy2 = sy2 = height;
vBtn = new JButton("垂直翻转");
hBtn = new JButton("水平翻转");
JPanel bottom = new JPanel();
bottom.add(hBtn);
bottom.add(vBtn);
Container c = getContentPane();
c.add(bottom, BorderLayout.SOUTH);
canvasPanel = new CanvasPanel();
c.add(canvasPanel, BorderLayout.CENTER);
addListener();
setBounds(100, 100, 300, 260); // 设置窗体大小和位置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
setTitle("图片翻转"); // 设置窗体标题
}
private void addListener() {
vBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sy1 = Math.abs(sy1 - height);// 纵坐标互换
sy2 = Math.abs(sy2 - height);
canvasPanel.repaint();
}
});
hBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sx1 = Math.abs(sx1 - width);// 横坐标互换
sx2 = Math.abs(sx2 - width);
canvasPanel.repaint();
}
});
}
class CanvasPanel extends JPanel {
public void paint(Graphics g) {
g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, this);// 绘制指定大小的图片
}
}
public static void main(String[] args) {
new PartImage().setVisible(true);
}
}
3.图像旋转
package nineteentn;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class RotateImage extends JFrame {
private Image img;
public RotateImage() {
try {
img = ImageIO.read(new File("src/海豚.png"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
setBounds(100, 100, 400, 350); // 设置窗体大小和位置
add(new CanvasPanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
setTitle("图片旋转"); // 设置窗体标题
}
class CanvasPanel extends JPanel { // 画板
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.rotate(Math.toRadians(5));// 旋转5度
g2.drawImage(img, 70, 10, 300, 200, this);
g2.rotate(Math.toRadians(5));
g2.drawImage(img, 70, 10, 300, 200, this);
g2.rotate(Math.toRadians(5));
g2.drawImage(img, 70, 10, 300, 200, this);
}
}
public static void main(String[] args) { // 主方法
new RotateImage().setVisible(true);
}
}
4.图像倾斜
package nineteentn;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class TiltImage extends JFrame {
private Image img;
public TiltImage() {
try {
img = ImageIO.read(new File("src/cow.jpg"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
setBounds(100, 100, 400, 300); // 设置窗体大小和位置
add(new CanvasPanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
setTitle("图片倾斜"); // 设置窗体标题
}
class CanvasPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.shear(0.3, 0);// 倾斜30%
g2.drawImage(img, 0, 0, 300, 200, this);
}
}
public static void main(String[] args) {
new TiltImage().setVisible(true);
}
}