2 Replies - 312 Views - Last Post: 28 December 2012 - 11:41 AM Rate Topic: -----

#1 DrHau5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-December 12

Drawing graphics to separate JPanels

Posted 28 December 2012 - 09:59 AM

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?

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing graphics to separate JPanels

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing graphics to separate JPanels

Posted 28 December 2012 - 10:59 AM

You can, if the paint() is very short append it to the JPanel creation

	int foo = 10;
	JPanel x = new JPanel() {
		public void paint(Graphics g) {
		      super.paint(g);
		}
	};;
	double pi = 3.14159;



but better to have inner class... much clearer
And I hate to have code that is actually executed in my constructor :)

class MyFrame extends JFrame {

     MyFrame() {
        add(new MyPanel1());
        add(new MyPanel2());
     }

      class MyPanel1 extends JPanel {
         MyPanel1() {
         }

         public void paint(Graphics g) {
             super.paint(g);
             ...
         }
       }     
      
       class MyPanel2 extends JPanel {
         MyPanel2() {
         }

         public void paint(Graphics g) {
             super.paint(g);
             ...
         }
       }
}


This post has been edited by pbl: 28 December 2012 - 10:46 PM

Was This Post Helpful? 3
  • +
  • -

#3 DrHau5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-December 12

Re: Drawing graphics to separate JPanels

Posted 28 December 2012 - 11:41 AM

View Postpbl, on 28 December 2012 - 10:59 AM, said:

You can, if the paint() is very short append it to the JPanel creation

	JPanel x = new JPanel() {
		public void paint(Graphics g) {
		}
	};;



but better to have inner class... much clearer

class MyFrame extends JFrame {

     MyFrame() {
        add(new MyPanel1());
        add(new MyPanel2());
     }

      class MyPanel1 extends JPanel {
         MyPanel1() {
         }

         public void paint(Graphics g) {
             super.paint(g);
             ...
         }
       }     
      
       class MyPanel2 extends JPanel {
         MyPanel2() {
         }

         public void paint(Graphics g) {
             super.paint(g);
             ...
         }
       }
}



Thank you VERY much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1