Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Swing / AWT / SWT
clickable line
Ravi Gupta
Ranch Hand
Posts: 38
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi all
I would like to know if there is a way to make a line that connects two buttons clickable? i.e. on being clicked the line should change colour.
help your PC help you best
http://simplepccare.spaces.live.com
Craig Wood
Ranch Hand
Posts: 1535
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import javax.swing.*; public class Clickables extends JPanel { Point selection = new Point(-1,-1); protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Component[] c = getComponents(); for(int j = 0; j < c.length; j++) { Rectangle r = c[j].getBounds(); double x1 = r.getCenterX(); double y1 = r.getCenterY(); for(int k = j+1; k < c.length; k++) { r = c[k].getBounds(); double x2 = r.getCenterX(); double y2 = r.getCenterY(); Color color = (j==selection.x && k==selection.y) ? Color.red : Color.blue; g2.setPaint(color); g2.draw(new Line2D.Double(x1, y1, x2, y2)); } } } public static void main(String[] args) { Clickables test = new Clickables(); test.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; test.add(new JButton("Button 1"), gbc); test.add(new JButton("Button 2"), gbc); test.addMouseListener(test.ml); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,200); f.setLocation(200,200); f.setVisible(true); } private MouseListener ml = new MouseAdapter() { final int S = 6; Rectangle sensor = new Rectangle(S,S); public void mousePressed(MouseEvent e) { Point p = e.getPoint(); sensor.setFrameFromCenter(p.x, p.y, p.x+S/2, p.y+S/2); Container source = (Container)e.getComponent(); Component[] c = source.getComponents(); for(int j = 0; j < c.length; j++) { Rectangle r = c[j].getBounds(); double x1 = r.getCenterX(); double y1 = r.getCenterY(); for(int k = j+1; k < c.length; k++) { r = c[k].getBounds(); double x2 = r.getCenterX(); double y2 = r.getCenterY(); if(new Line2D.Double(x1,y1,x2,y2).intersects(sensor)) { if(selection.x == j && selection.y == k) selection.setLocation(-1,-1); // toggle off else selection.setLocation(j, k); // set values source.repaint(); return; } } } } }; }
A magnificient life is loaded with tough challenges. En garde tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Deploy Java Application On Customer Side?
Text links in applet
Interactive Charts
clickable text
Java Swing Stand-alone application
More...