//Page 551 for update ideas import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.math.BigInteger; public class CCanvas extends Canvas implements MouseMotionListener, MouseListener { private int xShape, yShape, canW = 650, canH= 300; public boolean draw = false; private Applet app; private Color color = Color.pink; public xyArray array; private int group = 1; public int offX, offY; private static BigInteger ZERO = new BigInteger("0"); private static BigInteger ONE = new BigInteger("1"); private static BigInteger NEGONE = new BigInteger("-1"); public CCanvas(Applet a) { app = a; addMouseMotionListener(this); addMouseListener(this); repaint(); array = new xyArray(40); } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, canW, canH); g.setColor(Color.black); int n = 0, p = 0; for(int x = 5; x < canW; x += 10) { for(int y = 5; y < canH; y +=10) { if((y%20 == 15) && (offY%2 == 0) || (y%20 == 5) && (offY%2 == 1)) { n = (int)y/10 + 1 + offY; p = (int)(x+5)/10 + 1 - 32 + (int)n/2 +n%2 + offX; if(0 <= p && p <= n) g.fillOval(x + 5, y, 2, 2); } if((y%20 == 5) && (offY%2 == 0) || (y%20 == 15) && (offY%2 == 1)) { n = (int)y/10 + 1 + offY; p = (int)x/10 + 1 - 32 + (int)n/2 +n%2 + offX; if(0 <= p && p <= n) g.fillOval(x, y, 2, 2); } } } int size = array.getSize(); int gr = 0, x = 0, y = 0; Color tempColor = color; for(int i = 0; i < size; i ++) { n = array.getN(i); p = array.getP(i); gr = array.getG(i); setColor(gr); g.setColor(color); y = (n - 1 - offY) * 10 + 5; x = (p + 32 - 1 - n%2 - (int)n/2 -offX)*10 + (n%2)*5; if((x > 0) && (x < canW) && (y > 0) && (y < canH)) g.fillOval(x, y, 4, 4); } color = tempColor; g.setColor(color); if(draw) { draw = false; if(array.reMove) { g.setColor(Color.white); g.fillOval(xShape, yShape, 4, 4); g.setColor(Color.black); g.fillOval(xShape, yShape, 2, 2); g.setColor(color); array.reMove = false; } else { g.setColor(color); g.fillOval(xShape, yShape, 4, 4); } } } public void update(Graphics g){ paint(g);} public void draw(int x, int y) { draw = true; xShape = x; yShape = y; repaint(); } public void setColor(int g) { if(g == 1) color = Color.pink; if(g == 2) color = Color.blue; if(g == 3) color = Color.black; if(g == 4) color = Color.red; if(g == 5) color = Color.green; } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { int y, x, n = 0, p = 0; y = e.getY(); x = e.getX(); if(((y%10 < 5) && (offY%2 == 0)) || ((y%10 > 5) && (offY%2 == 1))) { y = (int)(y/5) + 1 - offY%2; y *= 5; } else if(((y%10 > 5) && (offY%2 == 0)) || ((y%10 < 5) && (offY%2 == 1))) { y = (int)(y/5) + offY%2; y *= 5; } if(((y%20 == 5) && (offY%2 == 0)) || ((y%20 == 15) && (offY%2 == 1))) { if(x%10 < 5) { x = (int)(x/5) + 1; x *= 5; } else if(x%10 > 5) { x = (int)(x/5); x *= 5; } } else if(((y%20 == 15) && (offY%2 == 0)) || ((y%20 == 5) && (offY%2 == 1))) { if( x%10 < 5) { x = (int)(x/5); x *= 5; } else if( x%10 >= 5) { x = (int)(x/5) + 1; x *= 5; } } n = (int)y/10 + 1 + offY; p = (int)x/10 + 1 - 32 + (int)n/2 + n%2 + offX; if(0 <= p && p <= n) app.showStatus(" n " + n + " p " + p); else app.showStatus(" Out of range"); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { int y, x, n = 0, p = 0; y = e.getY(); x = e.getX(); if(((y%10 < 5) && (offY%2 == 0)) || ((y%10 > 5) && (offY%2 == 1))) { y = (int)(y/5) + 1 - offY%2; y *= 5; } else if(((y%10 > 5) && (offY%2 == 0)) || ((y%10 < 5) && (offY%2 == 1))) { y = (int)(y/5) + offY%2; y *= 5; } if(((y%20 == 5) && (offY%2 == 0)) || ((y%20 == 15) && (offY%2 == 1))) { if(x%10 < 5) { x = (int)(x/5) + 1; x *= 5; } else if(x%10 > 5) { x = (int)(x/5); x *= 5; } } else if(((y%20 == 15) && (offY%2 == 0)) || ((y%20 == 5) && (offY%2 == 1))) { if( x%10 < 5) { x = (int)(x/5); x *= 5; } else if( x%10 >= 5) { x = (int)(x/5) + 1; x *= 5; } } n = (int)y/10 + 1 + offY; p = (int)x/10 + 1 - 32 + (int)n/2 +n%2 + offX; if(0 <= p && p <= n) { array.addElement(n, p, group); draw(x, y); } } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void setGroup(int g) { group = g; setColor(group); } public void getLcmGcd(TextField gcd, TextField lcm, int g) { BigInteger gcdlcm[] = new BigInteger [2]; array.calcLcmGcd(g, gcdlcm); if(gcdlcm[0] != NEGONE) { gcd.setText(gcdlcm[0].toString()); lcm.setText(gcdlcm[1].toString()); } else if(gcdlcm[0] == NEGONE) { gcd.setText("big prime fact"); lcm.setText("big prime fact"); } } public void redraw() { repaint(); } public void clear() { array.clear(); repaint(); } public int move(int direction) { int temp = array.move(direction); if(temp == 1) { redraw(); return 1; } else return 0; } public void report(int begRow, int endRow, TextArea txtReport, String colDel, boolean delimited) { array.report(begRow, endRow, txtReport, colDel, delimited); } }