• 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

Design Advice

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!
I've been struggling for a couple of hours to achieve something pretty simple.
I need to get a custom painting Panel which has many shapes and a field in which I'm displaying the name of the shape that has been mouse over'ed( mouseEntered event).

My design is very clumsy.

First I will explain how I thought I could design this and then add the code below.
Well, I thought I should use inheritance since I have many figures that are shapes.
Every shape will be painted in a different paint Panel(many classes that extends JPanel) so I can control them individually. I thought I could make the superclass extends JPanel and then override paintComponent(Graphics g) method in each and every subclass I already have or will add in the future.
I will also need an MouseListener for every shape so like the above scenario, I thought I could make the superclass implement MouseListener and override itself the methods I don't need in my subclasses...and in my subclasses I only need mouseEntered event( equivalent to mouse over).
But here arises the problems: Where will the frame be created and all the panels that will be added to the frame...I used gridlayout for the frame's layout so I can add multiple paint Panels!
Then...how on earth should I make to get the field in the mouseEntered() method of every subclass and also in the class where I'm creating the frame, an auxiliary class!

This is my code...please tell me what am I wrong...thanks in advance!!



As you can see, there are only 3 shapes but many other could be added due to inheritance...Don't ask me why I've used an abstract class which I cannot instantiate...I'm just at the beginning...please tell me all the big mistakes I made...and how can I make that textfield be visible in the subclasses as well as the auxiliary class. Thanks in advance
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Sing/AWT forum. You'll probably get better answers there.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A shape isn't a panel, so you should not be extending the panel class. I am afraid you will have to start again. If you can design shapes without using a single class from packages whose names start with java.awt or javax.swing you have some sort of chance.

Remember the GUI classes are not there for modelling things; they are there for displaying things and should not be used for anything else.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some points to get you started.
1) You need varied shapes. All shapes can be termed as polygons.
2) A polygon by definitition, can be defined as a set of points
3) These points can be represented by sets of co-ordinates (x,y)
4) You need to "draw" these shapes. The recommended way to do custom drawing/painting is via the Graphics object
5) These shapes need to be drawn "on" something, a canvas if you want to think about it (Though do NOT take it to be the existing Canvas class)

In pseudo code
1) Subclass a JPanel
2) All custom painting needs to happen inside the paintComponent(Graphics g); which you will need to override
3) Define your polygons. Fortunately for you, java has a built in Polygon class which you can use.
4) In your paintComponent, ask your graphics object to draw these polygons. Again, fortunately for you, the Graphics class has a convenient method to draw polygons (psst it can even fill polygons if you wish!)

Find more info on painting here
https://docs.oracle.com/javase/tutorial/uiswing/painting/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic