Advertisement
Guest User

GUI Class

a guest
Oct 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package paint;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.*;
  7. import java.awt.ActiveEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseMotionListener;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16.  
  17. public class GUI extends JFrame implements MouseListener , MouseMotionListener{
  18.  
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 1L;
  23. JButton ClearBtn , RedBtn ,BlackBtn , LineBtn;
  24. JFrame paint = new JFrame("Paint");
  25. JPanel controls = new JPanel();
  26. private int OldX , OldY , CurrentX , CurrentY ;
  27. boolean LineClicked = false ;
  28.  
  29. MouseEvent mouse;
  30. Sketch sketch = new Sketch();
  31. Container content = paint.getContentPane();
  32. ActionListener action= new ActionListener() {
  33.  
  34. @Override
  35. public void actionPerformed(ActionEvent e) {
  36. // TODO Auto-generated method stub
  37. System.out.println(e.getSource() == ClearBtn);
  38. System.out.println(e.getSource());
  39.  
  40. if (e.getSource()==RedBtn) {
  41. sketch.red();
  42. }
  43. else if (e.getSource() == ClearBtn) {
  44. System.out.println(e.getSource() == ClearBtn);
  45. if(e.getSource() == ClearBtn){
  46. sketch.clear();
  47. }
  48. }
  49. }
  50. };
  51.  
  52.  
  53. public static void main (String[] args) {
  54. // GUI here = new GUI ();
  55. // here.setVisible(true);
  56. new GUI().show();
  57.  
  58. }
  59. public void show () {
  60. paint.setLayout(new BorderLayout());
  61.  
  62. // sketch.setLayout(new BorderLayout());
  63.  
  64. content.add(sketch, BorderLayout.SOUTH);
  65. content.setLayout(new BorderLayout());
  66. content.add(sketch, BorderLayout.CENTER);
  67. // create control .. buttons
  68. JButton BlackBtn = new JButton();
  69.  
  70. BlackBtn.setBackground(Color.black);
  71.  
  72.  
  73. BlackBtn.addActionListener(action);
  74.  
  75. ClearBtn = new JButton();
  76. ClearBtn.setVisible(true);
  77. ClearBtn.setText("CLEAR");
  78. ClearBtn.addActionListener(action);
  79. ClearBtn.setBackground(Color.white);
  80. ClearBtn.setForeground(Color.black);
  81. getContentPane().add(ClearBtn);
  82.  
  83.  
  84. RedBtn = new JButton();
  85. RedBtn.setVisible(true);
  86. RedBtn.addActionListener(action);
  87. RedBtn.setBackground(Color.RED);
  88. RedBtn.setForeground(Color.black);
  89. getContentPane().add(RedBtn);
  90. RedBtn.setBackground(Color.red);
  91.  
  92. LineBtn = new JButton();
  93. LineBtn.setVisible(true);
  94. LineBtn.addActionListener(action);
  95. LineBtn.setText("line");
  96.  
  97. LineBtn.setForeground(Color.green);
  98. getContentPane().add(LineBtn);
  99. LineBtn.addMouseListener(this);
  100.  
  101. controls.add(RedBtn);
  102. controls.add(BlackBtn);
  103. controls.add(LineBtn);
  104. controls.add(ClearBtn);
  105.  
  106.  
  107. content.add(controls , BorderLayout.NORTH);
  108. paint.setSize(600, 600);
  109. paint.setVisible(true);
  110. }
  111. @Override
  112. public void mouseDragged(MouseEvent e) {
  113. // TODO Auto-generated method stub
  114.  
  115. }
  116. @Override
  117. public void mouseMoved(MouseEvent e) {
  118. // TODO Auto-generated method stub
  119.  
  120. }
  121. @Override
  122. public void mouseClicked(MouseEvent e) {
  123. // TODO Auto-generated method stub
  124. if (e.getSource() == LineBtn){
  125. LineClicked = true ;
  126. }
  127.  
  128.  
  129.  
  130. }
  131. @Override
  132. public void mouseEntered(MouseEvent e) {
  133. // TODO Auto-generated method stub
  134.  
  135. }
  136. @Override
  137. public void mouseExited(MouseEvent e) {
  138. // TODO Auto-generated method stub
  139.  
  140. }
  141. @Override
  142. public void mousePressed(MouseEvent e) {
  143. // TODO Auto-generated method stub
  144. if (LineClicked){
  145. OldX = e.getX();
  146. OldY = e.getY();
  147. }
  148.  
  149. }
  150. @Override
  151. public void mouseReleased(MouseEvent e) {
  152. // TODO Auto-generated method stub
  153. CurrentX=e.getX();
  154. CurrentY=e.getY();
  155.  
  156. if (sketch.graphic !=null){
  157. sketch.graphic.drawLine(OldX , OldY , CurrentX , CurrentY);
  158. repaint();
  159. OldX = CurrentX;
  160. OldY = CurrentY;
  161. }
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement