十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
找到了,很久以前写的一个简单画图,呵呵~当时要求用AWT写,很难受。
创新互联公司-专业网站定制、快速模板网站建设、高性价比宁津网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式宁津网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖宁津地区。费用合理售后完善,10多年实体公司更值得信赖。
package net.miqiang.gui;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
/**
* 简单画图板程序
* 好久没用 AWT 了,写起来真别扭,如果用 swing 会很舒服,有空再改写吧。
*
* @author 米强
*
*/
public class TestMain extends Frame {
// 画板
private Palette palette = null;
// 显示当前颜色的面板
private Panel nonceColor = null;
// 画笔粗细
private Label drawWidth = null;
// 画笔端点的装饰
private Label drawCap = null;
// 选取颜色按钮的监听事件类
private ButtonColorAction buttonColorAction = null;
// 鼠标进入按钮后光标样式的监听事件类
private ButtonCursor buttonCursor = null;
// 画笔样式的监听事件
private ButtonStrokeAction buttonStrokeAction = null;
/**
* 构造方法
*
*/
public TestMain() {
// 设置标题栏文字
super("简易画图板");
// 构造一个画图板
palette = new Palette();
Panel pane = new Panel(new GridLayout(2, 1));
// 画笔颜色选择器
Panel paneColor = new Panel(new GridLayout(1, 13));
// 12 个颜色选择按钮
Button [] buttonColor = new Button[12];
Color [] color = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
// 显示当前颜色的面板
nonceColor = new Panel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction = new ButtonColorAction();
buttonCursor = new ButtonCursor();
for(int i = 0; i buttonColor.length; i++){
buttonColor[i] = new Button();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
}
pane.add(paneColor);
// 画笔颜色选择器
Panel paneStroke = new Panel(new GridLayout(1, 13));
// 控制画笔样式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[1] = new Button("3");
buttonStroke[2] = new Button("5");
buttonStroke[3] = new Button("7");
buttonStroke[4] = new Button("9");
buttonStroke[5] = new Button("11");
buttonStroke[6] = new Button("13");
buttonStroke[7] = new Button("15");
buttonStroke[8] = new Button("17");
buttonStroke[9] = new Button("■");
buttonStroke[10] = new Button("●");
drawWidth = new Label("3", Label.CENTER);
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
if(i = 8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
}
if(i == 8){
paneStroke.add(drawCap);
}
}
pane.add(paneStroke);
// 将画笔颜色选择器添加到窗体中
this.add(pane, BorderLayout.NORTH);
// 将画图板添加到窗体中
this.add(palette);
// 添加窗口监听,点击关闭按钮时退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置窗体 ICON 图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));
// 设置窗口的大小
this.setSize(new Dimension(400, 430));
// 设置窗口位置,处于屏幕正中央
this.setLocationRelativeTo(null);
// 显示窗口
this.setVisible(true);
}
/**
* 程序入口
*
* @param args
* 字符串数组参数
*/
public static void main(String[] args) {
new TestMain();
}
/**
* 选取颜色按钮的监听事件类
* @author 米强
*
*/
class ButtonColorAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color color_temp = ((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
}
}
/**
* 鼠标进入按钮变换光标样式监听事件类
* @author 米强
*
*/
class ButtonCursor extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
/**
* 设置画笔的监听事件类
* @author 米强
*
*/
class ButtonStrokeAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button button_temp = (Button) e.getSource();
String name = button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}else if(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}else if(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
}
}
}
}
}
/**
* 画板类
*
* @author 米强
*
*/
class Palette extends Panel implements MouseListener, MouseMotionListener {
// 鼠标 X 坐标的位置
private int mouseX = 0;
// 上一次 X 坐标位置
private int oldMouseX = 0;
// 鼠标 Y 坐标的位置
private int mouseY = 0;
// 上一次 Y 坐标位置
private int oldMouseY = 0;
// 画图颜色
private Color color = null;
// 画笔样式
private BasicStroke stroke = null;
// 缓存图形
private BufferedImage image = null;
/**
* 构造一个画板类
*
*/
public Palette() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
// 默认黑色画笔
color = new Color(0, 0, 0);
// 设置默认画笔样式
stroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
// 建立 1280 * 1024 的 RGB 缓存图象
image = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB);
// 设置颜色
image.getGraphics().setColor(Color.white);
// 画背景
image.getGraphics().fillRect(0, 0, 1280, 1024);
}
/**
* 重写 paint 绘图方法
*/
public void paint(Graphics g) {
super.paint(g);
// 转换为 Graphics2D
Graphics2D g2d = (Graphics2D) g;
// 获取缓存图形 Graphics2D
Graphics2D bg = image.createGraphics();
// 图形抗锯齿
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置绘图颜色
bg.setColor(color);
// 设置画笔样式
bg.setStroke(stroke);
// 画线,从上一个点到新的点
bg.drawLine(oldMouseX, oldMouseY, mouseX, mouseY);
// 将缓存中的图形画到画板上
g2d.drawImage(image, 0, 0, this);
}
/**
* 重写 update 方法
*/
public void update(Graphics g) {
this.paint(g);
}
/**
* @return stroke
*/
public BasicStroke getStroke() {
return stroke;
}
/**
* @param stroke 要设置的 stroke
*/
public void setStroke(BasicStroke stroke) {
this.stroke = stroke;
}
/**
* 设置画笔粗细
* @param width
*/
public void setStroke(float width) {
this.stroke = new BasicStroke(width, stroke.getEndCap(), stroke.getLineJoin());
}
/**
* 设置画笔端点的装饰
* @param cap 参考 java.awt.BasicStroke 类静态字段
*/
public void setStroke(int cap) {
this.stroke = new BasicStroke(stroke.getLineWidth(), cap, stroke.getLineJoin());
}
/**
* @return color
*/
public Color getColor() {
return color;
}
/**
* @param color 要设置的 color
*/
public void setColor(Color color) {
this.color = color;
}
public void mouseClicked(MouseEvent mouseEvent) {
}
/**
* 鼠标按下
*/
public void mousePressed(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX = mouseEvent.getX();
this.oldMouseY = this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseReleased(MouseEvent mouseEvent) {
}
/**
* 鼠标进入棋盘
*/
public void mouseEntered(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
/**
* 鼠标退出棋盘
*/
public void mouseExited(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
/**
* 鼠标拖动
*/
public void mouseDragged(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.mouseX = mouseEvent.getX();
this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseMoved(MouseEvent mouseEvent) {
}
}
//百度文库找的,可费了我的财富值了,你可要把分给我呀。
package lee;
/*文件名:Calculator.java
*说明:简易科学计算器
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends Frame implements ActionListener, WindowListener
{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField; //计算结果显示区
private String lastCommand; //保存+,-,*,/,=命令0
private double result; //保存计算结果
private boolean start; //判断是否为数字的开始
private JMenuBar menubar;
private JMenuItem m_exit,m2_ejz,m2_bjz;
private Dialog dialog;
private Label label_dialog;
private JButton button_sqrt,button_plusminus,button_CE,button_cancel,button_1,button_2,
button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_0,
button_plus,button_minus,button_multiply,button_divide,button_point,
button_equal,button_log,button_tan,button_cos,button_sin,button_exp;
public Calculator() //构造方法设置布局、为按钮注册事件监听器
{
super("Calculator");
this.setLocation(240,200);
this.setSize(350,300);
this.setResizable(true);
this.setLayout(new GridLayout(7,1));
this.addmyMenu(); //调用成员方法添加菜单
displayField=new JTextField(30);
this.add(displayField);
displayField.setEditable(true);
start=true;
result=0;
lastCommand = "=";
JPanel panel0=new JPanel();
panel0.setLayout(new GridLayout(1,4,4,4));
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,5,4,4));
this.add(panel1);
button_sqrt=new JButton("sqrt");
button_plusminus=new JButton("+/-");
button_exp=new JButton("exp");
button_CE=new JButton("退格");
button_cancel=new JButton("C");
JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,5,4,4));
this.add(panel2);
button_7=new JButton("7");
button_8=new JButton("8");
button_9=new JButton("9");
button_log=new JButton("log");
button_divide=new JButton("/");
JPanel panel3=new JPanel();
panel3.setLayout(new GridLayout(1,5,4,4));
this.add(panel3);
button_4=new JButton("4");
button_5=new JButton("5");
button_6=new JButton("6");
button_tan=new JButton("tan");
button_multiply=new JButton("*");
JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(1,5,4,4));
this.add(panel4);
button_1=new JButton("1");
button_2=new JButton("2");
button_3=new JButton("3");
button_cos=new JButton("cos");
button_minus=new JButton("-");
JPanel panel5=new JPanel();
panel5.setLayout(new GridLayout(1,5,4,4));
this.add(panel5);
button_0=new JButton("0");
button_point=new JButton(".");
button_equal=new JButton("=");
button_sin=new JButton("sin");
button_plus=new JButton("+");
panel1.add(button_sqrt);
panel1.add(button_plusminus);
panel1.add(button_exp);
panel1.add(button_CE);
panel1.add(button_cancel);
panel2.add(button_7);
panel2.add(button_8);
panel2.add(button_9);
panel2.add(button_log);
panel2.add(button_divide);
panel3.add(button_4);
panel3.add(button_5);
panel3.add(button_6);
panel3.add(button_tan);
panel3.add(button_multiply);
panel4.add(button_1);
panel4.add(button_2);
panel4.add(button_3);
panel4.add(button_cos);
panel4.add(button_minus);
panel5.add(button_0);
panel5.add(button_point);
panel5.add(button_equal);
panel5.add(button_sin);
panel5.add(button_plus);
button_sqrt.addActionListener(this);
button_plusminus.addActionListener(this);
button_exp.addActionListener(this);
button_CE.addActionListener(this);
button_cancel.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_log.addActionListener(this);
button_divide.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_tan.addActionListener(this);
button_multiply.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_cos.addActionListener(this);
button_minus.addActionListener(this);
button_0.addActionListener(this);
button_point.addActionListener(this);
button_equal.addActionListener(this);
button_sin.addActionListener(this);
button_plus.addActionListener(this);
this.addWindowListener(new WinClose()); //注册窗口监听器
this.setVisible(true);
}
private void addmyMenu() //菜单的添加
{
JMenuBar menubar=new JMenuBar();
this.add(menubar);
JMenu m1=new JMenu("选项");
JMenu m2=new JMenu("进制转换");
JMenuItem m1_exit=new JMenuItem("退出");
m1_exit.addActionListener(this);
JMenuItem m2_ejz=new JMenuItem("二进制");
m2_ejz.addActionListener(this);
JMenuItem m2_bjz=new JMenuItem("八进制");
m2_bjz.addActionListener(this);
JMenu m3 = new JMenu(" 帮助");
JMenuItem m3_Help = new JMenuItem("用法");
m3_Help.addActionListener(this);
dialog = new Dialog(this,"提示",true); //模式窗口
dialog.setSize(240,80);
label_dialog = new Label("",Label.CENTER); //标签的字符串为空,居中对齐
dialog.add(label_dialog);
dialog.addWindowListener(this); //为对话框注册窗口事件监听器
m1.add(m1_exit);
menubar.add(m1);
m2.add(m2_ejz);
m2.add(m2_bjz);
menubar.add(m2);
m3.add(m3_Help);
menubar.add(m3); }
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(e.getSource().equals(button_1)||e.getSource().equals(button_2)||
e.getSource().equals(button_3)||e.getSource().equals(button_4)||
e.getSource().equals(button_5)|| e.getSource().equals(button_6)||
e.getSource().equals(button_7)|| e.getSource().equals(button_8)||
e.getSource().equals(button_9) ||e.getSource().equals(button_0)||
e.getSource().equals(button_point)||e.getSource().equals(button_plusminus)||
e.getSource().equals(button_cancel)||e.getSource().equals(button_CE))
{ //非运算符的处理方法
String input=e.getActionCommand();
if (start)
{
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}
if(!input.equals("+/-"))
{
String str=displayField.getText();
if(input.equals("退格")) //退格键的实现方法
{
if(str.length()0)
displayField.setText(str.substring(0,str.length()-1));
}
else if(input.equals("C")) //清零键的实现方法
{
displayField.setText("0");
start=true;
}
else
displayField.setText(displayField.getText()+input);
}
}
else if (e.getActionCommand()=="二进制") //二进制的转换
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toBinaryString(n));
}
else if (e.getActionCommand()=="八进制") //八进制的转换
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toOctalString(n));
}
else if (e.getActionCommand()=="退出") //选项中退出的处理方法
{
System.exit(0);
}
else if (e.getActionCommand()=="用法") //按下'帮助'菜单栏中用法的处理方法
{
label_dialog.setText("sqrt,exp等键是先输运算符再输数字\n");
dialog.setLocation(400,250);
dialog.setVisible(true);
}
else //各运算符的识别
{
String command=e.getActionCommand();
if(start)
{
lastCommand=command;
}
else
{
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}
public void calculate(double x) //各运算符的具体运算方法
{
double d=0;
if (lastCommand.equals("+"))
result+= x;
else if (lastCommand.equals("-"))
result-=x;
else if (lastCommand.equals("*"))
result*=x;
else if (lastCommand.equals("/"))
result/=x;
else if (lastCommand.equals("="))
result=x;
else if (lastCommand.equals("sqrt"))
{
d=Math.sqrt(x);
result=d;
}
else if (lastCommand.equals("exp"))
{
d=Math.exp(x);
result=d;
}
else if (lastCommand.equals("log"))
{
d=Math.log(x);
result=d;
}
else if (lastCommand.equals("tan"))
{
d=Math.tan(x);
result=d;
}
else if (lastCommand.equals("cos"))
{
d=Math.cos(x);
result=d;
}
else if (lastCommand.equals("sin"))
{
d=Math.sin(x);
result=d;
}
displayField.setText(""+ result);
}
public void windowClosing(WindowEvent e)
{
if(e.getSource()==dialog)
dialog.setVisible(false); //隐藏对话框
else
System.exit(0);
}
public void windowOpened(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public static void main(String args[])
{
Calculator calculator=new Calculator();
}
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e) //单击窗口关闭按钮时触发并执行实现窗口监听器接口中的方法
{
System.exit(0); //结束程序运行
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
package hao;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;
JButton music;
public ChatPanel() {
setLayout(new BorderLayout());
text = new JTextPane();
text.setEditable(false);
doc = text.getStyledDocument();//跟踪文本和图片写到该区域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面对JComboBox的巧用***********************************************************************
String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);
fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//创建一个可以容纳多个Box组件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字体~~"), b2 = new JLabel("样式~~"),b3 = new JLabel("字号~~"),b4 = new JLabel("颜色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);
textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));
inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //设置文本区的换行策略。88888*********************************
send = new JButton("发送");
record=new JButton("显示记录");
saveRecord=new JButton("储存记录");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //设置按钮间距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置Box的边框线********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);
inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置输入窗口边框线*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//设置输入窗口边框线*******************
JPanel audioPanel = new JPanel();//最上面的边框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));
music = new JButton("想听就听");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//设置输入窗口边框线*******************
audioPanel.add(music);
add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}
void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}
public void insert(MessageStyle attrib) {
try {
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//写完后接着换行************
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public MessageStyle getMessageStyle(String line) {
MessageStyle att = new MessageStyle();
att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗体")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜体")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜体")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}
String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("红色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("蓝色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黄色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("绿色")) {
att.setColor(new Color(0, 255, 0));
}
String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡红")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡蓝")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黄")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡绿")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}
}
本来不该直接给人作业的,所以一些格式很乱也不打算改了。框架已经写好,自己参考着加上三角形的功能吧(总得自己做点东西吧)。
/*
* Paint.java
*
* Created on May 9, 2008, 10:10 AM
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author uestcfb
*/
public class Paint extends JFrame {
ListShape list = new ArrayListShape();
private Point end;
class PaintArea extends JPanel {
ListShape list;
public PaintArea(ListShape list) {
this.list = list;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape s : list) {
s.paint(g);
}
}
}
private ShapeType selectedShape = ShapeType.values()[0];
/** Creates new form Paint */
public Paint() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox(ShapeType.values());
jPanel1 = new PaintArea(list);
jlebel1 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jPanel1MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jPanel1MouseReleased(evt);
}
});
jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jPanel1MouseDragged(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 445,
Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 388,
Short.MAX_VALUE));
jlebel1.setText("" + jComboBox1.getSelectedItem());
jlebel1.setToolTipText(getWarningString());
jLabel1.setText("Choose shape");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addContainerGap())
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout
.createSequentialGroup()
.addComponent(
jlebel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
134,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(
142,
142,
142))))
.addGroup(
layout
.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(
jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
103,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
159,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(105, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
21,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
18,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jlebel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
pack();
}// /editor-fold
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
selectedShape = (ShapeType) jComboBox1.getSelectedItem();
jlebel1.setText(selectedShape.toString());
}
Shape current = null;
private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
start = null;
end = null;
}
private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (start != null) {
end = evt.getPoint();
list.remove(current);
if (selectedShape == ShapeType.RECTANGLE) {
current = new Rectangle(start, end);
} else if (selectedShape == ShapeType.CIRCLE) {
current = new Circle(start, end);
}
if (current != null) {
list.add(current);
}
System.out.println(start + "," + end);
System.out.println("----");
repaint();
}
}
private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
start = evt.getPoint();
end = evt.getPoint();
if (selectedShape == ShapeType.RECTANGLE) {
current = new Rectangle(start, end);
} else if (selectedShape == ShapeType.CIRCLE) {
current = new Circle(start, end);
}
if (current != null) {
list.add(current);
}
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Paint().setVisible(true);
}
});
}
enum ShapeType {
RECTANGLE, CIRCLE;
}
abstract class Shape {
abstract protected void paint(Graphics g);
}
class Rectangle extends Shape {
Point p1;
Point p2;
Rectangle(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
@Override
protected void paint(Graphics g) {
g.drawRect((int) p1.getX(), (int) p1.getY(), (int) (p2.getX() - p1
.getX()), (int) (p2.getY() - p1.getY()));
}
}
class Circle extends Shape {
Point p1;
Point p2;
Circle(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
@Override
protected void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
double x1 = p1.getX();
double y1 = p1.getY();
double x2 = p2.getX();
double y2 = p2.getY();
g2.drawOval((int) x1, (int) y1, (int) x2, (int) y2);
}
}
// Variables declaration - do not modify
private javax.swing.JLabel jlebel1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
private Point start = null;
}