📄 whiteboard.java
字号:
yPoints[3] = endY + dy; // Fill the rectangle, effectively drawing a thick line g.fillPolygon(xPoints, yPoints, 4); } public void drawLine(Graphics g, int wid, Color col) { drawLine(g, startX, startY, endX, endY, wid, col); } public void drawLine(Graphics g) { drawLine(g, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor()); } /** * Apply the line tool to the canvas events passed to it. * Apply both to the canvas, and to the double-buffer. */ public void applyTool(Event e, Graphics g, Graphics bg) { super.applyTool(e, g, bg); if (e.id == Event.MOUSE_DOWN) { // Save starting (and ending) location... startX = e.x; startY = e.y; endX = e.x; endY = e.y; } else if (e.id == Event.MOUSE_DRAG) { // // Draw a gray line from the starting location to the current location // g.setXORMode(Color.gray); // Erase previous line g.drawLine(startX, startY, endX, endY); // Update the ending location endX = e.x; endY = e.y; // Draw new line g.drawLine(startX, startY, endX, endY); } else if (e.id == Event.MOUSE_UP) { // // Draw a line in the current color, from starting location to current // // Erase the gray line g.setXORMode(Color.gray); g.drawLine(startX, startY, endX, endY); // Draw the real line, on the display g.setPaintMode(); drawLine(g); // Update the off-screen buffer bg.setPaintMode(); drawLine(bg); } } /** * Construct a command that encapsulates the tool operation. */ public String makeCommandString(Event e) { if (e.id != Event.MOUSE_UP) return null; Command cmd = new Command(toolId, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor(), startX, startY, endX, endY); return cmd.makeCommandString(); } /** * Apply a command to the specified graphics context. */ public void applyCommand(Command cmd, Graphics g, Graphics bg) { if (TestData.TESTMODE) System.out.println("Applying line cmd: from (" + cmd.startX + "," + cmd.startY + ") to (" + cmd.endX + "," + cmd.endY + ")"); drawLine(g, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); drawLine(bg, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); } /** * Return the name of this tool. */ public String getToolName() { return toolName; }}/** * Allow the user to draw a box, using the current color and line thickness. */class BoxTool extends ToolCanvas{ final static String toolName = "BoxTool"; final static int toolId = 4; // Tool state: int startX; int startY; int endX; int endY; private Rectangle rect; /** * Construct the box-drawing tool. */ public BoxTool(Tools t) { super(t); setBackground(Color.yellow); resize(width, height); rect = new Rectangle(); dim.width = width; dim.height = height; } Dimension getDimension() { return dim; } /** * (Re)paint the line-drawing tool icon. */ public void paint(Graphics g) { g.setColor(Color.white); g.fill3DRect(0, 0, width-1, height-1, true); g.setColor(Color.black); g.drawRect(4, 4, width-9, height-9); } /** * Draw a rectangle, using the current line tool thickness */ public void drawBox(Graphics g, int startX, int startY, int endX, int endY, int wid, Color c) { // Make this reflect the current width and color... g.setColor(c); // Draw a thick box by drawing successively smaller boxes... Helper.makeRect(startX, startY, endX, endY, rect); for (int i = 0; i < wid; i++) { if ((rect.width < 0) || (rect.height < 0)) break; g.drawRect(rect.x, rect.y, rect.width, rect.height); rect.x++; rect.y++; rect.width -= 2; rect.height -= 2; } } public void drawBox(Graphics g) { drawBox(g, startX, startY, endX, endY, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor()); } /** * Apply canvas events to the canvas and the double buffer. */ public void applyTool(Event e, Graphics g, Graphics bg) { super.applyTool(e, g, bg); if (e.id == Event.MOUSE_DOWN) { // Save starting (and ending) location... startX = e.x; startY = e.y; endX = e.x; endY = e.y; } else if (e.id == Event.MOUSE_DRAG) { // // Draw a gray box from the starting location to the current location // g.setXORMode(Color.gray); // Erase previous line Helper.makeRect(startX, startY, endX, endY, rect); g.drawRect(rect.x, rect.y, rect.width, rect.height); // Update the ending location endX = e.x; endY = e.y; // Draw new line Helper.makeRect(startX, startY, endX, endY, rect); g.drawRect(rect.x, rect.y, rect.width, rect.height); } else if (e.id == Event.MOUSE_UP) { // // Draw a box in the current color, from starting location to current // // Erase the gray box g.setXORMode(Color.gray); Helper.makeRect(startX, startY, endX, endY, rect); g.drawRect(rect.x, rect.y, rect.width, rect.height); // Draw the real box, on the screen g.setPaintMode(); drawBox(g); // Update the off-screen buffer bg.setPaintMode(); drawBox(bg); } } /** * Construct a command that encapsulates the tool operation. */ public String makeCommandString(Event e) { if (e.id != Event.MOUSE_UP) return null; Command cmd = new Command(toolId, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor(), startX, startY, endX, endY); return cmd.makeCommandString(); } /** * Apply a command to the specified graphics context. */ public void applyCommand(Command cmd, Graphics g, Graphics bg) { if (TestData.TESTMODE) System.out.println("Applying box cmd: from (" + cmd.startX + "," + cmd.startY + ") to (" + cmd.endX + "," + cmd.endY + ")"); drawBox(g, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); drawBox(bg, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); } /** * Return the name of this tool. */ public String getToolName() { return toolName; }}/** * Allow the user to draw ovals, using the current color and line thickness. */class OvalTool extends ToolCanvas{ final static String toolName = "OvalTool"; final static int toolId = 5; // Tool state: int startX; int startY; int endX; int endY; private Rectangle rect; /** * Construct the oval-drawing tool. */ public OvalTool(Tools t) { super(t); setBackground(Color.yellow); resize(width, height); rect = new Rectangle(); dim.width = width; dim.height = height; } Dimension getDimension() { return dim; } /** * (Re)paint the oval-drawing tool icon. */ public void paint(Graphics g) { g.setColor(Color.white); g.fill3DRect(0, 0, width-1, height-1, true); g.setColor(Color.black); g.drawOval(4, 4, width-9, height-9); } /** * Draw an oval, using the current color and line tool thickness */ public void drawOval(Graphics g, int startX, int startY, int endX, int endY, int wid, Color c) { // Make this reflect the current width and color... g.setColor(c); // Draw a thick oval by drawing successively smaller ovals... Helper.makeRect(startX, startY, endX, endY, rect); for (int i = 0; i < wid; i++) { if ((rect.width < 0) || (rect.height < 0)) break; g.drawOval(rect.x, rect.y, rect.width, rect.height); rect.x++; rect.y++; rect.width -= 2; rect.height -= 2; } } public void drawOval(Graphics g) { drawOval(g, startX, startY, endX, endY, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor()); } /** * Apply canvas events to the canvas and the double buffer. */ public void applyTool(Event e, Graphics g, Graphics bg) { super.applyTool(e, g, bg); if (e.id == Event.MOUSE_DOWN) { // Save starting (and ending) location... startX = e.x; startY = e.y; endX = e.x; endY = e.y; } else if (e.id == Event.MOUSE_DRAG) { // // Draw a gray oval from the starting location to the current location // g.setXORMode(Color.gray); // Erase previous line Helper.makeRect(startX, startY, endX, endY, rect); g.drawOval(rect.x, rect.y, rect.width, rect.height); // Update the ending location endX = e.x; endY = e.y; // Draw new line Helper.makeRect(startX, startY, endX, endY, rect); g.drawOval(rect.x, rect.y, rect.width, rect.height); } else if (e.id == Event.MOUSE_UP) { // // Draw an oval in the current color, from starting location to current // // Erase the gray oval g.setXORMode(Color.gray); Helper.makeRect(startX, startY, endX, endY, rect); g.drawOval(rect.x, rect.y, rect.width, rect.height); // Draw the real box, on the screen g.setPaintMode(); drawOval(g); // Update the off-screen buffer bg.setPaintMode(); drawOval(bg); } } /** * Construct a command that encapsulates the tool operation. */ public String makeCommandString(Event e) { if (e.id != Event.MOUSE_UP) return null; Command cmd = new Command(toolId, tools.lineWidthTool.curLineWidth(), tools.colorPaletteTool.curColor(), startX, startY, endX, endY); return cmd.makeCommandString(); } /** * Apply a command to the specified graphics context. */ public void applyCommand(Command cmd, Graphics g, Graphics bg) { if (TestData.TESTMODE) System.out.println("Applying oval cmd: from (" + cmd.startX + "," + cmd.startY + ") to (" + cmd.endX + "," + cmd.endY + ")"); drawOval(g, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); drawOval(bg, cmd.startX, cmd.startY, cmd.endX, cmd.endY, cmd.lineWidth, cmd.color); } /** * Return the name of this tool. */ public String getToolName() { return toolName; }}/** * A dialog box for selecting a font and font size. */class FontSelectorDialog extends Dialog{ FontSelector fontSelector; // the object that creates this Frame frame; // the frame that owns this dialog Choice fontChoice; // for displaying the list of available fonts TextField ptSizeBox; // for entering a point size Button okButton; // when user wants changes to take effect Button cancelButton; // when user wants to exit the dialog String fontName; // the chosen font name int fontSize; // the chosen font size /** * Construct a font selector dialog box. */ public FontSelectorDialog(Frame f, FontSelector fs) { super(f, true); fontSelector = fs; frame = f; setTitle("Select A Font"); setLayout(new FlowLayout()); // Add a choice of available fonts... fontChoice = new Choice(); for (int i = 0; i < fontSelector.fontList.length; i++) { fontChoice.addItem(fontSelector.fontList[i]); } add(fontChoice); // Add a text field for entering a font size... ptSizeBox = new TextField(3); ptSizeBox.setText(String.valueOf(fontSelector.font.getSize())); ptSizeBox.setEditable(true); add(ptSizeBox); // Add OK and Cancel buttons add(okButton = new Button("OK")); add(cancelButton = new Button("Cancel")); resize(300, 80); } /** * Handle OK and Cancel button events. */ public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { // Destroy the dialog without making any changes frame.remove(this); dispose(); fontSelector.fontSelectorDialog = null; frame.show(); return true; } else if (e.id == Event.ACTION_EVENT) { if (e.target == okButton) { // Retrieve the new values from the dialog fontName = fontChoice.getSelectedItem(); try {fontSize = Integer.parseInt(ptSizeBox.getText().trim());} catch (NumberFormatException ex) { System.out.println("Illegal font size"); return true; } // Change the font fontSelector.font = new Font(fontName, Font.BOLD, fontSize); System.out.println("Font set to " + fontSelector.curFont().getName()); return true; } if (e.target == cancelButton) { // Destroy the window without making any changes frame.remove(this); dispose(); fontSelector.fontSelectorDialog = null; frame.show(); return true; } } return super.handleEvent(e); }}/** * Allow the user to select a font style and a font size. When the user clicks * on this tool's icon, a FontSelectorDialog is created. * This tool maintains the current font state of the Whiteboard. */class FontSelector extends ToolCanvas{ final static String toolName = "FontSelector"; final static int toolId = 7; // Tool state: Font font; // the current font Font iconFont; // font to use for drawing the icon String[] fontList; // the available fonts FontSelectorDialog fontSelectorDialog = null; /** * Construct a font selector tool. */ public FontSelector(Tools t) { super(t); setBackground(Color.darkGray); fontList = getToolkit().getFontList(); iconFont = new Font("Times", Font.BOLD, 16); font = iconFont; resize(width, height); dim.width = width; dim.height = height; } Dimension getDimension() { return dim; } /** * Return the current font. */ public Font curFont() { return font; } /** * (Re)paint the font selector tool icon when necessary. */ public void paint(Graphics g) { // Draw a little box with a "ff" in it super.paint(g); g.setColor(Color.black); g.setFont(iconFont); g.drawString("ff", 5, height-3); } /** * Handle font tool selection events. */ public boolean handleEvent(Event e) { // If the mouse is pressed, diaplay a dialog box that allows the // user to select a font and a font size. if (e.id == Event.MOUSE_DOWN) { // if a font dialog box already exists, don't create another... if (fontSelectorDialog != null) return true; // Find the owning Frame (the dialog box needs this)... Component p; for (p = this;;) { if ((p = p.getParent()) instanceof Frame) break; } Frame f = (Frame)p; // Create a dialog box for choosing a font... fontSelectorDialog = new FontSelectorDialog(f, this); f.add(fontSelectorDialog); fontSelectorDialog.show(); return true; } return super.handleEvent(e); } /** * Return the name of this tool */ public String getToolName() { return toolName; }}/** * Text is drawn in the current color, font and font size. */class TextTool extends ToolCanvas{ final static String toolName = "TextTool"; final static int toolId = 8; // Tool state Font iconFont; int startX = 0; int startY = 0; /** * Construct the text tool. */ public TextTool(Tools t) { super(t); setBackground(Color.orange); iconFont = new Font("Times", Font.BOLD, 16); if (iconFont == null) System.out.println("Unable to load Times font"); resize(width, height); dim.width = width; dim.height = height; } Dimension getDimension() { return dim; } /** * (Re)paint the text tool icon. */ public void paint(Graphics g) { // Draw a little box with a "T" in it super.paint(g); g.setColor(Color.black); g.setFont(iconFont); g.drawString("T", 5, height-3); } /** * Apply canvas events to the canvas and the double buffer. */ public void applyTool(Event e, Graphics g, Graphics bg) { super.applyTool(e, g, bg); char[] c = {' '}; if (e.id == Event.MOUSE_DOWN) { // Update the text start location startX = e.x; startY = e.y; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -