Java: Undecorated JFrame not displaying Graphics? - java

I've been programming in Java for about six months now and have done many work with Graphics in JPanels and JFrames. But recently I got a problem. All the JFrames I've been making before were always setUndecorated(false), but I needed to make one that was setUndecorated(true). So I tried, like usual, just putting the frame.setUndecorated(true) above all the frame's specs, so it looked something like this:
public static void main(String[] args){
JFrame frame = new JFrame("Frame");
frame.setUndecorated(true);
frame.setSize(600, 800);
frame.setVisible(true);
frame.add(new custompanel());
}
And the custompanel class:
public class custompanel{
public void paintComponent(Graphics g){
g.fillRect(100, 100, 100, 100);
}
}
The g in custompanel doesn't paint anything.
However, if I remove the frame.setUndecorated(true) or change it to frame.setUndecorated(false) it will paint a rectangle.
Any thoughts?

Assuming CustomPanel extends JComponent or JPanel, make the frame visible after adding CustomPanel. Calling super.paintComponent(g) is a good idea to paint any background components.
frame.setVisible(true);

Related

Java: JLabel not getting added on JFrame

Why am I not getting the label on the screen. This is my code :
class Guide extends JComponent {
public static void main(String[] args) {
Guide guide = new Guide();
JFrame frame = new JFrame("Pong Game's Guide");
frame.add(guide);
frame.pack();
frame.setSize(600,500);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
JLabel guideLabel = new JLabel();
guideLabel.setText("GUIDE");
guideLabel.setFont(new Font("Serif",Font.BOLD, 20));
guideLabel.setForeground(new Color(255,0,0));
guideLabel.setBounds(290,25,50,20);
frame.add(guideLabel);
}
public void paintComponent(Graphics g) {
g.setColor(new Color(150,255,150));
g.fillRect(0,0,690,590);
}
}
What am I doing wrong here? Please help.
You’ve made the window visible before adding the label. You will need to revalidate and repaint the container
JFrame by default uses a BorderLayout, which by default only allows a single component to be laid out in each of 5 available positions, which likely to cause issues. Probably better to add it to guide
Try adding the label to the frame, then setting the visibility of the frame to true.

Repaint never reaches paintComponent();

I'm back with a problem about java-graphics by swing... I want to paint some stuff at a jframe, here is the code:
PaintUtil-class:
public class PaintUtil extends JPanel{
public PaintUtil(){
this.setFocusable(true);
this.requestFocus();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println("Repainted");
g.drawstuff...
}
}
Main-class:
public static PaintUtil util = new PaintUtil();
JFrame frame = new JFrame();
frame.setSize(500,600);
frame.setRezisable(false);
frame.add(util);
frame.setDefaultCloseOperation( 3 );
frame.getContentPane().setColor(Color.BLACK);
setup(); //This add some buttons
frame.setVisible(true);
util.repaint(); //not working
util.paintComponent(frame.getGraphics()); //works
Can you guys help me?
There is no error, no message in the console, just nothing
frame.setLayout(null);
Don't use a null layout. Swing was designed to be used with layout managers. Get rid of that statement.
By default the size of your panel is (0, 0) so there is nothing to paint.
You will need to override the getPreferredSize() method of your panel so the layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

How to successfully draw background JPanel once and update foreground JPanel constantly?

