📄 drawfigure.java
字号:
y = e.getY(); //获得新位置 g456.setColor(c); //设置色彩 g456.setPaintMode(); //设置直接绘图模式 g456.drawLine(sx, sy, x, y); //画最后的直线 Lines lines = new Lines(); //将所画直线存入栈中 lines.setValue(sx, sy, x, y, c); stack.push(lines); saved=false; break; case 3: //画椭圆 case 4: //画矩形 g456.setXORMode(Color.white); //设置异或画图方式 x0 = x > sx ? sx : x; y0 = y > sy ? sy : y; //计算上次的左上角坐标 if (!fill) { if (f == 3) { g456.drawOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g456.drawRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } else { if (f == 3) { g456.fillOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g456.fillRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } g456.setPaintMode(); //设置异或画图方式 x = e.getX(); y = e.getY(); x0 = x > sx ? sx : x; y0 = y > sy ? sy : y; //得到最后的左上角坐标 g456.setColor(c); if (!fill) { if (f == 3) { g456.drawOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); Circle circle = new Circle(); //将所画园存入栈中 circle.setValue(sx, sy, x, y, c,fill); stack.push(circle); saved=false; } else { g456.drawRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); Rectangle rectangle = new Rectangle(); //将所画矩形存入栈中 rectangle.setValue(sx, sy, x, y, c, fill); stack.push(rectangle); saved=false; } } else { if (f == 3) { g456.fillOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); Circle circle = new Circle(); //将所画园存入栈中 circle.setValue(sx, sy, x, y, c, fill); stack.push(circle); saved=false; } else { g456.fillRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); Rectangle rectangle = new Rectangle(); //将所画矩形存入栈中 rectangle.setValue(sx, sy, x, y, c, fill); stack.push(rectangle); saved=false; } } break; } }public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseClicked(MouseEvent e){}public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e) { int x0, y0; Graphics g123 = center.getGraphics(); switch (f) { case 1: //任意绘图 x = e.getX(); y = e.getY(); g123.setColor(c); g123.setPaintMode(); g123.drawLine(sx, sy, x, y); Lines lines = new Lines(); //将直线放入栈中。 lines.setValue(sx, sy, x, y, c); stack.push(lines); saved=false; sx = x; sy = y; break; case 2: //绘制直线 if (!flag) { x = e.getX(); y = e.getY(); g123.setXORMode(Color.white); g123.drawLine(sx, sy, x, y); flag = true; } else { g123.setXORMode(Color.white); g123.drawLine(sx, sy, x, y); x = e.getX(); y = e.getY(); g123.setXORMode(Color.white); g123.drawLine(sx, sy, x, y); } break; case 3: //绘制圆 case 4: //绘制矩形 if (!flag) { x = e.getX(); y = e.getY(); x0 = x > sx ? sx : x; y0 = y > sy ? sy : y; g123.setXORMode(Color.white); if (!fill) { if (f == 3) { g123.drawOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.drawRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } else { if (f == 3) { g123.fillOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.fillRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } flag = true; } else { g123.setXORMode(Color.white); x0 = x > sx ? sx : x; y0 = y > sy ? sy : y; if (!fill) { if (f == 3) { g123.drawOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.drawRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } else { if (f == 3) { g123.fillOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.fillRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } x = e.getX(); y = e.getY(); x0 = x > sx ? sx : x; y0 = y > sy ? sy : y; if (!fill) { if (f == 3) { g123.drawOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.drawRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } else { if (f == 3) { g123.fillOval(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } else { g123.fillRect(x0, y0, Math.abs(x - sx), Math.abs(y - sy)); } } } break; } }public void actionPerformed(ActionEvent e){f=Integer.parseInt(e.getActionCommand());}public void adjustmentValueChanged(AdjustmentEvent e){c=new Color(r1.getValue(),g1.getValue(),b1.getValue());tt.setBackground(c);}public void itemStateChanged(ItemEvent e){if(e.getItemSelectable() instanceof JCheckBox){JCheckBox temp=(JCheckBox)e.getItemSelectable();fill=temp.isSelected();}else{JRadioButton temp=(JRadioButton)e.getItemSelectable();if(temp==op1)f=1;else if(temp==op2)f=2;else if(temp==op3)f=3;else if(temp==op4)f=4;}}public static void main(String args[]) {JFrame.setDefaultLookAndFeelDecorated(true);Font font = new Font("JFrame", Font.PLAIN, 14);Enumeration keys = UIManager.getLookAndFeelDefaults().keys(); while (keys.hasMoreElements()) {Object key = keys.nextElement();if (UIManager.get(key) instanceof Font)UIManager.put(key, font);}DrawFigure mainFrame = new DrawFigure(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -