码迷,mamicode.com
首页 > 编程语言 > 详细

使用Java2D改善API的绘制效果

时间:2017-04-04 23:33:01      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:默认   .com   color   src   ade   graphics   log   except   orange   

---------------siwuxie095

   

   

   

   

   

   

   

工程名:TestSwingPaintAPI

包名:com.siwuxie095.swingpaint

类名:SwingPaintAPI.java(主类)、MyPanel.java

   

   

向 com.siwuxie095.swingpaint 包中放入图像文件:img.png

   

   

工程结构目录如下:

   

技术分享

   

   

   

   

SwingPaintAPI.java(主类):

   

package com.siwuxie095.swingpaint;

   

import java.awt.BorderLayout;

import java.awt.EventQueue;

   

import javax.swing.JFrame;

import javax.swing.border.EmptyBorder;

   

public class SwingPaintAPI extends JFrame {

   

//将原本声明的 JPanel 注释掉,改为 MyPanel

//private JPanel contentPane;

private MyPanel contentPane;

   

 

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

SwingPaintAPI frame = new SwingPaintAPI();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

   

/**

* Create the frame.

*/

public SwingPaintAPI() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

 

//将原本的实例化方式注释掉,改为 MyPanel()

//contentPane = new JPanel();

contentPane=new MyPanel();

 

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new BorderLayout(0, 0));

setContentPane(contentPane);

}

   

}

   

   

   

MyPanel.java:

   

package com.siwuxie095.swingpaint;

   

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import java.io.IOException;

   

import javax.imageio.ImageIO;

import javax.swing.JPanel;

   

//使用 Java 2D 完成界面的绘图优化

   

//MyPanel 继承自 JPanel