I have a custom JLayeredPane, and I am repainting it in my game loop. There are two custom JPanels added into the JLayeredPane. These are foreground and background JPanels. How do I successfully only draw my background JPanel once, (And repaint when window is re-sized or any other reason) to reduce impact on system resources, while continuing to update my foreground JPanel constantly.
To re-iterate, I dont want to constantly repaint the background JPanel in a loop. I want to repaint it only when it is nessessary, as the background does not change. and is large.
In my attempt to do this, I have only drawn the background once. However. the background JPanel is simply not visible. while the foreground JPanel updates as normal. It is almost as if the foreground JPanel paints ontop of the background JPanel, even though I have both of the JPanels set to setOpaque(false)
I have made a mvce which shows my attempt at only drawing the background JPanel once, while updating the foreground JPanel constantly.
The problem with my code is that the background JPanel does not show.
Now. I know that if I were to draw it constantly it would show. But that defeats the purpose of what i'm trying to do. I am trying to only draw it once, and have be seen at the same time
My code successfully only draws the background JPanel once. The problem is that the background JPanel does not show. How do I fix THIS problem
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JLayeredPane {
static JFrame frame;
static Main main;
static Dimension screenSize;
public Main() {
JPanel backPanel = new BackPanel();
JPanel frontPanel = new FrontPanel();
add(backPanel, new Integer(7));
add(frontPanel, new Integer(8));
new Thread(() -> {
while (true){
repaint();
}
}).start();
}
public static void main(String[] args) {
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Game"); // Just use the constructor
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
frame.add(main, BorderLayout.CENTER);
frame.pack();
frame.setSize(screenSize);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class BackPanel extends JPanel{
public boolean drawn = false;
public BackPanel(){
setVisible(true);
setOpaque(false);
setSize(screenSize);
JLabel test1 = new JLabel("Test1");
JLabel test2 = new JLabel("Test2");
add(test1);
add(test2);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
drawOnce(g);
}
public void drawOnce(Graphics g){
if (!drawn){
g.setColor(Color.red);
g.fillRect(0, 0, screenSize.width, 200);
drawn=true;
}
}
}
public class FrontPanel extends JPanel{
public FrontPanel(){
setVisible(true);
setOpaque(false);
setSize(screenSize);
JLabel test = new JLabel("Test");
add(test);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(0+screenSize.width/2, 0, screenSize.width/4, 300);
}
}
}
Try RepaintManager.currentManager(component).markCompletelyClean(component). It will prevent the component from repainting. You might need to do this after each time you add new components.
http://docs.oracle.com/javase/6/docs/api/javax/swing/RepaintManager.html#markCompletelyClean%28javax.swing.JComponent%29
I don't know if this two lines of code
super.paintComponent(g);
drawOnce(g);
are the root of problem, I sincerly don't remember how paintComponent works (a test could help) but try to swap them :
drawOnce(g);
super.paintComponent(g);
maybe, on your original version, you tells JVM to paint the whole component and, only after the AWTEvent has been added to the queue, to draw what you need.
I guess that the awt's documentation will explain it.

Drawing with AWT and components in Java

I'm currently making a game in Java with AWT. The main class extends Frame, and I've been using it to draw the graphics using .getGraphics() and .drawRect(). This has been working fine, except that when I add components like labels to the frame it stops rendering the graphics and only displays the components.
Don't
Use getGraphics() to paint. That is not the proper way.
Try and paint on top-level containers like JFrame
Instead
Paint on a JPanel or JComponent (I prefer the former)
Override the paintComponent(Graphics g) method of the JPanel. Do all your painting in this method, use the implicitly passed Graphics context. You never have to actually call paintComponent as it will be implicitly called for you.
See
Performing Custom Painting for more details on painting in Swing
Edit
Just noticed you are using AWT. You really should consider upgrading to Swing. Otherwise, instead of paintComponent, you're going to want to override paint, as AWT components don't have a paintComponent method. But I strongly urge you to use Swing
Example (Using Swing)
public class SimplePaint {
public SimplePaint() {
JFrame frame = new JFrame();
frame.add(new DrawPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class DrawPanel extends JPanel {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(50, 50, 150, 150);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SimplePaint();
}
});
}
}

Java - GUI (swing) - Null Pointer Exception

I got 2 classes:
- 1st. makes a frame (JFrame) and adds a panel (JPanel) on it
- second one makes the panel and draws a rectangle on it (at least i thought it would)
this is the first class
class Frame {
JFrame frame;
Panel panel;
void draw() {
frame = new JFrame ("qwertz");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(300,200);
panel = new Panel();
panel.setLayout(null);
panel.paint();
frame.add(panel);
}}
and the second
class Panel extends JPanel {
void paint() {
Graphics g = getGraphics();
g.drawRect(50,50,90,70);
}}
when i call the draw() method from the first class it throws this exception at me:
java.lang.NullPointerException
at Panel.paint(Panel.java:8) (( g.drawRect(50,50,90,70); ))
at Frame.draw(Frame.java:15) (( panel.paint(); ))
That's not how you're supposed to paint. To paint a component, override the paintComponent(Graphics g) method of the JPanel then call repaint();
class MyPanel extends JPanel {
#Override // <-- this makes a compiler error if you typod the method name
public void paintComponent(Graphics g) {
g.drawRect(50,50,90,70);
}
}
and
panel = new MyPanel();
panel.setLayout(null);
panel.repaint(); // <<---- Look here! It says repaint() not paint()
frame.add(panel);
Also, if all you have to do is paint on this panel, I'd consider using a plain-old Component, and overriding paint(Graphics g) instead of paintComponent(Graphics g). paintComponent(Graphics g) is exclusively for swing components.
instead of implementing the paint method, you should implement the paintComponent(Graphics g) method. This way, the graphics object you have is valid.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
You are trying to draw the panel before it is added to the Frame. Try to move frame.paint(); below frame.add(panel);. Additionally if you are using Swing you should use JPanel instead of Panel.

Categories

Resources