package draw; import javax.swing.*; import java.awt.*; public class drawtower extends JFrame { DrawAction gol=new DrawAction(); public void initUI() { this.setTitle("金字塔"); this.setDefaultCloseOperation(3); this.setSize(800, 800); JButton anniu1 = new JButton("画金字塔"); this.add(anniu1); JButton anniu2=new JButton("画方形"); this.add(anniu2); JButton anniu3=new JButton("画笔"); this.add(anniu3); this.setVisible(true); FlowLayout fl = new FlowLayout(); Graphics g=this.getGraphics(); this.setLayout(fl); DrawAction UI = new DrawAction(g); anniu1.addActionListener(UI); anniu2.addActionListener(UI); anniu3.addActionListener(UI); this.addMouseListener(gol); penDraw pan=new penDraw(g); this.addMouseMotionListener(pan); } public static void main(String[] args) { drawtower hua = new drawtower(); hua.initUI(); } }
package draw; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class DrawAction implements MouseListener, ActionListener { private Graphics g = null; public DrawAction(Graphics g) { this.g = g; } public DrawAction() { } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); System.out.println(x); System.out.println(y); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); g.setColor(new Color(255, 255, 255, 255)); g.fillRect(0, 100, 2000, 1000); if (cmd.equals("画金字塔")) { System.out.println("1"); int a = 200, A = 300; int p1x = 400, p1y = 200; int p2x = p1x - 200, p2y = p1y + 300; int p3x = p1x - 50, p3y = p1y + 450; int p4x = p1x + 200, p4y = p1y + 300; int a1x = p1x - 70, a1y = p1y + 105; int a2x = p1x - 150, a2y = p1y + 225; int b1x = a1x + 53, b1y = a1y + 53; int b2x = a2x + 113, b2y = a2y + 113; int c1x = b1x + 87, c1y = b1y - 53; int c2x = b2x + 186, c2y = b2y - 112; java.awt.Polygon po1 = new Polygon(); po1.addPoint(p1x, p1y); po1.addPoint(p2x, p2y); po1.addPoint(p3x, p3y); g.setColor(new Color(148, 101, 0)); g.fillPolygon(po1); java.awt.Polygon po2 = new Polygon(); po2.addPoint(p1x, p1y); po2.addPoint(p4x, p4y); po2.addPoint(p3x, p3y); g.setColor(new Color(250, 192, 4)); g.fillPolygon(po2); g.setColor(Color.black); g.drawLine(p1x, p1y, p2x, p2y); g.drawLine(p2x, p2y, p3x, p3y); g.drawLine(p4x, p4y, p3x, p3y); g.drawLine(p1x, p1y, p3x, p3y); g.drawLine(p1x, p1y, p4x, p4y); g.drawLine(p1x + 1, p1y + 1, p2x + 1, p2y + 1); g.drawLine(p2x + 1, p2y + 1, p3x + 1, p3y + 1); g.drawLine(p4x + 1, p4y + 1, p3x + 1, p3y + 1); g.drawLine(p1x + 1, p1y + 1, p3x + 1, p3y + 1); g.drawLine(p1x + 1, p1y + 1, p4x + 1, p4y + 1); g.drawLine(a1x, a1y, b1x, b1y); g.drawLine(a2x, a2y, b2x, b2y); g.drawLine(b1x, b1y, c1x, c1y); g.drawLine(b2x, b2y, c2x, c2y); g.drawLine(a1x + 1, a1y + 1, b1x + 1, b1y + 1); g.drawLine(a2x + 1, a2y + 1, b2x + 1, b2y + 1); g.drawLine(b1x + 1, b1y + 1, c1x + 1, c1y + 1); g.drawLine(b2x + 1, b2y + 1, c2x + 1, c2y + 1); g.drawLine(p1x, p1y, 353, 328); g.drawLine(341, 315, 270, 444); g.drawLine(370, 344, 330, 505); g.drawLine(300, 475, 268, 568); g.drawLine(p1x, p1y, 433, 329); g.drawLine(410, 341, 422, 505); g.drawLine(454, 317, 515, 447); g.drawLine(470, 474, 496, 564); } else if (cmd.equals("画方形")) { int w = 200, h = 150; int wx = 80, wy = 100; int p1x = 200, p1y = 500; int p2x = p1x + w, p2y = p1y; int p3x = p2x, p3y = p2y - h; int p4x = p1x, p4y = p1y - h; int p5x = p4x + wx, p5y = p4y - wy; int p6x = p3x + wx, p6y = p3y - wy; int p7x = p2x + wx, p7y = p2y - wy; g.setColor(Color.black); g.drawLine(p1x, p1y, p2x, p2y); g.drawLine(p1x, p1y, p4x, p4y); g.drawLine(p2x, p2y, p3x, p3y); g.drawLine(p3x, p3y, p4x, p4y); g.drawLine(p3x, p3y, p6x, p6y); g.drawLine(p4x, p4y, p5x, p5y); g.drawLine(p5x, p5y, p6x, p6y); g.drawLine(p6x, p6y, p7x, p7y); g.drawLine(p2x, p2y, p7x, p7y); Polygon p1 = new Polygon(); p1.addPoint(p1x, p1y); p1.addPoint(p2x, p2y); p1.addPoint(p3x, p3y); p1.addPoint(p4x, p4y); g.setColor(new Color(246, 81, 10)); g.fillPolygon(p1); Polygon p2 = new Polygon(); p2.addPoint(p3x, p3y); p2.addPoint(p4x, p4y); p2.addPoint(p5x, p5y); p2.addPoint(p6x, p6y); g.setColor(new Color(252, 133, 68)); g.fillPolygon(p2); Polygon p3 = new Polygon(); p3.addPoint(p2x, p2y); p3.addPoint(p3x, p3y); p3.addPoint(p6x, p6y); p3.addPoint(p7x, p7y); g.setColor(new Color(255, 157, 99)); g.fillPolygon(p3); } else if (cmd.equals("画笔")) { g.fillRect(0, 100, 1000, 1000); } } public void mouseClicked(MouseEvent e) { } ; public void mouseReleased(MouseEvent e) { } ; public void mouseEntered(MouseEvent e) { } ; public void mouseExited(MouseEvent e) { } ; }
package draw; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class penDraw implements java.awt.event.MouseMotionListener{ private Graphics g; public penDraw(Graphics g) { this.g = g; } public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); System.out.println("横坐标;"+x+"纵坐标;"+y); g.setColor(Color.black); g.fillOval(x-25,y-25,50,50); } ; public void mouseMoved(MouseEvent e) { } ; }