tssguy123
tssguy123

Reputation: 185

Using a paintComponent with layout

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.

Upvotes: 2

Views: 463

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168835

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

Examples:

Upvotes: 2

Kayaman
Kayaman

Reputation: 73568

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.

Upvotes: 0

Related Questions