I have a program that extends JFrame and that is what my paint method is drawing on. I am trying to have a JFrame with multiple JPanels within that JFrame, which is not a problem, but how could I have separate paint methods for separate JPanels within one class?
as you can see, I have 2 JPanels. In one of them I'd like to place my graphics (imgPanel), and in the other I would like to have other swing objects. Is it possible to allocate a specific JPanel for graphics?
Much thanks!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.event.*;
public class Mandelbrot extends JFrame implements ActionListener {
private JPanel ctrlPanel;
private JPanel imgPanel;
private final int numIter = 1000;
private final double zoom = 150;
private BufferedImage I;
private double zx, zy, cx, cy, temp;
private int xMove, yMove = 0;
public Mandelbrot() {
super("Mandelbrot Set");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
plotPoints();
Container contentPane = getContentPane();
ctrlPanel = new JPanel();
ctrlPanel.setBounds(600,0,200,600);
imgPanel = new JPanel();
imgPanel.setBounds(0,0,600,600);
imgPanel.repaint();
contentPane.add(ctrlPanel);
contentPane.add(imgPanel);
}
public void plotPoints(){
I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
zx = zy = 0;
cx = (x - 400 +xMove) / zoom;
cy = (y - 300 +yMove) / zoom;
int iter = numIter;
while (zx * zx + zy * zy < 4 && iter > 0) {
temp = zx * zx - zy * zy + cx;
zy = 2 * zx * zy + cy;
zx = temp;
iter--;
}
I.setRGB(x, y, iter | (iter << 20));
}
}
}
public void actionPerformed(ActionEvent ae){
String event = ae.getActionCommand();
}
@Override
public void paint(Graphics g) {
g.drawImage(I, 0, 0, this);
}
public static void main(String[] args) {
new Mandelbrot().setVisible(true);
}
}
as you can see, I have 2 JPanels. In one of them I'd like to place my graphics (imgPanel), and in the other I would like to have other swing objects. Is it possible to allocate a specific JPanel for graphics?
Much thanks!