public class MyPanel extends JPanel {

 

//创建一个 BufferedImage

BufferedImage image=null;

 

public MyPanel() {

 

try {

//使用 ImageIO.read()读取图像,传入 URL

//可以是本地图像,也可以是网络图像

//这里读取本地图像,因为使用的是 getClass().getResource()

//所以 img.png 必须和调用的类 MyPanel 在同一个包中

//有异常抛出,使用 try catch 捕获

image=ImageIO.read(getClass().getResource("img.png"));

 

} catch (IOException e) {

e.printStackTrace();

}

}

 

 

//覆盖 JPanel paintComponent() 方法,

//右键->Source->Override/Implement Methods->JComponent

   

//对于每一个组件来说,paintComponent() 方法是绘制组件本身

//传入 Graphics,通过它在界面绘制图像

@Override

protected void paintComponent(Graphics g) {

 

//注释掉默认的从父类继承的绘图方法

//super.paintComponent(g);

 

//如果不使用 Java 2D,使用普通的绘图API

//则绘制的图像有很明显的锯齿

 

 

//创建一个 Graphics2D 对象:g2d,需要强转

//g2d 支持的属性和方法更多

Graphics2D g2d=(Graphics2D) g;

 

//图像渲染的提示,提示值代表了当前的绘图是否支持锯齿消除

//传入键值对,为Graphics2D添加抗锯齿

//(通过类调用静态值)

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

 

 

//Graphics2D 完全兼容 Graphics 的一些方法,包括 draw fill 方法

//draw 方法绘制的都是线框、轮廓(空心),而 fill 方法绘制的是填充的图像(实心)

//对于一般的 draw 方法,都会有对应的 fill 方法(空心对实心)

 

//Graphics2D setClip() 方法

//需要指定 X Y 坐标,宽度,高度

//指定绘制图像的区域,超出就不会被绘制

g2d.setClip(0, 0, 500, 500);

 

 

//先使用 setColor() 为当前的绘图指定颜色

//使用匿名对象,创建一个新的Color对象

//每一个 rgb 的值最大为 255 ,最小为 0

//可以为不同的形状填充不同的颜色

g2d.setColor(new Color(255,0,0));//红色

 

 

//绘制方块,需要指定 X Y 坐标,宽度,高度

// 0 0 即从左上角开始绘制

g2d.drawRect(0, 0, 100, 100);

 

 

//也可以使用静态方法通过类调用

g2d.setColor(Color.GREEN);

//绘制圆形、椭圆形,需要指定 X Y 坐标,宽度,高度

//如果宽高一致,绘制的就是圆形

g2d.drawOval(0, 0, 100, 100);//圆形

g2d.setColor(Color.BLUE);

g2d.drawOval(0, 25, 100, 50);//椭圆形

 

 

g2d.setColor(Color.ORANGE);

//填充方块,需要指定 X Y 坐标,宽度,高度

g2d.fillRect(100, 100, 100, 100);

 

 

//填充带圆角效果的方块,需要指定 X Y 坐标,宽度,高度,圆角的弧宽,圆角的弧高

//一般情况下,将圆角的弧宽,圆角的弧高,两个弧度值设置成相等

//弧宽越大,则 X 方向上圆角越长,弧高越大,则 Y 方向上圆角越长

//弧宽与弧高指定了 X Y 方向上圆角的大小

g2d.fillRoundRect(200, 0, 100, 100, 10, 10);

g2d.fillRoundRect(300, 100, 100, 100, 200, 50);

 

 

//绘制 String,需要指定 X Y 坐标

//绘制 Bytes Chars String

//设置颜色与字体

g2d.setColor(Color.YELLOW);

g2d.setFont(new Font("Arial", Font.BOLD, 20));

g2d.drawString("TestAPI", 110, 50);

 

 

g2d.setColor(Color.CYAN);

//绘制弧线 需要指定 x y 坐标,宽度,高度,起始角度,弧线延长的角度

//绘制时按照逆时针绘制弧线

//

//先画一个矩形,然后以这个矩形的中心为所要画的弧的中心,

//以水平向右为 0 度,逆时针为正方向

g2d.drawArc(0, 200, 100, 100, 270, 90);

g2d.fillArc(0, 200, 100, 100, 0, 270);

g2d.fillArc(100, 200, 100, 200, 0, 90);

g2d.drawArc(100, 200, 100, 200, 90, 90);

g2d.fillArc(200, 200, 100, 200, 30, 120);

 

 

 

//绘制图像,选择传参最少的方法

//需要指定:图像对象imageX Y 坐标,observer(可指定为空)

//对于 image 可以在构造函数 MyPanel() 中加载图像

if (image!=null) {

g2d.drawImage(image, 0, 100, null);

//绘制图像时可以指定图像的大小

}

 

 

//绘制线段时,线段可以指定粗细等属性

//第一个参数是线段的粗细,

//第二个参数 CAP 表示线段头部的形状(圆/方等)

//第三个参数 JOIN 表示线段曲折的地方的形状(尖/圆等)

g2d.setStroke(new BasicStroke(5,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND));

 

 

g2d.setColor(new Color(0,0,0));

//绘制直线 需要指定两组 X Y 坐标

g2d.drawLine(205, 105, 300, 200);

 

 

//绘制多边形,传入一组 X 坐标,一组 Y 坐标,和 坐标数目

g2d.fillPolygon(new int[]{350,300,333,366,400}, new int[]{0,50,100,100,50}, 5);

g2d.drawPolygon(new int[]{350,300,333,366,400}, new int[]{200,250,300,300,250}, 5);

 

}

 

}

   

   

   

修改 SwingPaintAPI.java(主类) 中的 contentPane 的

声明与实例化方式:

   

技术分享

   

技术分享

   

   

   

   

MyPanel.java 中覆盖 JPanel 的 paintComponent() 方法

   

右键->Source->Override/Implement Methods->JComponent

   

   

   

运行程序:

   

技术分享

   

   

   

   

   

   

   

【made by siwuxie095】

使用Java2D改善API的绘制效果

标签:默认   .com   color   src   ade   graphics   log   except   orange   

原文地址:http://www.cnblogs.com/siwuxie095/p/6666541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!