# JAVA图形与文本基础

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.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.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.设置笔画的粗细

<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.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);
final JPanel panel=new JPanel();
final FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(30);
panel.setLayout(flowLayout);

final JButton btn_large=new JButton();
public void actionPerformed(ActionEvent e) {
flag=1;
zoomPanel.repaint();//重新调用面板的paint()方法
}
});
btn_large.setText("放大");

final JButton btn_small=new JButton();
public void actionPerformed(ActionEvent e) {
flag=2;
zoomPanel.repaint();
}
});
btn_small.setText("缩小");

final JButton btn_restore=new JButton();
@Override
public void actionPerformed(ActionEvent e) {
flag=0;
zoomPanel.repaint();
}
});
btn_restore.setText("还原");
}

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);
final JPanel panel=new JPanel();
final FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(30);
panel.setLayout(flowLayout);

JButton jbt_shun=new JButton();

@Override
public void actionPerformed(ActionEvent e) {
flag=1;
rotateValue+=0.1;
rotatePanel.repaint();
}

});
jbt_shun.setText("顺时针");

JButton jbt_ni=new JButton();

@Override
public void actionPerformed(ActionEvent e) {
flag=2;
rotateValue-=0.1;
rotatePanel.repaint();
}

});
jbt_ni.setText("逆时针");

JButton jbt_restore=new JButton();

@Override
public void actionPerformed(ActionEvent e) {
flag=0;
rotateValue=0.0;
rotatePanel.repaint();
}

});
jbt_restore.setText("还原");

}

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{

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);
}

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);
setBounds(200,160,355,253);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
imagePanel.repaint();
}
});
}

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.裁剪图片

//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.KeyEvent;
import java.awt.event.MouseEvent;
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);
splitPane.setLeftComponent(oldimage);
splitPane.setRightComponent(cutimage);

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;
}
});
public void mouseDragged(MouseEvent e){
if(flag){
releaseX=e.getXOnScreen();
releaseY=e.getYOnScreen();
oldimage.repaint();
}
}
});

@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();
}
}
}


#### Java基础练习 字母图形

2016-02-07 18:44:11

#### java基础练习 字母图形

2016-12-03 00:01:40

#### 蓝桥杯：基础练习 字母图形【JAVA算法实现】

2015-04-13 10:14:38

#### java绘制图形和文本<一>

2017-03-29 16:27:45

#### 简单绘画图形基本工

2017-11-24 00:05:19

#### JAVA基础8.1——绘图基础（1）绘制图形

2016-10-11 19:58:16

#### 蓝桥杯——基础练习之字母图形

2014-05-17 07:07:25

#### 【JAVA语言程序设计基础篇】--图形用户界面基础--一些总结

2016-08-03 20:34:41

#### Java基础之图形用户界面的设计

2016-06-08 16:50:14

#### C++游戏与图形编程基础 第2版

2016年09月21日 86.42MB 下载