2

I am trying to code an application that resembles a Karnaugh Map, and I want to draw an ellipse to enclose a group of numbers.

What I have is a JFrame, which has a JPanel with a GridLayout, where I add several other panels to it. So, in the end, I have a grid-like interface. I want the ellipse to be on top of these panels.

I am trying to use Graphics2D and the method draw(), but I cannot even begin to understand how to instantiate the Graphics2D object.

What I've done is: get the Graphics object from the JPanel with the GridLayout, then

g is the Graphics object.

Graphics2D g2 = (Graphics2D) g;

g2.draw(new Ellipse2D.Double(x, y,rectwidth,rectheight));

Then repaint() the gridlayout panel, but nothing happens and it doesnt throw an exception or anything.

What am I doing wrong?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Aguaka MPA
  • 43
  • 1
  • 4
  • 1
    You shouldn't instantiate `Graphics2D`, you should override `paintComponent(Graphics)` from the `JPanel`, and do your drawing there. See here : http://stackoverflow.com/questions/2509561/how-to-draw-a-filled-circle-in-java – Arnaud May 19 '16 at 07:00
  • Oh thanks, but how do java manage the `paintComponent(Graphics)`? I mean, can I just call it when I want to? – Aguaka MPA May 19 '16 at 07:05
  • This method is called automatically every time the component gets repainted . You may call repaint() when you really feel the need to manually trigger a repaint, but I don't think you will have to. – Arnaud May 19 '16 at 07:06
  • I see, I'll try to do it. Thanks! – Aguaka MPA May 19 '16 at 07:10
  • 1
    Look up `glassPane` or `JLayer`, for [example](http://stackoverflow.com/questions/27516581/how-to-highlight-uniform-visually-select-draw-transparent-overlay-jpanel/27516784#27516784), [example](http://stackoverflow.com/questions/22289157/java-netbeans-ide-overlay-images-chessboard-chesspiece/22289435#22289435), [example](http://stackoverflow.com/questions/16703650/how-to-convert-jgraph-to-a-glass-pane/16704288#16704288) – MadProgrammer May 19 '16 at 09:23

1 Answers1

4

The basic idea is examined in Performing Custom Painting. The approach is to override the paintComponent() method of a suitable JComponent and render your content relative to it's current size when asked. This example draws numbers; this example illustrates overlapping translucent shapes suitable for such a map.

Alternatively, add suitable components, e.g. JLabel or JButton, to a GridLayout, as shown here. Render the desired shapes in a LayerUI wrapped in a JLayer, as shown in How to Decorate Components with the JLayer Class and the examples cited here by MadProgrammer: here, here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Your "examples cited here" link points to your answers and not MadProgrammer's answer. – Bilesh Ganguly May 19 '16 at 11:08
  • 1
    @BileshGanguly: Thank you for a helpful edit; although the comment link is correct, I've edited the answer to include the examples [cited](http://stackoverflow.com/questions/37315982/how-to-draw-an-ellipse-on-top-of-panel/37319505?noredirect=1#comment62157701_37315982). – trashgod May 19 '16 at 13:02
  • Thank you all, I was not using well the Graphics object, I could do what I wanted with that. – Aguaka MPA Jun 01 '16 at 20:08
  • Glad you got it working; this related [example](http://stackoverflow.com/a/2545642/230513) draws on the glass pane. – trashgod Jun 01 '16 at 22:28