java实现画笔的画圆与矩形功能_JAVA怎么画出一个任意大小的圆形和矩形

展开全部

package test.xxl;

import java.awt.Button;

import java.awt.Color;

import java.awt.Cursor;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class Demo0617 extends JFrame implements MouseListener,ActionListener{

private static int x = 0 ;

private static int y = 0 ;

private static int w = 0 ;

private static int h = 0 ;

private static Color c ;

// 真为圆,假为方

private boolean flag = false ;

private static final long serialVersionUID = 1L;

public Demo0617(){

e68a84e8a2ad62616964757a686964616f31333361303639this.setSize(440, 500) ;

this.setVisible(true) ;

this.setLayout(null) ;

this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)) ;

this.setResizable(false);//不能改变窗体大小

this.setBackground(Color.WHITE) ;

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

this.addMouseListener(this) ;

this.getContentPane().setBackground(Color.WHITE) ;

Button b1,b2,b3,b4,b5,b6,b7,b8,b9 ;

b1 = new Button("红色") ;

b1.setBounds(0, 0, 100, 30) ;

b1.setBackground(Color.RED) ;

b1.addActionListener(this) ;

this.add(b1) ;

b2 = new Button("黑色") ;

b2.setBounds(110, 0, 100, 30) ;

b2.setBackground(Color.BLACK) ;

b2.addActionListener(this) ;

this.add(b2) ;

b3 = new Button("黄色") ;

b3.setBounds(220, 0, 100, 30) ;

b3.setBackground(Color.YELLOW) ;

b3.addActionListener(this) ;

this.add(b3) ;

b4 = new Button("蓝色") ;

b4.setBackground(Color.BLUE) ;

b4.setBounds(330, 0, 100, 30) ;

b4.addActionListener(this) ;

this.add(b4) ;

b5 = new Button("橡皮擦") ;

b5.setBounds(0, 40, 100, 30) ;

b5.addActionListener(this) ;

this.add(b5) ;

b6 = new Button("撤销") ;

b6.setBounds(110, 40, 100, 30) ;

b6.addActionListener(this) ;

this.add(b6) ;

b7 = new Button("全部删除") ;

b7.setBounds(220, 40, 100, 30) ;

b7.addActionListener(this) ;

this.add(b7) ;

b8 = new Button("圆形") ;

b8.setBounds(0, 80, 100, 30) ;

b8.addActionListener(this) ;

this.add(b8) ;

b9 = new Button("矩形") ;

b9.setBounds(110, 80, 100, 30) ;

b9.addActionListener(this) ;

this.add(b9) ;

}

/**

* @param args

*/

public static void main(String[] args) {

new Demo0617() ;

}

@Override

public void paint(Graphics g) {

if(c == null)

c = g.getColor();

g.setColor(c);

if(flag){

g.fillOval(x, y, w, h);

} else {

g.fillRect(x, y, w, h) ;

}

}

public void clear(Graphics g){

g.setColor(Color.WHITE) ;

g.clearRect(0, 0, 440, 500) ;

}

/**

* 单击

*/

@Override

public void mouseClicked(MouseEvent e) {

}

/**

* 按下

*/

@Override

public void mousePressed(MouseEvent e) {

x = e.getX() ;

y = e.getY() ;

}

/**

* 松开

*/

@Override

public void mouseReleased(MouseEvent e) {

int x = e.getX() ;

int y = e.getY() ;

if(x > this.x){

w = x - this.x ;

} else {

w = this.x - x ;

}

if(y > this.y){

h = y - this.y ;

} else {

h = this.y - y ;

}

paint(getGraphics()) ;

}

/**

* 鼠标进入事件

*/

@Override

public void mouseEntered(MouseEvent e) {

}

/**

* 鼠标移除事件

*/

@Override

public void mouseExited(MouseEvent e) {

}

@Override

public void actionPerformed(ActionEvent e) {

switch (e.getActionCommand().hashCode()) {

case 1038352:

// 红色

c = Color.RED ;

break;

case 1293761:

// 黑色

c = Color.BLACK ;

break;

case 1293358:

// 黄色

c = Color.YELLOW ;

break;

case 1087797:

// 蓝色

c = Color.BLUE ;

break;

case 27138585:

// 橡皮擦

c = Color.WHITE ;

break ;

case 836828:

Graphics graphics = getGraphics() ;

graphics.setColor(Color.WHITE) ;

if(flag){

graphics.fillOval(x, y, w, h) ;

} else {

graphics.fillRect(x, y, w, h) ;

}

break;

case 657183940:

// 全部删除

clear(getGraphics()) ;

break;

case 715036:

// 圆形

flag = true ;

break;

case 976025:

// 矩形

flag = false ;

break;

default:

System.out.println(e.getActionCommand().hashCode());

break ;

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值