实现了简单的电脑画图软件,代码如下:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.swing.*;
public class MYPAINT extends JFrame
{
private int flag = 1; //功能端
private JButton bpan = new JButton(" 画笔 ");
private JButton bxpc = new JButton("橡皮擦");
private JButton bwbk = new JButton("文本框");
private JButton bqk = new JButton("清空");
private JButton bpq = new JButton("喷漆");
private JButton bb = new JButton("B");
private JButton bzfx = new JButton("矩形");
private JButton bsjx = new JButton("三角形");
private JButton byx = new JButton("圆形");
private JButton bty = new JButton("椭圆");
private JButton byjjx = new JButton("圆角矩形");
private JButton bzx = new JButton("直线");
private JRadioButton issx = new JRadioButton("是否实心");
private JLabel lnowc = new JLabel("当前\n颜色: ");
private JButton bhong = new JButton();
private JButton bhuang = new JButton();
private JButton bcolor = new JButton();
private JButton blan = new JButton();
private JButton blv = new JButton();
private JButton bmore = new JButton("更多");
private JLabel lzt = new JLabel("字体:");
private String str[] = {
"宋体", "隶书", "华文彩云", "仿宋_GB2312", "华文行楷","方正舒体"
};
private String nzt = new String(" 宋体 ");
private JComboBox czt = new JComboBox(str);
private JLabel lcx = new JLabel("粗细:");
private TextField tcx = new TextField("5",10);
private JCheckBox rb = new JCheckBox("粗体");
private JCheckBox ri = new JCheckBox("斜体");
private int isb ;
private int isi ;
private TextField stata = new TextField(30); //创建文本框对象,长度为30个字符
private JPanel tool = new JPanel();
private MyCanvas pmain = new MyCanvas();
private JPanel p1 = new JPanel();//工具
private JPanel p2 = new JPanel();//形状
private JPanel p3 = new JPanel();//颜色
private JPanel p4 = new JPanel();//文本
private Label p5 = new Label();//
private PAN a = new PAN();
private int MAX = 0;
private int R=0, G=00, B=0;
private shap[] csave = new shap[10000];
public MYPAINT()
{
setLocation(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1080, 960);
setLocationRelativeTo(null);
setVisible(true);
//this.setResizable(false);
GridBagLayout gridbag = new GridBagLayout();
tool.setLayout(gridbag);
tool.add(p1);tool.add(p2);tool.add(p3);tool.add(p4);tool.add(p5);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth=1;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个
gbc.weightx = 0;//该方法设置组件水平的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
gbc.weighty=0;//该方法设置组件垂直的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
gridbag.setConstraints(p1, gbc);//设置组件
p1.setLayout(new GridLayout(3,2));
JPanel tp11 = new JPanel();JPanel tp22 = new JPanel();JPanel tp33 = new JPanel();JPanel tp44 = new JPanel();JPanel tp55 = new JPanel();JPanel tp66 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("工具"));
tp11.add(bpan);tp22.add(bxpc);tp33.add(bwbk);tp44.add(bqk);tp55.add(bpq);tp66.add(bb);
p1.add(tp11);p1.add(tp22);p1.add(tp33);p1.add(tp44);p1.add(tp55);p1.add(tp66);
gbc.gridwidth=1;
gbc.weightx = 0;
gbc.weighty=0;
gridbag.setConstraints(p2,gbc);
p2.setBorder(BorderFactory.createTitledBorder("形状"));
p2.setLayout(new GridLayout(3,4));
JPanel tp20= new JPanel(); JPanel tp21= new JPanel();
p2.add(bzfx);p2.add(bsjx);p2.add(byx);p2.add(bty);p2.add(byjjx);p2.add(bzx);p2.add(tp20);p2.add(tp21);p2.add(issx);
gbc.gridwidth=1;
gbc.weightx = 0;
gbc.weighty=0;
gridbag.setConstraints(p3,gbc);
p3.setBorder(BorderFactory.createTitledBorder("颜色"));
p3.setLayout(new GridLayout(3,2));
JPanel pbcolor = new JPanel();pbcolor.add(bcolor);
p3.add(lnowc);p3.add(bhong);p3.add(bhuang);p3.add(pbcolor);;p3.add(blan);p3.add(blv);p3.add(new Label());p3.add(new Label());p3.add(bmore);
bcolor.setBackground(new Color(R,G,B));;bhong.setBackground(Color.RED); bhuang.setBackground(Color.yellow);
blan.setBackground(Color.BLUE);blv.setBackground(Color.GREEN);
gbc.gridwidth=1;
gbc.weightx = 0;
gbc.weighty=0;
gridbag.setConstraints(p4,gbc);
p4.setBorder(BorderFactory.createTitledBorder("文本"));
JPanel tp0 = new JPanel();
czt.setPreferredSize(new Dimension(100,20));
tp0.add(lzt);tp0.add(czt);
JPanel tp1 = new JPanel();
czt.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
nzt = str[czt.getSelectedIndex()];
System.out.println(nzt);
}
});
tcx.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e) {
int keyChar = e.getKeyChar();
if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
}else{
e.consume(); //屏蔽掉非法输入
}
}
});
JPanel tp2 = new JPanel();tp2.add(ri);tp2.add(rb);
checkBox han = new checkBox();
ri.addItemListener(han);rb.addItemListener(han);
tp1.add(lcx);tp1.add(tcx);
p4.setLayout(new GridLayout(3,1));
p4.add(tp1); p4.add(tp0);p4.add(tp2);
gbc.gridwidth=0;//该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个
gbc.weightx = 1;//不能为1,j4是占了4个格,并且可以横向拉伸,
gbc.weighty=0;
gridbag.setConstraints(p5,gbc);
/**TOOL结束,画板开始***/
pmain.setBorder(BorderFactory.createTitledBorder("画板"));
pmain.setBackground(Color.WHITE);
dosave();
bpan.addActionListener(new ActionListener() {//画笔
@Override
public void actionPerformed(ActionEvent e) {flag = 1;}});
bpq.addActionListener(new ActionListener() {//喷漆
@Override
public void actionPerformed(ActionEvent e) {flag = 15;}});
bxpc.addActionListener(new ActionListener() {//橡皮擦
@Override
public void actionPerformed(ActionEvent e) {flag = 2;}});
bwbk.addActionListener(new ActionListener() {//文本框
@Override
public void actionPerformed(ActionEvent e) {flag = 3;}});
bqk.addActionListener(new ActionListener() {//清空
@Override
public void actionPerformed(ActionEvent e) {MAX=0;repaint();}});
bzfx.addActionListener(new ActionListener() {//矩形
@Override
public void actionPerformed(ActionEvent e) {
if(issx.isSelected())
flag = 5;
else
flag = 4;
}});
bsjx.addActionListener(new ActionListener() {//三角形
@Override
public void actionPerformed(ActionEvent e) {
if(issx.isSelected())
flag = 7;
else
flag = 6;}});
byx.addActionListener(new ActionListener() {//圆形
@Override
public void actionPerformed(ActionEvent e) {
if(issx.isSelected())
flag = 9;
else
flag = 8;}});
bty.addActionListener(new ActionListener() {//椭圆
@Override
public void actionPerformed(ActionEvent e) {
if(issx.isSelected())
flag = 11;
else
flag = 10;}});
byjjx.addActionListener(new ActionListener() {//圆角矩形
@Override
public void actionPerformed(ActionEvent e) {
if(issx.isSelected())
flag = 13;
else
flag = 12;}});
bzx.addActionListener(new ActionListener() {//直线
@Override
public void actionPerformed(ActionEvent e) {flag = 14;}});
bhong.addActionListener(new ActionListener() {//红
@Override
public void actionPerformed(ActionEvent e) {R = 255;G = 0;B = 0; bcolor.setBackground(new Color(R,G,B));}});
bhuang.addActionListener(new ActionListener() {//黄
@Override
public void actionPerformed(ActionEvent e) {R = 255;G = 255;B = 0; bcolor.setBackground(new Color(R,G,B));}});
blan.addActionListener(new ActionListener() {//蓝
@Override
public void actionPerformed(ActionEvent e) {R = 0;G = 0;B = 255; bcolor.setBackground(new Color(R,G,B));}});
blv.addActionListener(new ActionListener() {//绿
@Override
public void actionPerformed(ActionEvent e) {R = 0;G = 255;B = 0; bcolor.setBackground(new Color(R,G,B));}});
bmore.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Color color=new Color(200,200,200);
color=JColorChooser.showDialog(null,"请选择你喜欢的颜色" ,color);
R = color.getRed();
B = color.getBlue();
G = color.getGreen();
bcolor.setBackground(new Color(R,G,B));
}
});
bwbk.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
flag = 3;
}
});
bb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(MAX!=0)
MAX--;
repaint();
}
});
add(tool,"North");
add(pmain,"Center");
add(stata,"South");
stata.setEditable(false);
}
//画图面板类,用来画图
class MyCanvas extends JPanel {
public MyCanvas() {
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new mousec());
addMouseMotionListener(new mousec());
JPanel a = new JPanel();
}
@Override
public void paintComponent(Graphics g0) {
super.paintComponent(g0);
Graphics2D g = (Graphics2D) g0;
int i = 0;
while(i<=MAX)
{
//System.out.println("xx"+MAX);
draw(g,csave[i++]);
}
}
void draw(Graphics2D g, shap s) {
s.draw(g);//画
}
}
//保存画过的图像
void dosave()
{
switch(flag)
{
case 1:csave[MAX] = new PAN(); break;
case 2:csave[MAX] = new XPC(); break;
case 3:csave[MAX] = new TEXT(); break;
case 4:csave[MAX] = new ZFX(); break;
case 5:csave[MAX] = new SZFX(); break;
case 6:csave[MAX] = new SJX(); break;
case 7:csave[MAX] = new SSJX(); break;
case 8:csave[MAX] = new YX(); break;
case 9:csave[MAX] = new SYX(); break;
case 10:csave[MAX] = new TY(); break;
case 11:csave[MAX] = new STY(); break;
case 12:csave[MAX] = new YJJX(); break;
case 13:csave[MAX] = new SYJJX(); break;
case 14:csave[MAX] = new ZX(); break;
case 15:csave[MAX] = new PENQI();break;
}
csave[MAX].B = B;csave[MAX].G = G;csave[MAX].R = R;
csave[MAX].cx = Integer.valueOf(tcx.getText());
csave[MAX].zti = nzt;
csave[MAX].max = Integer.valueOf(tcx.getText())*2;
csave[MAX].time = Integer.valueOf(tcx.getText())*30;
Random random = new Random();
for(int i = 0;i<100;i++)
{
csave[MAX].fx[i] = random.nextInt(csave[MAX].max) - csave[MAX].max;
csave[MAX].fy[i] = random.nextInt(csave[MAX].max) - csave[MAX].max;
}
}
class mousec implements MouseMotionListener,MouseListener {
public void mousePressed(MouseEvent e) {
MAX++;
dosave();
csave[MAX].x1 = csave[MAX].x2 = e.getX();
csave[MAX].y1 = csave[MAX].y2 = e.getY();
System.out.println(e.getX());
if(flag == 3)
{
String input;
input = JOptionPane.showInputDialog(
"");
csave[MAX].isb = isb;
csave[MAX].isi = isi;
csave[MAX].zti = nzt;
csave[MAX].s = input;
csave[MAX].m = MAX;
System.out.println(csave[MAX].x1);
System.out.println(MAX);
repaint();
}
}
public void mouseReleased(MouseEvent e) {
csave[MAX].x2 = e.getX();
csave[MAX].y2 = e.getY();
repaint();
}
public void mouseEntered(MouseEvent e) {
String s = "鼠标已进入窗体";
stata.setText(s); //设置文本框显示鼠标已进入窗体字符串
}
public void mouseExited(MouseEvent e) {
String s = "鼠标已移出窗体";
stata.setText(s);
}
public void mouseDragged(MouseEvent e) {
String s = "鼠标拖动到位置(" + e.getX() + "," + e.getY() + ")"; //取得鼠标坐标
stata.setText(s); //设置文本框显示鼠标拖动坐标
if(flag == 1||flag == 2)
{
MAX++;
dosave();
csave[MAX - 1].x1 = e.getX();
csave[MAX].x2 = csave[MAX].x1 = e.getX();
csave[MAX - 1].y1 = e.getY();
csave[MAX].y2 = csave[MAX].y1 = e.getY();
//System.out.println(MAX);
}
if(flag == 15)
{
MAX++;
dosave();
csave[MAX].x2 = csave[MAX].x1 = e.getX();
csave[MAX].y2 = csave[MAX].y1 = e.getY();
//System.out.println(MAX);
}
csave[MAX].x2 = e.getX();
csave[MAX].y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) { //鼠标移动方法
String s = "鼠标移动到位置(" + e.getX() + "," + e.getY() + ")"; //取得鼠标坐标
stata.setText(s); //设置文本框显示鼠标拖动坐标
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
repaint();
}
}
private class checkBox implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == rb) {
if (e.getStateChange() == ItemEvent.SELECTED) {isb = Font.BOLD;}
else {isb = Font.PLAIN;}
}
if (e.getSource() == ri) {
if (e.getStateChange() == ItemEvent.SELECTED) {isi = Font.ITALIC;}
else {isi = Font.PLAIN;}
}
System.out.println(isb+" "+isi);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MYPAINT();
}
});
}
}
class shap implements Serializable
{
int x1, y1, x2, y2,R=100, G=100, B=100;
float cx; int m = 0;
String zti;
String s;
int isb,isi;
int time,max;
int fx[] = new int[100];int fy[] = new int[100];
void draw(Graphics2D g) {
};
}
class PAN extends shap//画笔
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx,BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(x1, y1, x2, y2);
}
}
class PENQI extends shap//喷漆
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(max/20,BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
for(int i = 0;i<100;i++)
{
double d = (double)fx[i];
double c = (double)fy[i];
g.drawLine((int) (x1+d*Math.sin(d)), (int) (y1+c*Math.sin(c)), (int) (x2+d*Math.sin(d)), (int) (y2+c*Math.sin(c)));
}
}
}
class XPC extends shap//橡皮擦
{
void draw(Graphics2D g) {
g.setColor(new Color(255, 255, 255));
g.setStroke(new BasicStroke(cx + 4,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(x1, y1, x2, y2);
}
}
class TEXT extends shap//文字
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setFont(new Font(zti, isb+isi, ((int) cx) * 20));
if (s != null) {
g.drawString(s, x1, y1);
}
System.out.println(s+","+ x1+","+x2+","+m);
}
}
class ZX extends shap //直线
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(x1, y1, x2, y2);
}
}
class ZFX extends shap//正方形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
class SZFX extends shap//实心正方形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.fillRect(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
class SJX extends shap//三角形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
int x3 = (x1+x2)/2;
g.drawLine(x1, y2, x3, y1);
g.drawLine( x3, y1, x2, y2);
g.drawLine( x1, y2, x2, y2);
}
}
class SSJX extends shap//实心三角形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
int x3 = (x1+x2)/2;
g.drawLine(x1, y2, x3, y1);
g.drawLine( x3, y1, x2, y2);
g.drawLine( x1, y2, x2, y2);
}
}
class TY extends shap//椭圆
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
class STY extends shap//实心椭圆
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.fillOval(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
class YX extends shap//圆形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
}
}
class SYX extends shap//实心圆形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.fillOval(Math.min(x1, x2), Math.min(y1, y2),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
}
}
class YJJX extends shap//圆角矩形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1 - x2), Math.abs(y1 - y2),50, 35);
}
}
class SYJJX extends shap//实现圆角矩形
{
void draw(Graphics2D g) {
g.setColor(new Color(R, G, B));
g.setStroke(new BasicStroke(cx));
g.fillRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2),50, 35);
}
}
/*无用调试*/
class TESE extends JPanel{
public void paint(Graphics g){
g.drawLine(6,150,50,50);
g.drawLine(6,150,50,150);
g.drawRect(60,30,100,30);
g.draw3DRect(60,160,100,30,true);
g.drawRoundRect(60, 200, 100, 40, 10, 10);
g.drawArc(200,50,10,8,0,45);
g.drawOval(200,150,80,80);
g.drawString("测试绘制文字", 200, 250);
Color c = Color.RED;
g.setColor(c);
g.fillOval(200, 100,100 , 40);
g.setColor(Color.PINK);
g.fill3DRect(60,80,100,30,true);
g.setColor(Color.BLUE);
g.fillArc(200, 200, 100, 80, 0, 45);
Font f = new Font("黑体",Font.ITALIC,60);
g.setFont(f);
g.drawString("欢迎访问",10,300);
}
}