Swing Gui

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 44
Reputation: jigvesh is an unknown quantity at this point 
Solved Threads: 0
jigvesh jigvesh is offline Offline
Light Poster

Swing Gui

 
0
  #1
Mar 11th, 2006
Well, I have done the following coding:-

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class line extends Component {
  5.  
  6. /**
  7. *
  8. */
  9. private static final long serialVersionUID = 1L;
  10. private int len;
  11. // private int nx,ny;
  12. private int relx,rely;
  13. boolean pressed = false;
  14. boolean endp = false;
  15. boolean resized=false;
  16. ActionListener actionListener = null;
  17.  
  18. public line() {
  19. this(5);
  20. }
  21.  
  22. public line(int len) {
  23. if (len < 0) {
  24. throw new IllegalArgumentException("Illegal len: " + len);
  25. }
  26. this.len = len;
  27. addMouseListener(new ClickAdapter());
  28. setSize(getPreferredSize());
  29. }
  30.  
  31. public void addActionListener(ActionListener l) {
  32. actionListener = AWTEventMulticaster.add(actionListener, l);
  33. }
  34.  
  35. public void removeActionListener(ActionListener l) {
  36. actionListener = AWTEventMulticaster.remove(actionListener, l);
  37. }
  38.  
  39. private final class ClickAdapter extends MouseAdapter {
  40.  
  41.  
  42. public void mousePressed(MouseEvent e) {
  43. // nx=e.getX();
  44. // ny=e.getY();
  45. //
  46. if (isEnabled()) {
  47. pressed = true;
  48.  
  49. repaint();
  50. }
  51. }
  52.  
  53. public void mouseReleased(MouseEvent e) {
  54.  
  55. if (isEnabled()) {
  56. pressed = false;
  57.  
  58. relx=e.getX();
  59. rely=e.getY();
  60.  
  61. resized=true;
  62.  
  63. repaint();
  64. if (actionListener != null) {
  65.  
  66.  
  67. actionListener.actionPerformed(
  68. new ActionEvent(line.this,
  69. ActionEvent.ACTION_PERFORMED, ""));
  70. }
  71. }
  72. }
  73. }
  74.  
  75. public Dimension getPreferredSize() {
  76. int side = len * 10;
  77. return new Dimension(side, side);
  78. }
  79.  
  80. public Dimension getMinimumSize() {
  81. return getPreferredSize();
  82. }
  83.  
  84. public Dimension getMaximumSize() {
  85. return getPreferredSize();
  86. }
  87.  
  88. public void paint(Graphics g) {
  89. super.paint(g);
  90. Graphics2D g2d = (Graphics2D)g;
  91.  
  92. if(resized)
  93. {
  94. g2d.clearRect(0,0,len*2,len*2);
  95. g2d.setStroke(new BasicStroke(2));
  96. g2d.setColor(Color.BLUE);
  97. g2d.drawRect(0,0,relx,rely);
  98.  
  99. System.out.println(relx + " " + rely);
  100.  
  101. }
  102. else{
  103. if (isEnabled()) {
  104. if (pressed) {
  105. g2d.setStroke(new BasicStroke (5));
  106. g2d.setColor(Color.white);
  107. g2d.drawRect(0, 0, len*2, len*2);
  108. g2d.setColor(Color.black);
  109. g2d.drawRect(0, 0, len*2, len*2);
  110. }
  111. else {
  112. g2d.setColor(Color.black);
  113. g2d.drawRect(0, 0, len*2, len*2);
  114. }
  115. }
  116. else {
  117. g2d.setColor(Color.gray);
  118. g2d.drawRect(0, 0, len*2, len*2);
  119. }
  120. }
  121. }
  122.  
  123. // g2d.setColor(Color.YELLOW);
  124. // g2d.drawLine(0,0,relx,rely);
  125.  
  126.  
  127. public boolean contains(int x, int y) {
  128. //distance = square root of (deltaX squared + deltaY squared)
  129. int deltax = x - len;
  130. int deltay = y - len;
  131. double distance = Math.sqrt(Math.pow(deltax, 2) + Math.pow(deltay, 2));
  132. return (distance <= len);
  133. }
  134.  
  135. public static void main(String[] args) {
  136. final line cb1, cb2, cb3, cb4, cb5;
  137. Frame f = new Frame();
  138. f.addWindowListener(
  139. new WindowAdapter() {
  140. public void windowClosing(WindowEvent e) {
  141. System.exit(0);
  142. }
  143. }
  144. );
  145. f.setLayout(new FlowLayout());
  146. f.add(cb1 = new line(20));
  147. f.add(cb2 = new line(20));
  148. f.add(cb3 = new line(20));
  149. f.add(cb4 = new line(20));
  150. f.add(cb5 = new line(20));
  151. ActionListener al = new ActionListener() {
  152. public void actionPerformed(ActionEvent e) {
  153. Object o = e.getSource();
  154. String msg;
  155. if (o.equals(cb1)) {
  156. msg = "One";
  157. }
  158. else if (o == cb2) {
  159. msg = "Two";
  160. }
  161. else if (o == cb3) {
  162. msg = "Three";
  163. }
  164. else if (o == cb4) {
  165. msg = "Four";
  166. }
  167. else if (o == cb5) {
  168. msg = "Five";
  169. }
  170. else {
  171. msg = "Huh?" + o;
  172. }
  173. System.out.println("Button: " + msg);
  174. }
  175. };
  176. cb1.addActionListener(al);
  177. cb2.addActionListener(al);
  178. cb3.addActionListener(al);
  179. cb4.addActionListener(al);
  180. cb5.addActionListener(al);
  181. cb5.setEnabled(false);
  182. f.pack();
  183. f.setVisible(true);
  184. // f.show();
  185. }
  186. }

What i want to do is that the newly drawn rectangles i.e. the blue colour ones.....they should also have the property of being redrawn....i.e.perhaps these new rectangles should be the objects of teh existing class...how do i go about it???
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Swing Gui

 
0
  #2
Mar 11th, 2006
If I understand correctly, all you need to do is call repaint();
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1987 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC