Swing Graphics on JFrame
Using Java Graphics, I tried to draw a simple rectangle.
How Applet
it works well, but when I use it to display on JFrame
, the rectangle zooms in, but with some kind of unusual background
Here is the code:
package graphicsex;
import java.awt.Graphics;
public class Graphics2 extends javax.swing.JFrame {
public static void main(String[] args) {
Graphics2 inst = new Graphics2();
inst.setVisible(true);
}
public Graphics2() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawRect(10, 20, 30, 40);
}
}
Then I tried using JTextArea
using these two classes, but in this case the rectangle is not displayed at all.
GraphicsEx1.java:
package graphicsex;
import javax.swing.JTextArea;
import java.awt.Graphics;
public class GraphicsEx1 extends javax.swing.JFrame {
private JTextArea jTextArea1;
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GraphicsEx1 inst = new GraphicsEx1();
inst.setVisible(true);
}
public GraphicsEx1() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jTextArea1 = new JTextArea();
getContentPane().add(jTextArea1);
jTextArea1.setBounds(7, 7, 371, 245);
jTextArea1.setEnabled(false);
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
postInitGUI();
}
public void postInitGUI() {
DisplayItems dp = new DisplayItems();
jTextArea1 = dp;
dp.setVisible(true);
this.add(jTextArea1);
}
}
And DisplayItems.java:
package graphicsex;
import java.awt.Dimension;
import java.awt.Graphics;
public class DisplayItems extends javax.swing.JTextArea {
public DisplayItems() {
super();
initGUI();
}
private void initGUI() {
try {
setPreferredSize(new Dimension(400, 300));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawRect(10, 20, 50, 100);
g.drawString("Kalai", 90, 150);
}
}
Can anyone help me to render graphics components on any swinging containers like JFrame,
JPanel or
JTextarea` etc.
source to share
It is not recommended to override the paint
top-level container method ...
JFrame
contains a number of important layers on which many other components fit, buy this ...
public void paint(Graphics g){
g.drawRect(10,20,30,40);
}
You have successfully stopped any of the child components from starting drawing, or in fact anything other than your rectangle.
(The Secret Life of a Top-Level Swing Container - Image taken from How to Use Root Panels )
While some may suggest a simple call addition super.paint(g)
, I would not recommend it.
Better to use something like a glass panel JFrame
... This will allow you to paint the components that are inside the frame. If you need to paint components, replace the content pane JFrame
.
You can find...
- Performing custom painting
- Painting in AWT and Swing
- How to decorate components with the JLayer class
Using...
Update
I think I am starting to see your problem trying to do a custom paint on the text area.
The problem is that it paintComponent
paints the background and the text in one stroke, which means that any painting you make before the call super.paintComponent
will be processed, but any painting made over it will paint the text.
You can set the text area to be opaque and draw the background yourself ...
public class CustomTextArea extends JTextArea {
public CustomTextArea() {
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 100);
super.paintComponent(g);
}
}
The problem is that it is easy for people to rest the opaque level and destroy your work. You can of course override setOpaque
or getOpaque
, but how do you know when the user actually wants the component to be transparent so you can stop populating the base?
source to share
* Using jPanel *
using jpanel, graphic elements can be drawn;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class GraphicsEx1 extends javax.swing.JFrame {
private JScrollPane jsp1;
private JTextArea jta1;
private JPanel jpnl1;
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch(Exception e) {e.printStackTrace();}
}
public static void main(String[] args) {
GraphicsEx1 inst = new GraphicsEx1();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public GraphicsEx1() {
super();
initGUI();
postInitGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jsp1 = new JScrollPane();
getContentPane().add(jsp1);
jsp1.setBounds(10, 32, 372, 223);
{
jpnl1 = new JPanel(); //<----------------
jsp1.setViewportView(jpnl1); //<----------------
jpnl1.setBackground(new java.awt.Color(255,255,255));
jpnl1.setLayout(null);
//jpnl1.setPreferredSize(new java.awt.Dimension(359, 327));
}
}
pack();
setSize(400, 300);
} catch (Exception e) {e.printStackTrace();}
}
public void postInitGUI(){
frmGrpView gp=new frmGrpView();
jta1=new JTextArea();
jta1=gp;
jta1.setBounds(0,0, 336, 197);
jta1.setVisible(true);
//jpnl1.setBounds(0, 0, 336, 197);
jpnl1.add(jta1); //<----------------
jpnl1.revalidate();
jsp1.revalidate();
}
}
//----------------------- Second Class --------------------------
class frmGrpView extends JTextArea{
public frmGrpView() {
super();
setEditable(false);
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(10, 10, 100, 100);
}
}
source to share