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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
how to paint some shape over another JComponent
Edward Chen
Ranch Hand
Posts: 798
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
how to paint some shape
over
another JComponent, such as JLabel ? The coding below is my try. But it doesn't work.
Thanks
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame(); f.setContentPane(new ContentPanel()); f.setSize(400, 400); f.setVisible(true); } } class ContentPanel extends JPanel implements MouseListener { int x; // hold the click point position int y; // hold the click point position JLabel imageLabel; public ContentPanel() { imageLabel = new JLabel(); imageLabel.setIcon(new ImageIcon(getClass().getResource("Adele.jpg"))); JPanel panel = new JPanel(); panel.add(imageLabel); addMouseListener(this); add(panel); } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); x = p.x; y = p.y; System.out.println("x: " + p.x + " y: " + p.y); repaint(); invalidate(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.drawRect(x, y, 200, 200); } public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
java swing repaint
Moveable tooltips
Get x,y coords outside of windows/screen using mouselook technique
Wanted: example mouse coordinates->world coordinates
Stamping an Image
More...