package huitu;
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.DISPOSE_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);
}
}
结果如下
1.绘制颜色
package huitu;
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) {
Color c1 = new Color(255,0,0);
g2.setColor(c1);
g2.fill(shape);//填充图形
}else {
Color c1 = new Color(0,0,255);
g2.setColor(c1);
BasicStroke bs = new BasicStroke(5,0,1);
g2.setStroke(bs) ;
g2.draw(shape);//绘制图形
}
}
}
}
public static void main(String[] args) {
new DrawFrame().setVisible(true);
}
}
结果如下
1.设置字体
package huitu;
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);
g2.setColor(Color.CYAN); // 设置当前绘图颜色
g2.fill(rect); // 填充矩形
Font font = new Font("宋体", Font.BOLD, 16);//创建字体
Date date = new Date();
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);
}
}
结果如下
PLAIN:正常文字
BOLD:加粗
ITALIC:斜体
package huitu;
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/mis.jpg"));// 读取图片文件
} catch (IOException e) {
e.printStackTrace();
}
setSize(800, 800); // 设置窗体大小
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); // 显示图片,图片,x,y轴,重新绘制时通知哪个对象
}
}
public static void main(String[] args) {
new DrawImage().setVisible(true);
}
}
结果如下
1.放大与缩小
package huitu;
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/mis.jpg"));// 读取图片文件
} 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);
}
}//例题19.5
结果如下
缩放效果如下
2.图像翻转
package huitu;
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 = 800, height = 888;// 图片宽高
private JButton vBtn = null;// 垂直翻转按钮
private JButton hBtn = null;// 水平翻转按钮
private CanvasPanel canvasPanel = null;
public PartImage() {
try {
img = ImageIO.read(new File("src/mis.jpg"));// 读取图片文件
} 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, 800, 600); // 设置窗体大小和位置
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.图像旋转
例题19.7:让照片围绕左上角点旋转
package huitu;
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/mis.jpg")); //读取图片
}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.图形倾斜
例题19.8:让照片变成左倾斜的平行四边形形状
package huitu;
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/mis.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);
}
}
结果如下