Advertisement
Guest User

javaCodeshape

a guest
Nov 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Cursor;
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.Graphics;
  6. import java.awt.Rectangle;
  7.  
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import javax.swing.JPanel;
  11. import javax.swing.border.EmptyBorder;
  12. import java.awt.SystemColor;
  13. import javax.swing.JButton;
  14. import javax.swing.JComponent;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JRadioButton;
  17. import javax.swing.ImageIcon;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ComponentEvent;
  21. import java.awt.event.ComponentListener;
  22. import java.awt.event.ItemEvent;
  23. import java.awt.event.ItemListener;
  24. import java.awt.event.MouseAdapter;
  25. import java.awt.event.MouseEvent;
  26. import java.awt.event.MouseMotionAdapter;
  27. import java.awt.geom.AffineTransform;
  28. import java.awt.geom.PathIterator;
  29. import java.awt.geom.Point2D;
  30. import java.awt.geom.Rectangle2D;
  31.  
  32. import javax.swing.JLabel;
  33. import java.awt.Color;
  34. import java.util.Random;
  35.  
  36.  
  37.  
  38.  
  39.  
  40. class MyComponent extends JComponent {
  41. public void paint(Graphics g) {
  42.  
  43. g.fillRect(30, 30, 100, 100);
  44.  
  45. }
  46.  
  47. }
  48.  
  49. public class testA extends JFrame {
  50.  
  51. private JPanel contentPane;
  52. boolean check=false;
  53. boolean choice=false;
  54. boolean choice1=false;
  55.  
  56. private Dimension area;
  57. private static final int NONE = -1;
  58. private static final int BORDER = 3;
  59. private static final ComponentListener ComponentEvent = null;
  60. private int startX = NONE;
  61. private int startY = NONE;
  62. private int prevX = NONE;
  63. private int prevY = NONE;
  64. private boolean resize = false;
  65. int shape;
  66. Random rd = new Random();
  67. int size,size1;
  68.  
  69. JButton btnNewButton = new JButton("");
  70. JButton btnNewButton_1 = new JButton("");
  71. JButton btnNewButton_2 = new JButton("");
  72. JButton btnNewButton_3 = new JButton("");
  73. JButton btnNewButton_4 = new JButton("");
  74. JButton btnNewButton_5 = new JButton("");
  75.  
  76. JButton button = new JButton("");
  77. JButton button_1 = new JButton("");
  78. JButton button_2 = new JButton("");
  79. JButton button_3 = new JButton("");
  80. JButton button_4 = new JButton("");
  81. JButton button_5 = new JButton("");
  82.  
  83. JPanel panel = new JPanel();
  84. JPanel panel_1 = new JPanel();
  85. JPanel panel_2 = new JPanel();
  86. JPanel panel_3 = new JPanel();
  87.  
  88. JRadioButton rdbtnFill = new JRadioButton("Fill");
  89. JRadioButton rdbtnNewRadioButton = new JRadioButton("Unfill");
  90.  
  91. /**
  92. * Launch the application.
  93. */
  94. public static void main(String[] args) {
  95. EventQueue.invokeLater(new Runnable() {
  96. public void run() {
  97. try {
  98. testA frame = new testA();
  99. frame.setVisible(true);
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. });
  105. }
  106.  
  107.  
  108.  
  109. /**
  110. * Create the frame.
  111. */
  112. public testA() {
  113. setResizable(false);
  114. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  115. setBounds(100, 100, 567, 490);
  116. contentPane = new JPanel();
  117. contentPane.setBackground(SystemColor.textHighlight);
  118. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  119. setContentPane(contentPane);
  120. contentPane.setLayout(null);
  121.  
  122. JPanel panel = new JPanel();
  123. panel.setBackground(Color.WHITE);
  124. panel.setBounds(443, 0, 115, 252);
  125. contentPane.add(panel);
  126. panel.setLayout(null);
  127.  
  128.  
  129. btnNewButton.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\rect.GIF"));
  130. btnNewButton.setBounds(29, 24, 55, 49);
  131. panel.add(btnNewButton);
  132. btnNewButton.addActionListener(new ButtonListener());
  133.  
  134.  
  135.  
  136.  
  137. btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\round.GIF"));
  138. btnNewButton_1.setBounds(29, 76, 55, 49);
  139. panel.add(btnNewButton_1);
  140. btnNewButton_1.addActionListener(new ButtonListener());
  141.  
  142.  
  143. btnNewButton_2.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\triangle.GIF"));
  144. btnNewButton_2.setBounds(28, 136, 56, 53);
  145. panel.add(btnNewButton_2);
  146. btnNewButton_2.addActionListener(new ButtonListener());
  147.  
  148. btnNewButton_3.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\oval.GIF"));
  149. btnNewButton_3.setBounds(29, 200, 55, 41);
  150. panel.add(btnNewButton_3);
  151. btnNewButton_3.addActionListener(new ButtonListener());
  152.  
  153. JLabel lblShape = new JLabel("Shape");
  154. lblShape.setForeground(SystemColor.textHighlight);
  155. lblShape.setBackground(SystemColor.textHighlight);
  156. lblShape.setBounds(38, -1, 46, 14);
  157. panel.add(lblShape);
  158.  
  159.  
  160. panel_1.setBackground(Color.WHITE);
  161. panel_1.setBounds(443, 253, 115, 80);
  162. contentPane.add(panel_1);
  163. panel_1.setLayout(null);
  164.  
  165.  
  166. //rdbtnFill.setSelected(true);
  167. rdbtnFill.setBounds(12, 18, 84, 23);
  168. panel_1.add(rdbtnFill);
  169.  
  170.  
  171.  
  172.  
  173. rdbtnNewRadioButton.setBounds(12, 44, 84, 23);
  174. panel_1.add(rdbtnNewRadioButton);
  175.  
  176.  
  177. panel_2.setBackground(Color.WHITE);
  178. panel_2.setBounds(0, 334, 558, 128);
  179. contentPane.add(panel_2);
  180. panel_2.setLayout(null);
  181.  
  182.  
  183.  
  184. btnNewButton_4.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\.5x.GIF"));
  185. btnNewButton_4.setBounds(23, 32, 83, 85);
  186. panel_2.add(btnNewButton_4);
  187. btnNewButton_4.addActionListener(new ButtonListener());
  188.  
  189.  
  190. button.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\2x.GIF"));
  191. button.setBounds(109, 32, 77, 82);
  192. panel_2.add(button);
  193. button.addActionListener(new ButtonListener());
  194.  
  195.  
  196. button_1.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\clear.GIF"));
  197. button_1.setBounds(270, 51, 90, 49);
  198. panel_2.add(button_1);
  199. button_1.addActionListener(new ActionListener() {
  200. public void actionPerformed(ActionEvent e) {
  201. panel_3.removeAll();
  202. panel_3.repaint();
  203. }
  204. });
  205.  
  206.  
  207.  
  208.  
  209. button_2.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\up.GIF"));
  210. button_2.setBounds(455, 21, 38, 27);
  211. panel_2.add(button_2);
  212. //button_2.addActionListener(new ButtonListener1());
  213.  
  214.  
  215.  
  216.  
  217. button_3.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\down.GIF"));
  218. button_3.setBounds(455, 84, 38, 33);
  219. panel_2.add(button_3);
  220. //button_3.addActionListener(new ButtonListener1());
  221.  
  222. button_4.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\Left.GIF"));
  223. button_4.setBounds(423, 50, 32, 33);
  224. panel_2.add(button_4);
  225. //button_4.addActionListener(new ButtonListener1());
  226.  
  227. button_5.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\right.GIF"));
  228. button_5.setBounds(493, 50, 32, 33);
  229. panel_2.add(button_5);
  230. //button_5.addActionListener(new ButtonListener1());
  231.  
  232. btnNewButton_5.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\A.GIF"));
  233. btnNewButton_5.setBounds(455, 50, 38, 33);
  234. panel_2.add(btnNewButton_5);
  235. //btnNewButton_5.addActionListener( this);
  236.  
  237. JLabel lblResize = new JLabel("Resize");
  238. lblResize.setForeground(SystemColor.textHighlight);
  239. lblResize.setBounds(82, 0, 46, 14);
  240. panel_2.add(lblResize);
  241.  
  242. JLabel lblClear = new JLabel("Clear");
  243. lblClear.setForeground(SystemColor.textHighlight);
  244. lblClear.setBounds(289, 0, 46, 14);
  245. panel_2.add(lblClear);
  246.  
  247. JLabel lblMove = new JLabel("Move");
  248. lblMove.setForeground(SystemColor.textHighlight);
  249. lblMove.setBounds(458, 0, 46, 14);
  250. panel_2.add(lblMove);
  251.  
  252.  
  253. panel_3.setBackground(Color.WHITE);
  254. panel_3.setBounds(0, 0, 441, 333);
  255. contentPane.add(panel_3);
  256.  
  257. rdbtnFill.addItemListener(new JButtonListener()) ;
  258. rdbtnNewRadioButton.addItemListener(new JButtonListener()) ;
  259. }
  260.  
  261. public class JButtonListener implements ItemListener
  262. {
  263.  
  264. public void itemStateChanged(ItemEvent e) {
  265. if(e.getSource()==rdbtnFill)
  266. {
  267. check=true;
  268. }
  269. else if(e.getSource()==rdbtnNewRadioButton)
  270. {
  271. check=false;
  272. }
  273. }
  274.  
  275. }
  276. class MyCanvas extends JComponent {
  277.  
  278.  
  279. public void paint(Graphics g) {
  280. super.paintComponent(g);
  281. g.setColor(Color.RED);
  282. g.fill3DRect(10, 10, 200, 200,true);
  283.  
  284. }
  285.  
  286. }
  287. class MyCanvas1 extends JComponent {
  288.  
  289.  
  290. public void paint(Graphics g) {
  291. super.paintComponent(g);
  292. g.setColor(Color.RED);
  293. g.drawRect(20, 20, 200, 200);
  294. }
  295. }
  296. class MyCanvas2 extends JComponent {
  297.  
  298.  
  299. public void paint(Graphics g) {
  300. super.paintComponent(g);
  301. g.setColor(Color.RED);
  302. g.fillOval(30, 30, 150, 150);
  303. }
  304. }
  305. class MyCanvas3 extends JComponent {
  306.  
  307.  
  308. public void paint(Graphics g) {
  309. super.paintComponent(g);
  310. g.setColor(Color.RED);
  311. g.drawOval(30, 30, 150, 150);
  312. }
  313. }
  314. class MyCanvas4 extends JComponent {
  315.  
  316.  
  317. public void paint(Graphics g) {
  318. super.paintComponent(g);
  319. int xPos=150;
  320. int yPos=50;
  321. int size=100;
  322. g.setColor(Color.RED);
  323. g.fillPolygon(
  324. new int[]{xPos, xPos + size, xPos - size},
  325. new int[]{yPos, yPos + size, yPos + size}, 3);
  326. }
  327. }
  328. class MyCanvas5 extends JComponent {
  329.  
  330.  
  331. public void paint(Graphics g) {
  332. super.paintComponent(g);
  333. int xPos=150;
  334. int yPos=50;
  335. int size=100;
  336. g.setColor(Color.RED);
  337. g.drawPolygon(
  338. new int[]{xPos, xPos + size, xPos - size},
  339. new int[]{yPos, yPos + size, yPos + size}, 3);
  340. }
  341. }
  342. class MyCanvas6 extends JComponent {
  343.  
  344.  
  345. public void paint(Graphics g) {
  346. super.paintComponent(g);
  347. g.setColor(Color.RED);
  348. g.drawOval(30, 30, 150, 100);
  349. }
  350. }
  351.  
  352. class MyCanvas7 extends JComponent {
  353.  
  354.  
  355. public void paint(Graphics g) {
  356. super.paintComponent(g);
  357. g.setColor(Color.RED);
  358. g.fillOval(30, 30, 150, 100);
  359. }
  360. }
  361.  
  362.  
  363. public class ButtonListener implements ActionListener
  364. {
  365. public void actionPerformed(ActionEvent e) {
  366. if(check)
  367. {
  368.  
  369. if(e.getSource()==btnNewButton)
  370. {
  371. MyCanvas can=new MyCanvas();
  372. can.setSize(200, 100);
  373. panel_3.add(can);
  374. panel_3.repaint();
  375. addComponent(can);
  376. //panel_3.addComponentListener(this);
  377.  
  378. }
  379. else if(e.getSource()==btnNewButton_1)
  380. {
  381. panel_3.removeAll();
  382. MyCanvas2 can=new MyCanvas2();
  383. can.setSize(300, 300);
  384. panel_3.add(can);
  385. panel_3.repaint();
  386. addComponent(can);
  387. can.addComponentListener((ComponentListener) this);
  388. }
  389. else if(e.getSource()==btnNewButton_2)
  390. {
  391. panel_3.removeAll();
  392. MyCanvas4 can=new MyCanvas4();
  393. can.setSize(300, 300);
  394. panel_3.add(can);
  395. panel_3.repaint();
  396. addComponent(can);
  397. }
  398. else if(e.getSource()==btnNewButton_3)
  399. {
  400. panel_3.removeAll();
  401. MyCanvas7 can=new MyCanvas7();
  402. can.setSize(500, 500);
  403. panel_3.add(can);
  404. panel_3.repaint();
  405. addComponent(can);
  406.  
  407. }
  408.  
  409.  
  410. }
  411. else
  412. {
  413. if(e.getSource()==btnNewButton)
  414. {
  415. panel_3.removeAll();
  416. MyCanvas1 can=new MyCanvas1();
  417. can.setSize(500, 500);
  418. panel_3.add(can);
  419. panel_3.repaint();
  420. addComponent(can);
  421. }
  422. else if(e.getSource()==btnNewButton_1)
  423. {
  424. panel_3.removeAll();
  425. MyCanvas3 can=new MyCanvas3();
  426. can.setSize(500, 500);
  427. panel_3.add(can);
  428. panel_3.repaint();
  429. addComponent(can);
  430. }
  431. else if(e.getSource()==btnNewButton_2)
  432. {
  433. panel_3.removeAll();
  434. MyCanvas5 can=new MyCanvas5();
  435. can.setSize(500, 500);
  436. panel_3.add(can);
  437. panel_3.repaint();
  438. addComponent(can);
  439. }
  440. else if(e.getSource()==btnNewButton_3)
  441. {
  442. panel_3.removeAll();
  443. MyCanvas6 can=new MyCanvas6();
  444. can.setSize(500, 500);
  445. panel_3.add(can);
  446. panel_3.repaint();
  447. addComponent(can);
  448. }
  449.  
  450. }
  451.  
  452.  
  453. }
  454.  
  455.  
  456. }
  457.  
  458. //This is mouse event Which will move shape with mouse
  459. public void addComponent( JComponent comp) {
  460. comp.setBounds(10, 10, 1000, 1000);
  461.  
  462. comp.addMouseListener(new MouseAdapter() {
  463. public void mouseReleased(MouseEvent e) {
  464. startX = NONE;
  465. startY = NONE;
  466. ((JComponent) e.getSource()).setCursor(Cursor.getDefaultCursor());
  467. }
  468.  
  469. public void mousePressed(MouseEvent e) {
  470. startX = e.getX();
  471. startY = e.getY();
  472. }
  473. });
  474.  
  475.  
  476.  
  477.  
  478.  
  479. comp.addMouseMotionListener(new MouseMotionAdapter() {
  480. public void mouseMoved(MouseEvent e) {
  481. JComponent source = (JComponent) e.getSource();
  482. int x = e.getX();
  483. int y = e.getY();
  484. Rectangle bounds = source.getBounds();
  485. resize = x < BORDER || y < BORDER || Math.abs(bounds.width - x) < BORDER || Math.abs(bounds.height - y) < BORDER;
  486. if (resize) {
  487. // TODO: there are a lot of resize cursors here, this is just of proof of concept
  488. source.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
  489. } else {
  490. source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
  491. }
  492. }
  493.  
  494. public void mouseDragged(MouseEvent e) {
  495. int x = e.getX();
  496. int y = e.getY();
  497. if (startX != NONE && startY != NONE) {
  498. JComponent source = (JComponent) e.getSource();
  499. Rectangle bounds = source.getBounds();
  500. int deltaX = x - startX;
  501. int deltaY = y - startY;
  502. if (resize) {
  503. // TODO: handle all resize cases, left, right,...
  504. source.setSize(Math.max(10, bounds.width + x - prevX), Math.max(10, bounds.height + y - prevY));
  505. } else {
  506. source.setLocation(bounds.x + deltaX, bounds.y + deltaY);
  507. }
  508. // TODO:
  509. // TODO:
  510. } else {
  511. startX = x;
  512. startY = y;
  513. }
  514. prevX = x;
  515. prevY = y;
  516. }
  517. });
  518.  
  519.  
  520. panel_3.add(comp);
  521.  
  522.  
  523. }
  524.  
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement