📄 javacoursedesign.java
字号:
package kao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
public class javacoursedesign extends Frame implements ActionListener,TextListener{
String des[]={"红色","蓝色","绿色","灰色","黑色"}; //设置字体
Color c[]={Color.red,Color.blue,Color.green,Color.gray,Color.black};
String wds[]={"华文彩云","华文行楷","隶书","方正舒体","幼圆"};
String ziti="宋体";
String s="";
int daxiao=45;
String sz[]={"20","30","40","50","60"};
int star;
private int orginx;private int orginy;private int x,y;
protected int lastx=0,lasty=0;
Label hint1;
Label hint2;
Label hint3;
Color scolor=Color.black; //初始颜色
TextField shuru;
Button triangular;
Button rect; //按钮
Button circle;
Button clear;
Button line;
private Image offscreens; //双缓冲用的Image
private Graphics og; //双缓冲用的Graphics
public javacoursedesign(){
Menu file = new Menu("file"); //创建菜单
Menu help = new Menu("help");
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
menubar.add(file);
menubar.add(help);
MenuItem save = new MenuItem("save");
MenuItem open = new MenuItem("open");
MenuItem del=new MenuItem("清除");
file.add(save); //添加菜单至容器
file.add(open);
file.addSeparator();
file.add(del);
this.addWindowListener(new WindowAdapter() { //添加关闭窗口动作
public void windowClosing(WindowEvent e) {
Frame frm = (Frame) (e.getSource());
frm.dispose();
}
});
triangular = new Button("三角型");
rect = new Button("矩形");
circle = new Button("圆形");
clear = new Button("清除");
line=new Button("直线");
hint1 = new Label("字体");
hint2 = new Label("颜色");
hint3 = new Label("大小");
Choice font = new Choice(); //创建下拉菜单,并把相应数组元素添加至里
for (int i = 0; i < wds.length; i++) {
font.add(wds[i]);
}
Choice color = new Choice();
for (int i = 0; i < des.length; i++) {
color.add(des[i]);
}
Choice size = new Choice();
for (int i = 0; i < sz.length; i++) {
size.add(sz[i]);
}
setLayout(new BorderLayout()); //才用flowLayout布局
Panel p2=new Panel();
shuru=new TextField(25);
Panel p1 = new Panel(); //创建面板
p1.setLayout(new GridLayout(1, 10));
p1.add(rect);
p1.add(circle);
p1.add(line);
p1.add(triangular);
p1.add(clear);
p1.add(hint1);
p1.add(font);
p1.add(hint2);
p1.add(color);
p1.add(hint3);
p1.add(size);
this.add("North",p1);
p2.add(shuru);
this.add("South",p2);
shuru.addTextListener(this);shuru.addActionListener(this); //为文本框注册事件监听者
rect.addActionListener(new ActionListener() //为按钮注册事件监听者
{
public void actionPerformed(ActionEvent e)
{
star=1;
}
});
circle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
star=2;
}
});
line.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
star = 3;
}
});
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
star = 4;
}
});
triangular.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
star = 5;
}
});
font.addItemListener(new ItemListener(){ //处理下拉菜单事件
public void itemStateChanged(ItemEvent e) {
Choice temp=(Choice)e.getItemSelectable();
for(int i=0;i<wds.length;i++){ //处理字体改变
if(temp.getSelectedIndex()==i){
ziti=wds[i];
break;
}
}
}
});
color.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
Choice temp=(Choice)e.getItemSelectable();
for(int i=0;i<wds.length;i++){ //处理字体改变
if(temp.getSelectedIndex()==i){
scolor=c[i];
break;
}
}
}
});
size.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
Choice temp=(Choice)e.getItemSelectable();
for(int i=0;i<wds.length;i++){ //处理字体改变
if(temp.getSelectedIndex()==i){
daxiao=Integer.parseInt(sz[i]);
break;
}
}
}
});
del.addActionListener(this);
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orginx = e.getX();
orginy = e.getY();
Graphics g=getGraphics();
Font myfont=new Font(ziti,Font.BOLD,daxiao); //创建字体对象的一个实例
g.setFont(myfont); //确定使用的字体
g.setColor(scolor);
g.drawString(s,orginx,orginy);
}
public void mouseReleased(MouseEvent arg0) {
switch(star){ //将图像保存到双缓冲区
case 1:
og.setColor(scolor);
og.drawRect(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 2:
og.setColor(scolor);
og.drawOval(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 3:
og.setColor(scolor);
og.drawLine(orginx,orginy,x,y);
break;
case 4:
og.setColor(Color.white);
og.fillRect(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 5:
og.setColor(Color.white);
og.drawLine((int)((x+orginx)/2),Math.min(y,orginy),Math.max(x,orginx),Math.max(y,orginy));
og.drawLine(Math.max(x,orginx),Math.max(y,orginy),Math.min(x,orginx),Math.max(y,orginy));
og.drawLine(Math.min(x,orginx),Math.max(y,orginy),(int)((x+orginx)/2),Math.min(y,orginy));
}
}
});
this.setSize(780,450);
this.setVisible(true);
offscreens=createImage(getSize().width,getSize().height); //创建与面板同样大小的缓冲区
og=offscreens.getGraphics();
}
public void textValueChanged(TextEvent e){ //为文本框加入事件
if(e.getSource()==shuru){
s=" "+shuru.getText();
}
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(offscreens,0,0,this);
switch(star){ //对应SWITCH方法双缓冲画不同的图形
case 1:
g.setColor(scolor);
g.drawRect(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 2:
g.setColor(scolor);
g.drawOval(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 3:
g.setColor(scolor);
g.drawLine(orginx,orginy,x,y);
break;
case 4:
g.setColor(Color.white);
g.fillRect(Math.min(orginx,x),Math.min(orginy,y),Math.abs(x-orginx),Math.abs(y-orginy));
break;
case 5:
g.setColor(Color.white);
g.drawLine((int)((x+orginx)/2),Math.min(y,orginy),Math.max(x,orginx),Math.max(y,orginy));
g.drawLine(Math.max(x,orginx),Math.max(y,orginy),Math.min(x,orginx),Math.max(y,orginy));
g.drawLine(Math.min(x,orginx),Math.max(y,orginy),(int)((x+orginx)/2),Math.min(y,orginy));
}
}
public void actionPerformed(ActionEvent e){ //为菜单中的清除项添加事件
Graphics g=this.getGraphics();
if(e.getActionCommand()=="清除"){
g.setColor(Color.white);
og.setColor(Color.white);
og.fillRect(0,0,this.getSize().width,this.getSize().height);
g.fillRect(0,0,this.getSize().width,this.getSize().height);
}
}
public static void main(String[] args){
new javacoursedesign();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -