Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- public class DrawingPad2 extends DrawingPad
- {
- public DrawingPad2()
- {
- this(true);
- }
- public DrawingPad2(boolean isApplet)
- {
- super(isApplet);
- canvas.addKeyListener((KeyListener) listener);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame();
- frame.setTitle("Drawing Pad 2");
- frame.setBackground(Color.lightGray);
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add(new DrawingPad2(false), BorderLayout.CENTER);
- frame.addWindowListener(new AppCloser());
- frame.pack();
- frame.setSize(700, 500);
- frame.show();
- }
- protected EventListener makeCanvasListener(ScribbleCanvas canvas)
- {
- return new KeyToolListener(this, canvas);
- }
- protected void initActions()
- {
- actions = new Vector();
- actions.addElement(new ToolAction("Scribble",
- getImageIcon("scribble.gif"),
- "scribble tool", this,
- currentTool = new ScribbleTool()));
- actions.addElement(new ToolAction("Line", new ToolIcon(ToolIcon.LINE),
- "draw line segments", this,
- new TwoEndsShapeTool(new LineShape())));
- actions.addElement(new ToolAction("Rectangle", new ToolIcon(ToolIcon.RECT),
- "draw rectangles", this,
- new TwoEndsShapeTool(new RectShape())));
- actions.addElement(new ToolAction("Oval", new ToolIcon(ToolIcon.OVAL),
- "draw ovals", this,
- new TwoEndsShapeTool(new OvalShape())));
- actions.addElement(new ToolAction("Filled Rectangle",
- new ToolIcon(ToolIcon.FILLRECT),
- "draw filled rectangles", this,
- new TwoEndsShapeTool(new FilledRectShape())));
- actions.addElement(new ToolAction("Filled Oval",
- new ToolIcon(ToolIcon.FILLOVAL),
- "draw filled ovals", this,
- new TwoEndsShapeTool(new FilledOvalShape())));
- actions.addElement(new ToolAction("Text", new ToolIcon(ToolIcon.TEXT),
- "type text", this,
- new TextTool()));
- actions.addElement(new ToolAction("Eraser",
- getImageIcon("eraser.gif"),
- "eraser tool", this,
- new EraserTool()));
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.util.*;
- import javax.swing.*;
- public class DrawingPad extends Scribble2
- {
- protected Vector actions;
- protected Tool currentTool;
- public DrawingPad()
- {
- this(true);
- }
- public DrawingPad(boolean isApplet)
- {
- super(isApplet);
- if (!isApplet)
- {
- init();
- }
- }
- public void init()
- {
- initActions();
- getContentPane().add(makeToolBar(actions), BorderLayout.WEST);
- getContentPane().add(makeMenuBar(actions), BorderLayout.NORTH);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame();
- frame.setTitle("Drawing Pad");
- frame.setBackground(Color.lightGray);
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add(new DrawingPad(false), BorderLayout.CENTER);
- frame.addWindowListener(new AppCloser());
- frame.pack();
- frame.setSize(600, 400);
- frame.show();
- }
- public void setCurrentTool(Tool tool)
- {
- currentTool = tool;
- }
- public Tool getCurrentTool()
- {
- return currentTool;
- }
- protected EventListener makeCanvasListener(ScribbleCanvas canvas)
- {
- return new ToolListener(this, canvas);
- }
- protected ImageIcon getImageIcon(String filename)
- {
- if (isApplet)
- {
- try
- {
- URL url = new URL(getCodeBase(), filename);
- return new ImageIcon(url);
- } catch (MalformedURLException e)
- {
- return null;
- }
- }
- else
- {
- return new ImageIcon(filename);
- }
- }
- protected void initActions()
- {
- actions = new Vector();
- actions.addElement(new ToolAction("Scribble",
- getImageIcon("scribble.gif"),
- "scribble tool", this,
- currentTool = new ScribbleTool()));
- actions.addElement(new ToolAction("Line", new ToolIcon(ToolIcon.LINE),
- "draw line segments", this,
- new TwoEndsTool(TwoEndsTool.LINE)));
- actions.addElement(new ToolAction("Rectangle", new ToolIcon(ToolIcon.RECT),
- "draw rectangles", this,
- new TwoEndsTool(TwoEndsTool.RECT)));
- actions.addElement(new ToolAction("Oval", new ToolIcon(ToolIcon.OVAL),
- "draw ovals", this,
- new TwoEndsTool(TwoEndsTool.OVAL)));
- actions.addElement(new ToolAction("Eraser",
- getImageIcon("eraser.gif"),
- "eraser tool", this,
- new EraserTool()));
- }
- protected Component makeToolBar(Vector actions)
- {
- Box toolbar = new Box(BoxLayout.Y_AXIS);
- Enumeration enum = actions.elements();
- while (enum.hasMoreElements())
- {
- Action a = (Action) enum.nextElement();
- JButton button = new JButton((Icon) a.getValue(Action.DEFAULT));
- button.setToolTipText((String) a.getValue(Action.SHORT_DESCRIPTION));
- button.addActionListener(a);
- toolbar.add(button);
- }
- toolbar.add(Box.createVerticalGlue());
- return toolbar;
- }
- protected Component makeMenuBar(Vector actions)
- {
- JMenuBar menubar = new JMenuBar();
- JMenu tools = new JMenu("Tool");
- Enumeration enum = actions.elements();
- while (enum.hasMoreElements())
- {
- Action a = (Action) enum.nextElement();
- tools.add(a);
- }
- menubar.add(tools);
- JMenu help = new JMenu("Help");
- help.add(new AboutAction());
- menubar.add(help);
- return menubar;
- }
- class AboutAction extends AbstractAction
- {
- public AboutAction()
- {
- super("About");
- }
- public void actionPerformed(ActionEvent event)
- {
- JOptionPane.showMessageDialog(null, "DrawingPad version 1.0\nCopyright (c) Xiaoping Jia 1998", "About",
- JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- package drawingPad;
- import java.awt.*;
- public class EraserTool implements Tool
- {
- public void mousePressed(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = true;
- canvas.x = p.x;
- canvas.y = p.y;
- offscreen = canvas.getOffScreenGraphics();
- offscreen.setColor(Color.white);
- }
- public void mouseDragged(Point p, ScribbleCanvas canvas)
- {
- if (canvas.mouseButtonDown)
- {
- int x0, y0, dx, dy;
- x0 = Math.min(canvas.x, p.x) - 2;
- y0 = Math.min(canvas.y, p.y) - 2;
- dx = Math.abs(p.x - canvas.x) + 5;
- dy = Math.abs(p.y - canvas.y) + 5;
- offscreen.fillRect(x0, y0, dx, dy);
- canvas.repaint(x0, y0, dx, dy);
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
- public void mouseReleased(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = false;
- canvas.setPenColor(canvas.penColor);
- }
- Graphics offscreen;
- }
- package drawingPad;
- import java.awt.*;
- public class FilledOvalShape implements TwoEndsShape
- {
- public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.fillOval(x, y, w, h);
- }
- public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawOval(x, y, w, h);
- }
- }
- package drawingPad;
- import java.awt.*;
- public class FilledRectShape implements TwoEndsShape
- {
- public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.fillRect(x, y, w, h);
- }
- public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawRect(x, y, w, h);
- }
- }
- package drawingPad;
- public interface KeyEventTool extends Tool
- {
- void keyPressed(char c, ScribbleCanvas canvas);
- }
- package drawingPad;
- import java.awt.*;
- public class LineShape implements TwoEndsShape
- {
- public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
- {
- g.drawLine(x1, y1, x2, y2);
- }
- public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
- {
- g.drawLine(x1, y1, x2, y2);
- }
- }
- package drawingPad;
- import java.awt.*;
- public class OvalShape implements TwoEndsShape
- {
- public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawOval(x, y, w, h);
- }
- public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawOval(x, y, w, h);
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.EventListener;
- public class KeyToolListener extends ToolListener implements KeyListener
- {
- public KeyToolListener(DrawingPad drawingPad, ScribbleCanvas canvas)
- {
- super(drawingPad, canvas);
- }
- public void keyPressed(KeyEvent e)
- {
- Tool tool = drawingPad.getCurrentTool();
- if (tool != null && tool instanceof KeyEventTool)
- {
- KeyEventTool keyTool = (KeyEventTool) tool;
- keyTool.keyPressed((char) e.getKeyChar(), canvas);
- }
- }
- public void keyReleased(KeyEvent e) {}
- public void keyTyped(KeyEvent e) {}
- public void mouseClicked(MouseEvent e)
- {
- canvas.requestFocus();
- }
- }
- package drawingPad;
- import java.awt.*;
- public class RectShape implements TwoEndsShape
- {
- public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawRect(x, y, w, h);
- }
- public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
- {
- int x = Math.min(x1, x2);
- int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2) + 1;
- int h = Math.abs(y1 - y2) + 1;
- g.drawRect(x, y, w, h);
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.EventListener;
- import javax.swing.*;
- public class Scribble2 extends Scribble
- {
- public Scribble2()
- {
- this(true);
- }
- public Scribble2(boolean isApplet)
- {
- super(isApplet);
- getContentPane().add(makeControlPanel(), BorderLayout.SOUTH);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame();
- frame.setTitle("Scribble Pad 2");
- frame.setBackground(Color.lightGray);
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add(new Scribble2(false), BorderLayout.CENTER);
- frame.addWindowListener(new AppCloser());
- frame.pack();
- frame.setSize(600, 400);
- frame.show();
- }
- protected Component makeControlPanel()
- {
- JPanel controlPanel = new JPanel();
- JButton button = new JButton("Clear");
- controlPanel.add(button);
- button.addActionListener(new ClearAction());
- controlPanel.add(Box.createHorizontalStrut(20));
- controlPanel.add(new JLabel("Pen color"));
- JComboBox choice = new JComboBox();
- choice.addItem("black");
- choice.addItem("blue");
- choice.addItem("green");
- choice.addItem("red");
- choice.addItemListener(new ColorChoiceListener());
- Dimension d = choice.getPreferredSize();
- d.width += 10;
- choice.setPreferredSize(d);
- controlPanel.add(choice);
- return controlPanel;
- }
- class ClearAction extends AbstractAction
- {
- public ClearAction()
- {
- super("clear");
- }
- public void actionPerformed(ActionEvent event)
- {
- clearCanvas();
- }
- }
- class ColorChoiceListener implements ItemListener
- {
- public void itemStateChanged(ItemEvent event)
- {
- if ("black".equals(event.getItem()))
- canvas.setPenColor(Color.black);
- else if ("blue".equals(event.getItem()))
- canvas.setPenColor(Color.blue);
- else if ("green".equals(event.getItem()))
- canvas.setPenColor(Color.green);
- else if ("red".equals(event.getItem()))
- canvas.setPenColor(Color.red);
- }
- }
- public void clearCanvas()
- {
- canvas.clearCanvas();
- canvas.repaint();
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.EventListener;
- import javax.swing.*;
- public class Scribble extends JApplet
- {
- protected ScribbleCanvas canvas;
- protected EventListener listener;
- protected boolean isApplet = false;
- public Scribble()
- {
- this(true);
- }
- public Scribble(boolean isApplet)
- {
- this.isApplet = isApplet;
- getContentPane().setLayout(new BorderLayout());
- getContentPane().add(makeCanvas(), BorderLayout.CENTER);
- listener = makeCanvasListener(canvas);
- canvas.addMouseListener((MouseListener) listener);
- canvas.addMouseMotionListener((MouseMotionListener) listener);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame();
- frame.setTitle("Scribble Pad");
- frame.setBackground(Color.lightGray);
- frame.getContentPane().setLayout(new BorderLayout());
- frame.getContentPane().add(new Scribble(false), BorderLayout.CENTER);
- frame.addWindowListener(new AppCloser());
- frame.pack();
- frame.setSize(600, 400);
- frame.show();
- }
- protected Component makeCanvas()
- {
- canvas = new ScribbleCanvas();
- canvas.setBackground(Color.red);
- return canvas;
- }
- protected EventListener makeCanvasListener(ScribbleCanvas canvas)
- {
- return new ScribbleCanvasListener(canvas);
- }
- static class AppCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ScribbleCanvas extends JComponent
- {
- protected Image im;
- protected int width, height;
- protected Graphics offscreen;
- protected Color penColor = Color.black;
- protected boolean mouseButtonDown = false;
- protected int x, y;
- public void update(Graphics g)
- {
- if (im != null)
- {
- g.drawImage(im, 0, 0, this);
- }
- }
- public void paint(Graphics g)
- {
- update(g);
- }
- public Graphics getOffScreenGraphics()
- {
- return offscreen;
- }
- public void setBounds(int x, int y,int width, int height)
- {
- this.width = width;
- this.height = height;
- if (width > 0 && height > 0)
- {
- Image newim = createImage(width, height);
- offscreen = newim.getGraphics();
- clearCanvas();
- if (im != null)
- {
- offscreen.drawImage(im, 0, 0, this);
- }
- im = newim;
- }
- super.setBounds(x, y, width, height);
- repaint();
- }
- public void setPenColor(Color c)
- {
- penColor = c;
- offscreen.setColor(penColor);
- }
- public void clearCanvas()
- {
- offscreen.setColor(Color.white);
- offscreen.fillRect(0, 0, width, height);
- offscreen.setColor(penColor);
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- public class ScribbleCanvasListener implements MouseListener, MouseMotionListener
- {
- public ScribbleCanvasListener(ScribbleCanvas canvas)
- {
- this.canvas = canvas;
- }
- public void mousePressed(MouseEvent e)
- {
- Point p = e.getPoint();
- canvas.mouseButtonDown = true;
- canvas.x = p.x;
- canvas.y = p.y;
- }
- public void mouseReleased(MouseEvent e)
- {
- canvas.mouseButtonDown = false;
- }
- public void mouseDragged(MouseEvent e)
- {
- Point p = e.getPoint();
- if (canvas.mouseButtonDown)
- {
- canvas.getOffScreenGraphics().drawLine(canvas.x, canvas.y, p.x, p.y);
- int x0 = Math.min(canvas.x, p.x);
- int y0 = Math.min(canvas.y, p.y);
- int dx = Math.abs(p.x - canvas.x) + 1;
- int dy = Math.abs(p.y - canvas.y) + 1;
- canvas.repaint(x0, y0, dx, dy);
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
- public void mouseClicked(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public void mouseMoved(MouseEvent e) {}
- protected ScribbleCanvas canvas;
- }
- package drawingPad;
- import java.awt.*;
- public class ScribbleTool implements Tool
- {
- public void mousePressed(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = true;
- canvas.x = p.x;
- canvas.y = p.y;
- }
- public void mouseDragged(Point p, ScribbleCanvas canvas)
- {
- if (canvas.mouseButtonDown)
- {
- int x0, y0, dx, dy;
- x0 = Math.min(canvas.x, p.x);
- y0 = Math.min(canvas.y, p.y);
- dx = Math.abs(p.x - canvas.x) + 1;
- dy = Math.abs(p.y - canvas.y) + 1;
- canvas.getOffScreenGraphics().drawLine(canvas.x, canvas.y, p.x, p.y);
- canvas.repaint(x0, y0, dx, dy);
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
- public void mouseReleased(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = false;
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- class TextTool implements KeyEventTool
- {
- public void mousePressed(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = true;
- canvas.x = p.x;
- canvas.y = p.y;
- offscreen = canvas.getOffScreenGraphics();
- offscreen.setFont(font);
- text = new StringBuffer();
- }
- public void mouseReleased(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = false;
- }
- public void mouseDragged(Point p, ScribbleCanvas canvas) {}
- public void keyPressed(char c, ScribbleCanvas canvas)
- {
- text.append(c);
- offscreen.drawString(text.toString(), canvas.x, canvas.y);
- canvas.repaint(canvas.x, canvas.y - ascent - 1,
- fm.stringWidth(text.toString()) + 1,
- ascent + descent + 2);
- }
- protected StringBuffer text;
- protected Font font = new Font("Helvetica", Font.BOLD, 24);
- protected FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
- protected int ascent = fm.getAscent();
- protected int descent = fm.getDescent();
- protected Graphics offscreen;
- }
- package drawingPad;
- import java.awt.*;
- public interface Tool
- {
- void mousePressed(Point p, ScribbleCanvas canvas);
- void mouseReleased(Point p, ScribbleCanvas canvas);
- void mouseDragged(Point p, ScribbleCanvas canvas);
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.EventListener;
- import javax.swing.*;
- public class ToolAction extends AbstractAction
- {
- protected DrawingPad drawingPad;
- protected Tool tool;
- public ToolAction(String name, Icon icon, String tip, DrawingPad drawingPad, Tool tool)
- {
- super(name);
- putValue(Action.DEFAULT, icon);
- putValue(Action.SHORT_DESCRIPTION, tip);
- setEnabled(tool != null);
- this.drawingPad = drawingPad;
- this.tool = tool;
- }
- public void actionPerformed(ActionEvent event)
- {
- drawingPad.setCurrentTool(tool);
- }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ToolIcon implements Icon
- {
- public static final int SCRIBBLE = 0;
- public static final int LINE = 1;
- public static final int RECT = 2;
- public static final int OVAL = 3;
- public static final int FILLRECT = 4;
- public static final int FILLOVAL = 5;
- public static final int TEXT = 6;
- public static final int ERASER = 7;
- protected Dimension dim = new Dimension(36, 36);
- protected int id;
- public ToolIcon(int id)
- {
- this.id = id;
- }
- public void paintIcon(Component c, Graphics g, int x, int y)
- {
- Color oldColor = g.getColor();
- g.setColor(c.getBackground());
- g.fillRect(x, y, dim.width, dim.height);
- g.setColor(Color.black);
- int x0 = x + 2;
- int y0 = y + 2;
- int w0 = dim.width - 4;
- int h0 = dim.height - 4;
- switch (id)
- {
- case SCRIBBLE:
- break;
- case LINE:
- g.drawLine(x0, y0, x0 + w0, y0 + h0);
- break;
- case RECT:
- g.drawRect(x0, y0, w0, h0);
- break;
- case OVAL:
- g.drawOval(x0, y0, w0, h0);
- break;
- case FILLRECT:
- g.fillRect(x0, y0, w0, h0);
- break;
- case FILLOVAL:
- g.fillOval(x0, y0, w0, h0);
- break;
- case TEXT:
- g.setFont(new Font("Serif", Font.BOLD, h0));
- FontMetrics fm = g.getFontMetrics();
- g.drawString("T", x0, y0 + fm.getAscent());
- break;
- case ERASER:
- break;
- }
- g.setColor(oldColor);
- }
- public int getIconWidth() { return dim.width; }
- public int getIconHeight() { return dim.height; }
- }
- package drawingPad;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.EventListener;
- public class ToolListener extends ScribbleCanvasListener
- {
- protected DrawingPad drawingPad;
- public ToolListener(DrawingPad drawingPad, ScribbleCanvas canvas)
- {
- super(canvas);
- this.drawingPad = drawingPad;
- }
- public void mousePressed(MouseEvent e)
- {
- Tool tool = drawingPad.getCurrentTool();
- if (tool != null)
- {
- tool.mousePressed(e.getPoint(), canvas);
- }
- }
- public void mouseReleased(MouseEvent e)
- {
- Tool tool = drawingPad.getCurrentTool();
- if (tool != null)
- {
- tool.mouseReleased(e.getPoint(), canvas);
- }
- }
- public void mouseDragged(MouseEvent e)
- {
- Tool tool = drawingPad.getCurrentTool();
- if (tool != null)
- {
- tool.mouseDragged(e.getPoint(), canvas);
- }
- }
- }
- package drawingPad;
- import java.awt.*;
- public interface TwoEndsShape
- {
- void drawShape(Graphics g, int x1, int y1, int x2, int y2);
- void drawOutline(Graphics g, int x1, int y1, int x2, int y2);
- }
- package drawingPad;
- import java.awt.*;
- public class TwoEndsShapeTool implements Tool
- {
- protected int xStart, yStart;
- protected TwoEndsShape shape;
- protected Graphics onscreen;
- public TwoEndsShapeTool(TwoEndsShape shape)
- {
- this.shape = shape;
- }
- public void mousePressed(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = true;
- xStart = canvas.x = p.x;
- yStart = canvas.y = p.y;
- onscreen = canvas.getGraphics();
- onscreen.setXORMode(Color.darkGray);
- onscreen.setColor(Color.lightGray);
- if (shape != null)
- {
- shape.drawOutline(onscreen, xStart, yStart, xStart, yStart);
- }
- }
- public void mouseDragged(Point p, ScribbleCanvas canvas)
- {
- if (canvas.mouseButtonDown && shape != null)
- {
- shape.drawOutline(onscreen, xStart, yStart, canvas.x, canvas.y);
- shape.drawOutline(onscreen, xStart, yStart, p.x, p.y);
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
- public void mouseReleased(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = false;
- onscreen.setPaintMode();
- if (shape != null)
- {
- Graphics offscreen = canvas.getOffScreenGraphics();
- shape.drawShape(offscreen, xStart, yStart, p.x, p.y);
- canvas.repaint();
- }
- }
- }
- package drawingPad;
- import java.awt.*;
- public class TwoEndsTool implements Tool
- {
- public static final int LINE = 0;
- public static final int OVAL = 1;
- public static final int RECT = 2;
- protected int shape = LINE;
- protected int xStart, yStart;
- protected Graphics onscreen;
- public TwoEndsTool(int shape)
- {
- this.shape = shape;
- }
- public void mousePressed(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = true;
- xStart = canvas.x = p.x;
- yStart = canvas.y = p.y;
- onscreen = canvas.getGraphics();
- onscreen.setXORMode(Color.darkGray);
- onscreen.setColor(Color.lightGray);
- switch (shape)
- {
- case LINE:
- drawLine(onscreen, xStart, yStart, xStart, yStart);
- break;
- case OVAL:
- drawOval(onscreen, xStart, yStart, 1, 1);
- break;
- case RECT:
- drawRect(onscreen, xStart, yStart, 1, 1);
- break;
- }
- }
- public void mouseDragged(Point p, ScribbleCanvas canvas)
- {
- if (canvas.mouseButtonDown)
- {
- switch (shape)
- {
- case LINE:
- drawLine(onscreen, xStart, yStart, canvas.x, canvas.y);
- drawLine(onscreen, xStart, yStart, p.x, p.y);
- break;
- case OVAL:
- drawOval(onscreen, xStart, yStart, canvas.x - xStart + 1, canvas.y - yStart + 1);
- drawOval(onscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
- break;
- case RECT:
- drawRect(onscreen, xStart, yStart, canvas.x - xStart + 1, canvas.y - yStart + 1);
- drawRect(onscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
- break;
- }
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
- public void mouseReleased(Point p, ScribbleCanvas canvas)
- {
- canvas.mouseButtonDown = false;
- onscreen.setPaintMode();
- Graphics offscreen = canvas.getOffScreenGraphics();
- switch (shape)
- {
- case LINE:
- drawLine(offscreen, xStart, yStart, p.x, p.y);
- break;
- case OVAL:
- drawOval(offscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
- break;
- case RECT:
- drawRect(offscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
- break;
- }
- canvas.repaint();
- }
- public static void drawLine(Graphics g, int x1, int y1, int x2, int y2)
- {
- g.drawLine(x1, y1, x2, y2);
- }
- public static void drawRect(Graphics g, int x, int y, int w, int h)
- {
- if (w < 0)
- {
- x = x + w;
- w = -w;
- }
- if (h < 0)
- {
- y = y + h;
- h = -h;
- }
- g.drawRect(x, y, w, h);
- }
- public static void drawOval(Graphics g, int x, int y, int w, int h)
- {
- if (w < 0)
- {
- x = x + w;
- w = -w;
- }
- if (h < 0)
- {
- y = y + h;
- h = -h;
- }
- g.drawOval(x, y, w, h);
- }
- }
Add Comment
Please, Sign In to add comment