2

How do I convert a paintComponent to something that I can manipulate with a layout in a JFrame?

So, I'm running into an issue. I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.

My issue is this: I have a program that the user inputs a number. Based on this number, the program calculates a circle and draws it out with a paintComponent method that has a for loop inside of it. The "pixels" that the circle is drawn with are actually fillRect methods. The current method of getting a user-input that I am using is a JOptionPane showInputDialog. This is MOSTLY fine, but I want the user to be able to select from a set of pre-defined numbers. Somebody suggested that I use a JComboBox, but I don't know how I would convert the paintComponent to something that would be usable by a layout manager (which a JComboBox must use, as far as I've learned). I know the dimensions of the paintComponent (805px by 805px) and there is no situation where it will change. If I could get some help with this bit, I am confident that I can figure out using a layout manager myself.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tssguy123
  • 185
  • 1
  • 1
  • 9
  • 2
    Do you have access to the internet? I heard there's a ton more information there than books. – Kayaman Dec 15 '13 at 12:53
  • @AndrewThompson Sorry about that, just woke up. How do I convert a `paintComponent` to something that I can manipulate with a layout in a `JFrame`? – tssguy123 Dec 15 '13 at 12:56
  • The question is the place for it. I've done an edit so we can delete this noise. – Andrew Thompson Dec 15 '13 at 12:57
  • @Kayaman There sure is! I said that because it's inevitable that somebody was going to tell me to read a book, or search. One of which I have done, and the other is not an option... :) – tssguy123 Dec 15 '13 at 12:57
  • 1
    `I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.` - Start with the [Swing Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html). – camickr Dec 15 '13 at 16:42

2 Answers2

2

Another way to paint (besides custom painting) is to paint to a BufferedImage. The image can then be displayed in a JLabel.

Examples:

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You don't know the dimensions of paintComponent because it's a method, and methods don't have dimensions. You probably know the dimensions of a JPanel or a JFrame or whatever your component is.

You should separate the panel where you do the painting, and a different panel which would contain any comboboxes or other inputs you decide to put in. That way you can keep your drawing panel as is, and they won't interfere with each other. You'll want to search for the tutorial on LayoutManagers.

Kayaman
  • 72,141
  • 5
  • 83
  • 121