java畫圖板 (部分功能未實現)
阿新 • • 發佈:2018-11-11
主類
package draw; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Draw extends JFrame{ /** * */ private static final long serialVersionUID = 1L; private JPanel bJpanel,cJpanel,jpanel; private Drawlistener Listener; private Graphics g; Draw(){ super("DRAW"); init(); } public void init() { String name[]= { "畫直線", "畫橢圓", "畫曲線", "多邊形", "橡皮擦", "三角形", "畫球形", "筆刷", "噴槍", "立體矩形", "立體圓", "立體三角", "楓葉", "畫樹","畫字元", "清空", "矩形","五角星","多線","字元" }; Color[] colorArray = { Color.BLACK,Color.DARK_GRAY,Color.GRAY, Color.BLUE, new Color(138,43,226),new Color(148,0,211), new Color(139,0,139),new Color(75,0,130),new Color(106,90,205), new Color(25,25,112),new Color(30,144,255),new Color(100,149,237), new Color(135,206,235),new Color(175,238,238),new Color(0,128,128), new Color(32,178,170),Color.CYAN,Color.GREEN, Color.MAGENTA,new Color(220,20,60), Color.RED, new Color(210,105,30),Color.ORANGE,Color.YELLOW, new Color(240,128,128),Color.PINK,new Color(255,192,203), new Color(255,240,245), new Color(240,255,240),Color.WHITE, }; Listener = new Drawlistener(); bJpanel = new JPanel(); bJpanel.setBackground(Color.WHITE); bJpanel.setLayout(new GridLayout(10,2,6,10)); bJpanel.setPreferredSize(new Dimension(200,500)); bJpanel.setBorder(BorderFactory.createLineBorder(Color.white,3)); for(int i=0;i<name.length;i++) { JButton but = new JButton (name[i]); but.addActionListener(Listener); but.setBackground(Color.GRAY); but.setBorderPainted(false); but.setFont(new Font("黑體",Font.PLAIN,14)); but.setForeground(Color.white); bJpanel.add(but); } cJpanel = new JPanel(); cJpanel.setLayout(new GridLayout(1,colorArray.length,0,0)); for(int i=0;i<colorArray.length;i++) { JButton but = new JButton (); but.addActionListener(Listener); but.setBackground(colorArray[i]); but.setPreferredSize(new Dimension(30,200)); cJpanel.add(but); } jpanel = new JPanel(); jpanel.setBackground(Color.white); jpanel.setBorder(BorderFactory.createLineBorder(Color.white,3)); jpanel.setPreferredSize(new Dimension(950,500)); JButton butt = new JButton (); butt.setBackground(Color.white); butt.setPreferredSize(new Dimension(250,200)); butt.setBorder(BorderFactory.createLineBorder(Color.GRAY,1)); this.setVisible(true); this.setSize(1200,725); this.setLayout(new FlowLayout()); setDefaultCloseOperation(3); this.add(bJpanel); this.add(jpanel); this.add(cJpanel); this.add(butt); g=jpanel.getGraphics(); Listener.setg(g); Listener.setNowcolor(butt); Listener.setjp(jpanel,Color.WHITE); jpanel.addMouseListener(Listener); jpanel.addMouseMotionListener(Listener); this.setBackground(Color.BLACK); setLocationRelativeTo(null); this.setResizable(false); } public static void main(String[] args) { // TODO Auto-generated method stub new Draw(); } }
Drawlistener 類
package draw; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JPanel; public class Drawlistener implements MouseListener, ActionListener, MouseMotionListener{ private Color color; private JPanel jpanel; private Graphics2D g; private JButton but; private String str; private int x1,y1,x2,y2; public void setjp(JPanel jpanel,Color c) { this.jpanel=jpanel; } public void setg(Graphics g) { this.g=(Graphics2D)g; this.g.setStroke(new BasicStroke(3.0f)); } public void setNowcolor(JButton butt) { this.but = butt; } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub if ("畫曲線".equals(str)) { int x, y; x = e.getX(); y = e.getY(); g.drawLine(x, y, x1, y1); x1 = x; y1 = y; } if ("橡皮擦".equals(str)) { int x, y; x = e.getX(); y = e.getY(); g.setColor(Color.WHITE); g.fillRect(x, y, 10, 10); x1 = x; y1 = y; } } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if ("".equals(e.getActionCommand())) { JButton jb = (JButton) e.getSource(); color = jb.getBackground(); but.setBackground(color);//處理當前顏色 } else { if(e.getActionCommand().equals("清空")){ jpanel.repaint(); } else str = e.getActionCommand(); } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub g.setColor(color); x1=e.getX(); y1=e.getY(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub x2=e.getX(); y2=e.getY(); if ("畫直線".equals(str)) { g.drawLine(x1, y1, x2, y2); } if ("立體矩形".equals(str)) { g.draw3DRect(x1,y1,(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),true); } if ("畫橢圓".equals(str)) { g.drawOval(x1,y1,(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))/2); } if ("立體圓".equals(str)) { g.drawOval(x1,y1,(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); } if ("矩形".equals(str)) { g.drawRect(x1,y1,(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); } if ("三角形".equals(str)||"立體三角".equals(str)) { int mx=(int)((x1+x2)/2); int x[]= {mx,Math.max(x1,x2),Math.min(x1,x2)}; int y[]= {Math.min(y1,y2),Math.max(y1,y2),Math.max(y1,y2)}; g.drawPolygon(x,y,3); } if ("五角星".equals(str)) { int x[],y[],x_[],y_[]; x=new int[10];y=new int[10];x_=new int[10];y_=new int[10]; int c = 360 / 5; // 角度 int r=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))/2; for (int i = 0; i < 5; i++) { x[i] = (int) (Math.cos(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r); y[i] = (int) (Math.sin(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r); } int r_ = (int) (r * Math.sin(18 * Math.PI / 180) / Math .sin(126 * Math.PI / 180)); // 內頂點外接圓半徑 for (int i = 0; i < 5; i++) { x_[i] = (int) (Math.cos((i * c + 18) * Math.PI / 30 - Math.PI / 2) * (r_) + r); y_[i] = (int) (Math.sin((i * c + 18) * Math.PI / 30 - Math.PI / 2) * (r_) + r); } int[] x1 = { x[0], x[2], x_[2] }; int[] y1 = { y[0], y[2], y_[2] }; int[] x2 = { x[1], x[3], x_[3] }; int[] y2 = { y[1], y[3], y_[3] }; int[] x3 = { x[2], x[4], x_[4] }; int[] y3 = { y[2], y[4], y_[4] }; g.drawPolygon(x1, y1, 3); g.drawPolygon(x2, y2, 3); g.drawPolygon(x3, y3, 3); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }