1.绘制线段
给出三条线段中两个端点的坐标 绘制三条线段
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.swing.*;
import java.awt.*;
public class DrawLinePanel extends JPanel{
public static void main(String args[]){
JFrame frame=new JFrame("DrawLine");
frame.getContentPane().add(new DrawLinePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
public void paint(Graphics g){
//super.paint(g);
g.drawLine(70,50,180,50);
g.drawLine(70,80,180,80);
g.drawLine(110,10,140,120);
}
}
</span></span>
2.绘制矩形
给出矩形左上角的坐标和宽度高度 画出空心矩形和实心矩形
(若要画正方形 把里面的宽度和高度设置成一样的即可)
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.swing.*;
import java.awt.*;
public class DrawRectangle extends JPanel{
public static void main(String args[]){
JFrame frame=new JFrame("DrawRectangle");
frame.getContentPane().add(new DrawRectangle());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
public void paint(Graphics g){
g.drawRect(30,40,80,60);//左上角的坐标 矩形的宽度 高度
g.fillRect(140,40,80,60);//实心矩形
}
}
</span></span>
同样的 使用相应的函数 也可以画出椭圆 圆弧 多边形等图形
3.设置笔画的粗细
使用Graphics2D来修改笔画对象参数 设置粗细
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class ChangeStrokeWidth extends JPanel{
public static void main(String args[]){
JFrame jf=new JFrame("设置笔画的粗细");
jf.getContentPane().add(new ChangeStrokeWidth());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300,200);
jf.setVisible(true);
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
BasicStroke stroke=new BasicStroke(1);//创建宽度是1的笔画对象
g2.setStroke(stroke);
Ellipse2D.Float ellipse=new Ellipse2D.Float(20,20,100,60);
g2.draw(ellipse);
stroke=new BasicStroke(4);
g2.setStroke(stroke);
ellipse=new Ellipse2D.Float(160,20,100,60);
g2.draw(ellipse);
}
}
</span>
4.缩放图形
通过按钮来控制对图形的缩放
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class ZoomShape extends JFrame{
int flag=0;//表示点击按钮的状态 0表示点击还原 1放大 2缩小
ZoomShapePanel zoomPanel=new ZoomShapePanel();
public static void main(String args[]){
ZoomShape jf=new ZoomShape();//创建窗体类的实例
jf.setVisible(true);
}
public ZoomShape(){
super();
setTitle("缩放图形");
setBounds(100,100,338,220);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(zoomPanel);//将面板类的实例添加到窗体容器中
final JPanel panel=new JPanel();
final FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(30);
panel.setLayout(flowLayout);
getContentPane().add(panel,BorderLayout.SOUTH);
final JButton btn_large=new JButton();
btn_large.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
flag=1;
zoomPanel.repaint();//重新调用面板的paint()方法
}
});
btn_large.setText("放大");
panel.add(btn_large);
final JButton btn_small=new JButton();
btn_small.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
flag=2;
zoomPanel.repaint();
}
});
btn_small.setText("缩小");
panel.add(btn_small);
final JButton btn_restore=new JButton();
btn_restore.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
flag=0;
zoomPanel.repaint();
}
});
btn_restore.setText("还原");
panel.add(btn_restore);
}
class ZoomShapePanel extends JPanel{//内部面板类
// float x,y;
// x=(float) 80.0; y=(float) 50.0;
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
float x,y;
x=(float) 80.0; y=(float) 50.0;
Rectangle2D.Float rect=new Rectangle2D.Float(120,50,x,y);
BasicStroke stroke=new BasicStroke(10);
g2.setStroke(stroke);
g2.clearRect(0,0,338,220);
if(flag==0){
g2.draw(rect);
}else if(flag==1){
x=(float) (x*1.2); y=(float) (y*1.2);
// g2.scale(1.2,1.2);//横坐标放大的倍数 纵坐标放大的倍数
rect=new Rectangle2D.Float(120,50,x,y);
g2.draw(rect);
}else{
x=(float) (x*0.8); y=(float) (y*0.8);
rect=new Rectangle2D.Float(120,50,x,y);
// g2.scale(0.8,0.8);
g2.draw(rect);
}
}
}
}
5.旋转图形
通过按钮来控制对图形的旋转
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class RotateShape extends JFrame{
int flag=0;//还原0 顺1 逆2
double rotateValue=0.0;
RotateShapePanel rotatePanel=new RotateShapePanel();
public static void main(String args[]){
RotateShape jf=new RotateShape();
jf.setVisible(true);
}
public RotateShape(){
super();
setTitle("旋转图形");
setBounds(100,100,338,220);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(rotatePanel);
final JPanel panel=new JPanel();
final FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(30);
panel.setLayout(flowLayout);
getContentPane().add(panel,BorderLayout.SOUTH);
JButton jbt_shun=new JButton();
jbt_shun.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
flag=1;
rotateValue+=0.1;
rotatePanel.repaint();
}
});
jbt_shun.setText("顺时针");
panel.add(jbt_shun);
JButton jbt_ni=new JButton();
jbt_ni.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
flag=2;
rotateValue-=0.1;
rotatePanel.repaint();
}
});
jbt_ni.setText("逆时针");
panel.add(jbt_ni);
JButton jbt_restore=new JButton();
jbt_restore.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
flag=0;
rotateValue=0.0;
rotatePanel.repaint();
}
});
jbt_restore.setText("还原");
panel.add(jbt_restore);
}
class RotateShapePanel extends JPanel{
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
Rectangle2D.Float rect=new Rectangle2D.Float(40,40,80,50);
BasicStroke stroke=new BasicStroke(10);
g2.setStroke(stroke);
g2.clearRect(0,0,338,220);
if(flag==0){
g2.draw(rect);
}else if(flag==1){
g2.rotate(rotateValue);
g2.draw(rect);
}else{
g2.rotate(rotateValue);
g2.draw(rect);
}
}
}
}
6.绘制艺术图案
使用随机生成的颜色 绘制由椭圆形构成的图案
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;
public class ArtDesign extends JFrame{
ArtDesignPanel adp=new ArtDesignPanel();
public static void main(String args[]){
ArtDesign jf=new ArtDesign();
jf.setVisible(true);
}
public ArtDesign(){
super();
setTitle("绘制艺术图案");
setBounds(100,100,438,320);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(adp);
}
class ArtDesignPanel extends JPanel{
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
Ellipse2D.Float ellipse=new Ellipse2D.Float(-80,30,160,10);
Random random=new Random();
g2.translate(160, 90);
int R=random.nextInt(256);
int G=random.nextInt(256);
int B=random.nextInt(256);//随机生成RGB三个颜色的值
Color color=new Color(R,G,B);
g2.setColor(color);
g2.draw(ellipse);
for(int i=0;i<100;i++){
R=random.nextInt(256);
G=random.nextInt(256);
B=random.nextInt(256);
color=new Color(R,G,B);
g2.setColor(color);
g2.rotate(10);
g2.draw(ellipse);
}
}
}
}
7.缩放图案
加载一张图片 使用滑块来控制图案的缩放
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.net.*;
public class ZoomImageFrame extends JFrame{
Image img=null;//声明土现象对象
ZoomImagePanel imagePanel=new ZoomImagePanel();
int height,width;
int nheight,nwidth;//图像缩放之后的高度宽度
JSlider slider=new JSlider();//滑块对象
public static void main(String args[]){
ZoomImageFrame jf=new ZoomImageFrame();
jf.setVisible(true);
}
public ZoomImageFrame(){
super();
setTitle("缩放图像");
URL imgUrl=ZoomImageFrame.class.getResource("/img/img.jpg");//img要放在bin文件夹中
img=Toolkit.getDefaultToolkit().getImage(imgUrl);
add(imagePanel);
setBounds(200,160,355,253);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
slider.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
imagePanel.repaint();
}
});
this.getContentPane().add(slider,BorderLayout.SOUTH);
}
class ZoomImagePanel extends JPanel{
public void paint(Graphics g){
g.clearRect(0, 0, this.getWidth(), this.getHeight());
width=img.getWidth(this);
height=img.getHeight(this);
float value=slider.getValue();//滑块组件的取值
nwidth=(int)(width*value/100);
nheight=(int)(height*value/100);
g.drawImage(img, 0, 0, nwidth, nheight, this);
}
}
}
8.裁剪图片
对一个图片进行截图 按ESC键退出时 可保存当前所截图片内容
//import javax.print.DocFlavor.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.Date;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.text.SimpleDateFormat;
public class CutImageFrame extends JFrame{
Image img=null;
OldImagePanel oldimage=new OldImagePanel();
CutImagePanel cutimage=new CutImagePanel();
int pressPanelX=0, pressPanelY=0;
int pressX=0, pressY=0;//鼠标按下点的x y坐标
int releaseX=0, releaseY=0;//鼠标释放点的x y坐标
Robot robot=null;
BufferedImage buffImage=null;//声明缓冲图像对象
boolean flag=false;
public static void main(String args[]){
CutImageFrame jf=new CutImageFrame();
jf.setVisible(true);
}
public CutImageFrame(){
super();
setTitle("裁剪图片");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
URL imgUrl=CutImageFrame.class.getResource("/img/img.jpg");
img=Toolkit.getDefaultToolkit().getImage(imgUrl);
setBounds(200,160,360,280);
JSplitPane splitPane=new JSplitPane();//分割容器类
splitPane.setDividerLocation((this.getWidth()/2)-10);
this.getContentPane().add(splitPane,BorderLayout.CENTER);
splitPane.setLeftComponent(oldimage);
splitPane.setRightComponent(cutimage);
oldimage.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){//鼠标按下事件
pressPanelX=e.getX();
pressPanelY=e.getY();
pressX=e.getXOnScreen()+1;
pressY=e.getYOnScreen()+1;
flag=true;
}
public void mouseReleased(MouseEvent e){
releaseX=e.getXOnScreen()-1;
releaseY=e.getYOnScreen()-1;
try{
robot=new Robot();
if(releaseX-pressX>0&&releaseY-pressY>0){
Rectangle rect=new Rectangle(pressX,pressY,releaseX-pressX,releaseY-pressY);
buffImage=robot.createScreenCapture(rect);
cutimage.repaint();
}
}catch(AWTException e1){
e1.printStackTrace();
}
flag=false;
}
});
oldimage.addMouseMotionListener(new MouseMotionAdapter(){//鼠标移动事件
public void mouseDragged(MouseEvent e){
if(flag){
releaseX=e.getXOnScreen();
releaseY=e.getYOnScreen();
oldimage.repaint();
}
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// 按Esc键退出
if (e.getKeyCode() == 27) {
saveToFile();
System.exit(0);
}
}
});
}
class OldImagePanel extends JPanel{
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
g2.setColor(Color.white);
if(flag){
float[] arr={5.0f};//创建虚线模式的数组
BasicStroke stroke=new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,1.0f,arr,0);
g2.setStroke(stroke);
g2.drawRect(pressPanelX, pressPanelY, releaseX-pressX, releaseY-pressY);
}
}
}
class CutImagePanel extends JPanel{
public void paint(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.drawImage(buffImage, 0, 0, releaseX-pressX, releaseY-pressY, this);
}
}
public void saveToFile() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String name = sdf.format(new Date());
File path = FileSystemView.getFileSystemView().getHomeDirectory();
String format = "jpg";
File f = new File(path + File.separator + name + "." + format);
System.out.println(path + File.separator + name + "." + format);
try {
ImageIO.write(buffImage, format, f);
} catch (IOException e) {
e.printStackTrace();
}
}
}