方形

package com032603;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

import com032603.DrawBorder;
import com032603.DrawListener;

public class DrawBorder extends JFrame{
//定义主函数
public static void main(String[] args){
//实例化DrawBorder类的对象
DrawBorder db=new DrawBorder();
//调用initUI函数
db.initUI();
}
//定义一个initUI函数
public void initUI(){
//设置属性值
this.setTitle("");
this.setSize(800, 900);
this.setDefaultCloseOperation(3);
this.setResizable(true);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());

JButton butLine=new JButton("Line");
JButton butRect=new JButton("Rect");
JButton butOval=new JButton("Oval");
JButton butColor=new JButton("Color");

this.add(butLine);
this.add(butRect);
this.add(butOval);
this.add(butColor);

ActionListener a=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Color")){
color=JColorChooser.showDialog(null, "颜色的选择", Color.black);
System.out.println("Color="+color);
}
else{
str=e.getActionCommand();
System.out.println("str="+str);
}
}

};
butLine.addActionListener(a);
butRect.addActionListener(a);
butOval.addActionListener(a);
butColor.addActionListener(a);



this.setVisible(true);

Graphics g=this.getGraphics();
DrawListener d=new DrawListener(g);
this.addMouseListener(d);


}
public static Color color=Color.black;
public static String str="Line";

}


package com032603;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DrawListener implements MouseListener{
public int x1,x2,y1,y2;
public int x,y;
Graphics g;

public DrawListener(Graphics g){
this.g=g;
}


public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}

public void mouseReleased(MouseEvent e) {
System.out.print("ssss");
x2=e.getX();
y2=e.getY();
g.setColor(DrawBorder.color);

this.Draw(e);
}

public void Draw(MouseEvent e){
for(int i=0;i<100;i++){
if(x1!=x2||y1!=y2){

g.drawRect(x1, y1,Math.abs(x1-x2),Math.abs(y1-y2));

x1=x1+2;y1=y1+2;x2=x2-2;y2=y2-2;
}

}





}




public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}
public void mouseClicked(MouseEvent e) {

}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值