开发者

How to have drawn objects(g.draw(...)) respond to mouse events?

开发者 https://www.devze.com 2022-12-21 12:06 出处:网络
I have this program where it draws a bar graph in a JFrame.The bars of the bar graph change color when I click on them.So my question is whats the best way to approach this?The bruteforce way,i.e. cal

I have this program where it draws a bar graph in a JFrame. The bars of the bar graph change color when I click on them. So my question is whats the best way to approach this? The bruteforce way,i.e. calculate the mouse click and see if it falls within the bars' range, o开发者_StackOverflow中文版r extends the BufferedImage class and have it implement mouselistener? Because if i were to create a class that extends a jpanel and override its paintcomponent method, it would for sure not run efficiently. And the bars of the bar graph will be animating also, as in values will be fed to the program and the graphs get updated all the time.


I would personally go for the "listen for clicks over the whole component and work out which bar a click falls on". It shouldn't be hard to do - just division to work out which bar, then a bounds check to work out if the particular bar is long enough for that point to be painted.

Introducing "one control per bar" feels like a recipe for trouble in terms of getting layout etc right. I'm sure it can be done - and I'm sure those with more GUI experience have less trouble with layout than I do - but I strongly suspect it would much more work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号