| | |
Swing Gui
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 44
Reputation:
Solved Threads: 0
Well, I have done the following coding:-
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???
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; public class line extends Component { /** * */ private static final long serialVersionUID = 1L; private int len; // private int nx,ny; private int relx,rely; boolean pressed = false; boolean endp = false; boolean resized=false; ActionListener actionListener = null; public line() { this(5); } public line(int len) { if (len < 0) { throw new IllegalArgumentException("Illegal len: " + len); } this.len = len; addMouseListener(new ClickAdapter()); setSize(getPreferredSize()); } public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener, l); } public void removeActionListener(ActionListener l) { actionListener = AWTEventMulticaster.remove(actionListener, l); } private final class ClickAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { // nx=e.getX(); // ny=e.getY(); // if (isEnabled()) { pressed = true; repaint(); } } public void mouseReleased(MouseEvent e) { if (isEnabled()) { pressed = false; relx=e.getX(); rely=e.getY(); resized=true; repaint(); if (actionListener != null) { actionListener.actionPerformed( new ActionEvent(line.this, ActionEvent.ACTION_PERFORMED, "")); } } } } public Dimension getPreferredSize() { int side = len * 10; return new Dimension(side, side); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMaximumSize() { return getPreferredSize(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; if(resized) { g2d.clearRect(0,0,len*2,len*2); g2d.setStroke(new BasicStroke(2)); g2d.setColor(Color.BLUE); g2d.drawRect(0,0,relx,rely); System.out.println(relx + " " + rely); } else{ if (isEnabled()) { if (pressed) { g2d.setStroke(new BasicStroke (5)); g2d.setColor(Color.white); g2d.drawRect(0, 0, len*2, len*2); g2d.setColor(Color.black); g2d.drawRect(0, 0, len*2, len*2); } else { g2d.setColor(Color.black); g2d.drawRect(0, 0, len*2, len*2); } } else { g2d.setColor(Color.gray); g2d.drawRect(0, 0, len*2, len*2); } } } // g2d.setColor(Color.YELLOW); // g2d.drawLine(0,0,relx,rely); public boolean contains(int x, int y) { //distance = square root of (deltaX squared + deltaY squared) int deltax = x - len; int deltay = y - len; double distance = Math.sqrt(Math.pow(deltax, 2) + Math.pow(deltay, 2)); return (distance <= len); } public static void main(String[] args) { final line cb1, cb2, cb3, cb4, cb5; Frame f = new Frame(); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); f.setLayout(new FlowLayout()); f.add(cb1 = new line(20)); f.add(cb2 = new line(20)); f.add(cb3 = new line(20)); f.add(cb4 = new line(20)); f.add(cb5 = new line(20)); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { Object o = e.getSource(); String msg; if (o.equals(cb1)) { msg = "One"; } else if (o == cb2) { msg = "Two"; } else if (o == cb3) { msg = "Three"; } else if (o == cb4) { msg = "Four"; } else if (o == cb5) { msg = "Five"; } else { msg = "Huh?" + o; } System.out.println("Button: " + msg); } }; cb1.addActionListener(al); cb2.addActionListener(al); cb3.addActionListener(al); cb4.addActionListener(al); cb5.addActionListener(al); cb5.setEnabled(false); f.pack(); f.setVisible(true); // f.show(); } }
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???
![]() |
Similar Threads
- including java programs or code in jsp (JSP)
- JTextArea question (Java)
- Java Visualizations with media (Game Development)
- Problem with Java Swing (Java)
- A Progress/Seek Bar for Audio (Java)
- GUId Greif! (Java)
- GUI Builder For Swing (Java)
- JEditorPane Display Errors (Java)
Other Threads in the Java Forum
- Previous Thread: I need help on send data with modem please
- Next Thread: How do i change/get my port number?
Views: 1987 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






