Program will recognize certain key inputs but not others.
程序应绘制用户制作的形状。 当用户按下"c"键时,应该能够在屏幕上拖动鼠标画一个圆圈。 当用户按下"r"时,他们应该绘制一个矩形。 当按下"l"时,应绘制一条线。 当按下"o"时,应制作椭圆形。 但是,由于某种原因,只能键"c"和"r"。 如果没有输入使用输入,则程序应默认为圆形。 然而,按"l"和"o"不会画椭圆形或线条。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /** * @(#)DrawShapes.java * * * @author * @version 1.00 2015/5/17 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.AlphaComposite; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.awt.geom.Ellipse2D; import java.awt.event.KeyEvent; import javax.swing.JComponent; import javax.swing.JFrame; public class DrawShapes extends JFrame implements KeyListener{ String key=""; public void keyPressed(KeyEvent event) { char temp = event.getKeyChar(); key = Character.toString(temp); } public void keyTyped (KeyEvent event) {} public void keyReleased (KeyEvent event) {} public DrawShapes() { this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(new PaintSurface(), BorderLayout.CENTER); this.setVisible(true); addKeyListener(this); } private class PaintSurface extends JComponent { ArrayList<Shape> shapes = new ArrayList<Shape>(); Point startDrag, endDrag; public PaintSurface() { this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { startDrag = new Point(e.getX(), e.getY()); endDrag = startDrag; repaint(); } public void mouseReleased(MouseEvent e) { Shape r; if (key.equals("l")) { r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("o")) { r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("r")) { r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else { r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } shapes.add(r); startDrag = null; endDrag = null; repaint(); } }); this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { endDrag = new Point(e.getX(), e.getY()); repaint(); } }); } private void paintBackground(Graphics2D g2){ g2.setPaint(Color.LIGHT_GRAY); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Shape s : shapes) { g2.setPaint(Color.BLACK); g2.draw(s); g2.fill(s); } if (startDrag != null && endDrag != null) { g2.setPaint(Color.LIGHT_GRAY); Shape r; if (key.equals("l")) { r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("o")) { r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("r")) { r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else { r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } g2.draw(r); } } private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) { return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } private Ellipse2D.Float makeCircle(int cx1, int cy1, int cx2, int cy2) { return new Ellipse2D.Float(Math.min(cx1, cx2), Math.min(cy1, cy2), Math.abs(cx1 - cx2), Math.abs(cx1 - cx2)); } private Ellipse2D.Float makeOval(int ox1, int oy1, int ox2, int oy2) { return new Ellipse2D.Float(Math.min(ox1, ox2), Math.min(oy1, oy2), Math.abs(ox1 - ox2), Math.abs(oy1 - oy2)); } private Line2D.Float makeLine(int lx1, int ly1, int lx2, int ly2) { return new Line2D.Float(Math.min(lx1, lx2), Math.min(ly1, ly2), Math.max(lx1, lx2), Math.max(ly1, ly2)); } } } |
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * @(#)ShapeViewer.java * * * @author * @version 1.00 2015/5/17 */ import javax.swing.*; public class ShapeViewer { //Creates and displays the application frame public static void main(String[] args) { JFrame ShapeViewer = new JFrame("Draw Stuff"); ShapeViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ShapeViewer.getContentPane().add(new DrawShapes()); ShapeViewer.pack(); ShapeViewer.setVisible(true); } } |
你的问题在这里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | if (key.equals("l")) { r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("o")) { r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } if (key.equals("r")) { r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else { r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } |
唯一可以使用的输入是
您需要使用else语句加入所有if语句。
1 2 3 4 5 6 7 8 9 | if (key.equals("l")) { r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else if (key.equals("o")) { r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else if (key.equals("r")) { r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } else { r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y); } |