Re: adding a graphics2d object to a jpanel



6e wrote:
is there a way to add a graphics2d object that alreasy exists to a
jpanel?

I have overridden jpanel as seen below, but cannot see the graphic
object....

any ideaS?



public class JPanelGraphics2d extends JPanel{

	private static final long serialVersionUID = 001;

	Graphics2D m_g2d;
	private int maxUnitIncrement = 1;

	JPanelGraphics2d (Graphics2D g){
		m_g2d = g;
		setPreferredSize(new Dimension(450, 450));
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		if (m_g2d != null){
			Graphics2D g2 = (Graphics2D)g;

			m_g2d.setBackground(Color.blue);
			g2 = m_g2d;
			System.out.println("always hit, but never shown on screen");
		}
		System.out.println("painting jpanelGraphics2d");

	}
}


The Graphics object passed into paintComponent() is a Graphics2D.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
    public test() {
        setPreferredSize(new Dimension(400,300));
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;

        g2d.setColor(Color.BLUE);
        g2d.fillRect(0,0,getWidth(),getHeight());

        g2d.setColor(Color.WHITE);
        g2d.drawString("Hello World!",40,150);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new test());
        f.pack();
        f.setVisible(true);
    }
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: cant see Jpanel and Jbutton
    ... > public class Test extends JFrame ... > JPanel jpanel; ... > class PaintPanel extends JPanel ...
    (comp.lang.java.help)
  • get the value of the position of JPanel inside the JFrame
    ... I have a JFrame and inside two JPanel one of them is on NORTH and the second ... And When I get the Container which is the JPanel, ... Les informations contenues dans le present message et dans tous les fichiers electroniques qui y sont attaches, sont strictement confidentielles et ne sont destinees qu'a l'usage de la personne dont le nom apparait ci-dessus et de toute autre personne specifiquement autorisee a les recevoir. ...
    (comp.lang.java.programmer)
  • Re: Please help the GridLayout problem
    ... I am tring to add several small JPanel to a big JPanel (using ... should never add components directly to a JFrame. ... your panels should be nor how big the JFrame should be. ... And since the GridLayout layoutManager's layout strategy ...
    (comp.lang.java.gui)
  • Re: odd mouselistener issue
    ... I've got a JFrame with borderlayout. ... So all I can do is give you an example of how to add a MouseListener to a JPanel. ... Don't forget that BorderLayout will cause your component to expand to the size of your container. ... public static void main{ ...
    (comp.lang.java.programmer)
  • Druckproblem
    ... Ich habe ein JFrame wo ein JPanel in einem JInternalFrame erstellt ... JFrame voll gedruckt. ... Prev by Date: ... Next by Date: ...
    (de.comp.lang.java)