java设计电子算盘_电子算盘,求教实现算珠的上拨与下拨

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这个是我做的全部代码,怎样都实现不了,谁有好想法,点拨点拨

package z4;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Toolkit;

import java.awt.event.*;

import javax.swing.*;

public class z4_1 extends JFrame implements MouseListener {

private static final long serialVersionUID = 1L;

int width = Toolkit.getDefaultToolkit().getScreenSize().width;

int height = Toolkit.getDefaultToolkit().getScreenSize().height;

int x = 0;

int y = 0;

int Flag = 0;

int suanZhu[][] = new int[4][4];

int suanZhu1[] = new int[4];

int suanZhu2[] = new int[4];

int suanZhu3[] = new int[4];

int suanZhu4[] = new int[4];

public z4_1() {

this.setTitle("电子算盘");

this.setLocation((width - 900) / 2, (height - 500) / 2);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setSize(900, 500);

this.setVisible(true);

this.addMouseListener(this);

}

public void paint(Graphics g) {

float lineWidth = 3.0f;

((Graphics2D) g).setStroke(new BasicStroke(lineWidth));

// 算盘边界

((Graphics2D) g).drawLine(50, 80, 480, 80);

((Graphics2D) g).drawLine(50, 179, 480, 179);

((Graphics2D) g).drawLine(50, 422, 480, 422);

((Graphics2D) g).drawLine(50, 80, 50, 422);

((Graphics2D) g).drawLine(480, 80, 480, 422);

// 画算珠

g.setColor(Color.ORANGE);

g.fillOval(84, 131, 65, 45);

g.fillOval(183, 131, 65, 45);

g.fillOval(282, 131, 65, 45);

g.fillOval(381, 131, 65, 45);

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

g.fillOval(84 + 99 * i, 230 + 48 * j, 65, 45);

if (Flag == 1) {

if (suanZhu1[i] == 1) {

g.fillOval(84, 182, 65, 45);

g.setColor(Color.WHITE);

g.fillOval(84, 230 + 48 * i, 65, 45);

suanZhu1[i] = 2;

Flag = 0;

} else if (suanZhu1[i] == 2) {

g.setColor(Color.ORANGE);

g.fillOval(84, 230 + 48 * i, 65, 45);

}

} else if (Flag == 2) {

if (suanZhu2[i] == 1) {

g.fillOval(183, 182, 65, 45);

g.setColor(Color.WHITE);

g.fillOval(183, 230 + 48 * i, 65, 45);

suanZhu2[i] = 2;

Flag = 0;

} else if (suanZhu2[i] == 2) {

g.setColor(Color.ORANGE);

g.fillOval(183, 230 + 48 * i, 65, 45);

}

} else if (Flag == 3) {

if (suanZhu3[i] == 1) {

g.fillOval(282, 182, 65, 45);

g.setColor(Color.WHITE);

g.fillOval(282, 230 + 48 * i, 65, 45);

suanZhu3[i] = 2;

Flag = 0;

} else if (suanZhu3[i] == 2) {

g.setColor(Color.ORANGE);

g.fillOval(282, 230 + 48 * i, 65, 45);

}

} else if (Flag == 4) {

if (suanZhu4[i] == 1) {

g.fillOval(381, 182, 65, 45);

g.setColor(Color.WHITE);

g.fillOval(381, 230 + 48 * i, 65, 45);

suanZhu4[i] = 2;

Flag = 0;

} else if (suanZhu4[i] == 2) {

g.setColor(Color.ORANGE);

g.fillOval(381, 230 + 48 * i, 65, 45);

}

}

}

}

}

public static void main(String[] args) {

new z4_1();

}

@Override

public void mouseClicked(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

System.out.println(e.getX());

System.out.println(e.getY());

x = e.getX();

y = e.getY();

// 实现点击那个算珠,就上移到最上面,然后该算珠消失,来模拟上拨动

if (x > 84 && y > 230 && x < 149 && y < 419) {

x = (x - 84) / 99;

y = (y - 230) / 48;

suanZhu1[y] = 1;

Flag = 1;

} else if (x > 183 && y > 230 && x < 248 && y < 419) {

x = (x - 84) / 99;

y = (y - 230) / 48;

suanZhu2[y] = 1;

Flag = 2;

} else if (x > 282 && y > 230 && x < 347 && y < 419) {

x = (x - 84) / 99;

y = (y - 230) / 48;

suanZhu3[y] = 1;

Flag = 3;

} else if (x > 381 && y > 230 && x < 446 && y < 419) {

x = (x - 84) / 99;

y = (y - 230) / 48;

suanZhu4[y] = 1;

Flag = 4;

} else {

JOptionPane.showMessageDialog(this, "请重新点击");

}

this.repaint();

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值