Guest User

Untitled

a guest
Feb 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8.  
  9. @SuppressWarnings("serial")
  10. public class hillstromnlab6 extends JFrame{
  11.  
  12. boolean Ffill = false;
  13. boolean Cred = false;
  14. boolean Cyellow = false;
  15. boolean Cblue = false;
  16. boolean Ssquare = false;
  17. boolean SRsquare = false;
  18. boolean Soval = false;
  19.  
  20.  
  21. class ControlPanel extends JPanel{
  22. JLabel label = new JLabel("Open");
  23. JToolBar toolbar = new JToolBar("Open/Fill");
  24. JRadioButton square = new JRadioButton("Square", true);
  25. JRadioButton oval = new JRadioButton("Oval", false);
  26. JRadioButton circle = new JRadioButton("RSqr", false);
  27. ButtonGroup Rgroup = new ButtonGroup();
  28. JCheckBox red = new JCheckBox("red", true);
  29. JCheckBox yellow= new JCheckBox("yellow", false);
  30. JCheckBox blue= new JCheckBox("blue", false);
  31. ButtonGroup Cgroup = new ButtonGroup();
  32. JToggleButton fill = new JToggleButton(new ImageIcon("open_shape.gif"));
  33.  
  34. public ControlPanel()
  35. {
  36. Rgroup.add(circle);
  37. Rgroup.add(square);
  38. Rgroup.add(oval);
  39.  
  40. red.setBackground(Color.red);
  41. yellow.setBackground(Color.yellow);
  42. blue.setBackground(Color.blue);
  43. Cgroup.add(blue);
  44. Cgroup.add(red);
  45. Cgroup.add(yellow);
  46.  
  47. toolbar.add(fill);
  48. toolbar.add(label);
  49. this.add(square);
  50. this.add(circle);
  51. this.add(oval);
  52. this.add(red);
  53. this.add(yellow);
  54. this.add(blue);
  55. this.add(toolbar);
  56. Ssquare = true;
  57. red.addActionListener(new ActionListener() {
  58. public void actionPerformed(ActionEvent evt){
  59. if(red.isSelected())
  60. Cred = true;Cblue = false;Cyellow = false;
  61. }
  62. });
  63. yellow.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent evt){
  65. if(yellow.isSelected())
  66. Cyellow = true;Cred = false;Cblue = false;
  67. }
  68. });
  69. blue.addActionListener(new ActionListener() {
  70. public void actionPerformed(ActionEvent evt){
  71. if(blue.isSelected())
  72. Cblue = true;Cred =false;Cyellow = false;
  73. }
  74. });
  75. square.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent evt){
  77. if(square.isSelected())
  78. Ssquare = true;SRsquare = false;Soval = false;
  79. }
  80. });
  81. circle.addActionListener(new ActionListener() {
  82. public void actionPerformed(ActionEvent evt){
  83. if(circle.isSelected())
  84. SRsquare = true;Ssquare = false;Soval = false;
  85. }
  86. });
  87. oval.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent evt){
  89. if(oval.isSelected())
  90. Soval = true;Ssquare = false;SRsquare = false;
  91. }
  92. });
  93. fill.addActionListener(new ActionListener() {
  94. public void actionPerformed(ActionEvent evt){
  95. if(fill.isSelected()){
  96. Ffill = true;
  97. fill.setSelectedIcon(new ImageIcon("fill_shape.gif"));
  98. label.setText("FILL");
  99. }
  100. else{
  101. Ffill = false;
  102. label.setText("OPEN");
  103. }
  104. }
  105. });
  106.  
  107. }
  108.  
  109. }//end controlpanel
  110.  
  111. class MyCanvas extends JPanel{
  112.  
  113. Listener myListener;
  114.  
  115. //Constructor
  116. public MyCanvas(){
  117. myListener = new Listener(this);
  118. addMouseListener(myListener);
  119. addMouseMotionListener(myListener);
  120. }
  121.  
  122. public void paintComponent(Graphics g){
  123. if(myListener.drag)
  124. {
  125. g.setColor(Color.green);
  126. g.setXORMode(Color.CYAN);
  127. rubberBand(g, myListener.startpt.x, myListener.startpt.y, myListener.temppt.x, myListener.temppt.x);
  128. rubberBand(g, myListener.startpt.x, myListener.startpt.y, myListener.endpt.x, myListener.endpt.y);
  129. //System.out.println("Here2");
  130.  
  131. }
  132.  
  133. else if(myListener.released)
  134. {
  135. Color color = getAColor();
  136. g.setColor(color);
  137. rubberBand(g, myListener.startpt.x,myListener.startpt.y, myListener.endpt.x,myListener.endpt.x);
  138.  
  139. }
  140.  
  141. }
  142.  
  143.  
  144. public void rubberBand(Graphics g, int startX, int startY, int endX, int endY)
  145. {
  146. int x, y, w, h;
  147.  
  148. //g.setColor(Color.green);
  149.  
  150. x = Math.min(myListener.startpt.x, myListener.endpt.x);
  151. y = Math.min(myListener.startpt.y, myListener.endpt.y);
  152. w = Math.abs(myListener.startpt.x - myListener.endpt.x);
  153. h = Math.abs(myListener.startpt.y - myListener.endpt.y);
  154. //System.out.println(myListener.startpt.y + " "+ myListener.endpt.y );
  155. //System.out.println(x + " " + y + " " + w + " " + h);
  156. //g.setXORMode(Color.BLACK);
  157. //g.setPaintMode();
  158. g.drawRect(x,y, w, h);
  159.  
  160. }
  161.  
  162. public Color getAColor()
  163. {
  164. if(Cblue)
  165. return Color.blue;
  166. else if(Cyellow)
  167. return Color.yellow;
  168. else
  169. return Color.red;
  170. }
  171. }
  172.  
  173.  
  174. public static void main(String[] args){
  175. new hillstromnlab6();
  176.  
  177. }
  178. public hillstromnlab6(){
  179. super("Lab6");
  180. setBounds(100, 100, 600, 600);
  181. MyCanvas thecanvas = new MyCanvas();
  182.  
  183. Container cp = getContentPane();
  184. cp.setBackground(Color.LIGHT_GRAY);
  185. ControlPanel dock = new ControlPanel();
  186. cp.add(dock, BorderLayout.NORTH);
  187. cp.add(thecanvas);
  188. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  189. setVisible(true);
  190. }
  191. }
  192.  
  193.  
  194.  
  195. /*
  196. if(Ffill)//fill is enabled
  197. {
  198. if(Ssquare)
  199. g.fillRect(x, y, w, h);
  200. if(Soval)
  201. g.fillOval(x, y, w, h);
  202. if(SRsquare)
  203. g.fillRoundRect(x, y, w, h, 20, 20);
  204. }
  205. else
  206. {
  207. if(Ssquare)
  208. g.drawRect(x, y, w, h);
  209. if(Soval)
  210. g.drawOval(x, y, w, h);
  211. if(SRsquare)
  212. g.drawRoundRect(x, y, w, h, 20, 20);
  213. }
  214. */
Add Comment
Please, Sign In to add comment