Re: adding a graphics2d object to a jpanel
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jan 2006 15:11:04 -0800
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/ .
- References:
- Prev by Date: Re: rude Mouse cursor
- Next by Date: Re: adding a graphics2d object to a jpanel
- Previous by thread: adding a graphics2d object to a jpanel
- Next by thread: Re: adding a graphics2d object to a jpanel
- Index(es):
Relevant Pages
|
|