paintComponent is not working at all - java

I'm using paintComponent to make a GUI for a class assignment and it's just not affecting the appearance of the GUI at all. To start, I'm just setting the background to white. The following code works:
import javax.swing.*;
import java.awt.*;
public class PA05a extends JPanel {
public static void main(String[] args) {
JFrame window = new JFrame("MouseDrawDemo");
JPanel content = new JPanel();
content.setBackground(Color.WHITE);
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(400,300);
window.setVisible(true);
}
}
but this does not:
import javax.swing.*;
import java.awt.*;
public class PA05a extends JPanel {
public static void main(String[] args) {
JFrame window = new JFrame("MouseDrawDemo");
JPanel content = new JPanel();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(400,300);
window.setVisible(true);
}
#Override
public void paintComponent(Graphics g) {
//add backdrop
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
}
}
I can't just not use paintComponent because I'll later be adding things that will change from frame to frame. Can someone pinpoint where I'm missing something?

JPanel content = new PA05a();
You did not create an object of PA05a. ;)

You just forgot to create your object. Change your code to:
import javax.swing.*;
import java.awt.*;
public class PA05a extends JPanel {
public static void main(String[] args) {
JFrame window = new JFrame("MouseDrawDemo");
JPanel content = new PA05a();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(400,300);
window.setVisible(true);
}
#Override
public void paintComponent(Graphics g) {
//add backdrop
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
}
}

Related

Adding JPanel to JLayeredPane causes paint() method to not get called

To provide some background info that might help, I am creating the game pong and I decided to add an escape/pause menu (when you press escape on the keyboard a menu pops up with some settings), I looked around and found that the best way to do this is to use JLayeredPane and add another JPanel on top. However, when I added my 'painter' class to the JLayeredPane, the paint(Graphics g) method stopped getting called (it worked fine when I just added it to the JFrame).
import javax.swing.*;
public class Game extends JFrame {
public static Painter painter;
public Game() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLayeredPane lp = getLayeredPane();
painter = new Painter();
add(lp);
}
public static void main(String[] args) {
Game frame = new Game();
frame.setVisible(true);
painter.repaint();
}
}
And here is my Painter class
import java.awt.*;
import javax.swing.*;
public class Painter extends JPanel {
public void paint(Graphics g) {
System.out.println("Working");
super.paint(g);
}
}
Instead of add(lp);, I originally tried lp.add(painter); in which case the paint method never got called. By doing add(lp) I get an IllegalArgumentException for adding container's parent to itself.
Here is mcve of using LayeredPane :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Game extends JFrame {
public Game() {
setSize(250, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLayeredPane lp = getLayeredPane();
//assign layout manager. otherwise you need to set content
//bounds (not recommended)
lp.setLayout(new BorderLayout());
Painter painter = new Painter();
lp.add(painter,1);
}
public static void main(String[] args) {
Game frame = new Game();
frame.setVisible(true);;
}
}
class Painter extends JPanel {
#Override
protected void paintComponent(Graphics g) { //do not override paint
super.paintComponent(g);
g.drawString("Working",50,50);
}
}

Swing Java repaint not working using 2 jpanels

As you can see from the code I am expecting to see a rectangle on DrawingPanel but I am not sure why repaint() method is not working. Any help will be much appreciated. I also tried revalidate method but that is not working either.
Here are my Classes:
import javax.swing.*;
import java.awt.*;
public class Designer extends JFrame {
static JFrame f = new Designer();
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel();
public Designer(){
Rectangle bounds = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getMaximumWindowBounds();
splitPane.setTopComponent(propertyPanel);
splitPane.setBottomComponent(drawingPanel);
add(splitPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(bounds);
setVisible(true);
}
public static void main(String[] args) {
System.out.println("App started...");
}
}
Class for first Jpanel: PropertyPanel.Java
import javax.swing.*;
import java.awt.event.*;
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
public PropertyPanel(){
JButton addShapesBtn = new JButton("Add");
addShapesBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
drawingPanel.addRectangle();
}
});
add(addShapesBtn);
}
}
Class for second Jpanel: DrawingPanel.Java
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class DrawingPanel extends JPanel{
private List<Rectangle> squares = new ArrayList<Rectangle>();
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
System.out.println("painting");
for (Rectangle rect : squares) {
g2.draw(rect);
}
}
public void addRectangle() {
Rectangle rectx = new Rectangle(10, 10, 100, 100);
squares.add(rectx);
repaint(); <-- this is not working
}
}
Your repaint works fine. Your problem is that you're calling addRectangle() to the wrong DrawingPanel instance, not the one that is currently displayed in the GUI. To solve this, pass a reference from the displayed one to the code that needs to call the method.
To see that this is correct, simply search this page for new DrawingPanel(). You should see this only once in your own code (and your code posted). You see it twice meaning you've created two instances of this.
e.g., change this:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
public PropertyPanel(){
to this:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel; // don't create new
public PropertyPanel(DrawingPanel drawingPanel){
this.drawingPanel = drawingPanel;
and then pass in the true DrawingPanel into this object on creation:
DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel(drawingPanel);
Better still: structure your code in a M-V-C way, and use dependency injection to make connections -- but this may be overkill for your simple GUI.

Java: paintComponent() Oval is not appearing in Netbeans

I'm trying to learn how to draw an oval in java but the paintComponent I made is not being called by anything, and attempting to call it only causes more issues.
The program runs successfully but the image I want displayed isn't showing up.
import java.awt.*;
import javax.swing.*;
public class TEST2{
public void paintComponent(Graphics g){
g.drawOval(70, 70, 100, 100);
}
public static void main(String[] args) {
TEST2 gui = new TEST2();
gui.setUpFrame();
}
public void setUpFrame(){
JFrame frame = new JFrame();
frame.setTitle("Images should be in this program");
frame.setSize(600,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Start by taking a look at Painting in AWT and Swing and Performing Custom Painting
In order to be able to perform custom painting in Swing, you must...
Inherit from a swing based component (like JComponent or JPanel)
You must then override it's paintComponent method and perform you custom painting within this method.
Add this component to something that is displayable (like a JFrame)
You should make sure to call super.paintComponent before doing any custom painting
To ensure that you're not making any (common) mistakes, you should use the #Override annotation
As an example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test2 extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(70, 70, 100, 100);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setTitle("Images should be in this program");
frame.add(new Test2());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
The paintComponent() method is a method that you override and it should be accessed inside a class that extends JPanel. You can create a new class that extends JPanel and override the paintComponent() method to draw your oval. You will also have to add the new JPanel to your JFrame for it to display. I modified your code below it should display the oval now. As Madprogrammer noted you should probably construct your GUI within the context of the edt to avoid concurrency issues but I will omit that for simplicity.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Test gui = new Test();
gui.setUpFrame();
}
public void setUpFrame() {
JFrame frame = new JFrame();
frame.setTitle("Images should be in this program");
frame.setSize(600, 300);
JPanel oval = new oval();
frame.setContentPane(oval);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class oval extends JPanel{
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(70, 70, 100, 100);
}
}
}

Unable to set JPanel's Background in my Swing program.

I set a JPanel as a contentPane of my JFrame.
When I use:
jPanel.setBackground(Color.WHITE);
The white color is not applied.
But when I use:
jFrame.setBackground(Color.WHITE);
It works... I am surprised by this behaviour. It should be the opposite, shouldn't it?
SSCCE:
Here is an SSCCE:
Main Class:
public class Main {
public static void main(String[] args) {
Window win = new Window();
}
}
Window Class:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
private Container mainContainer = new Container();
public Window(){
super();
this.setTitle("My Paint");
this.setSize(720, 576);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainContainer.setBackground(Color.WHITE); //Doesn't work whereas this.setBackground(Color.WHITE) works
this.setContentPane(mainContainer);
this.setVisible(true);
}
}
Container Class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Container extends JPanel {
public Container() {
super();
}
public void paintComponent(Graphics g) {
}
}
The reason is very simple include the following line
super.paintComponent(g);
when you override paintComponent.
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
Now it works perfectly.
You should always do this unless you have a very specific reason to do so .
[PS:Change the colour to red or something darker to notice the difference as sometimes it becomes difficult to differentiate between JFrame's default grey colour and White colour]
With my testcode it works the way you expected it to work:
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(new Dimension(400,400));
f.setLocationRelativeTo(null);
JPanel p = new JPanel();
p.setSize(new Dimension(20,20));
p.setLocation(20, 20);
//comment these lines out as you wish. none, both, one or the other
p.setBackground(Color.WHITE);
f.setBackground(Color.BLUE);
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

Issue with this simple code

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyDrawPanel extends JPanel{
public void paintComponents(Graphics g){
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().add(paintComponents(g));
frame.setVisible(true);
}
}
I think I should add something arguments in frame.getContentPane().add(paintComponents(g));.
I looked up Graphics class but I'm still struggling with it. What should be the parameter of it?
try this
public class MyDrawPanel extends JPanel{
MyDrawPanel()
{
setOpaque(true);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyDrawPanel());
frame.setBounds(10,10,500, 500);
frame.setVisible(true);
}
}
I'm no awt expert, but what I think you want to do is add a Canvas object to your content pane from the JFrame, then paint a Graphics object on it.
Okay, this is what I came up with:
public class MyDrawPanel extends JPanel
{
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
MyDrawPanel panel = new MyDrawPanel();
panel.setOpaque(true);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
createAndShowGUI();
}
});
}
}
Notes:
There are several problems with your line frame.getContentPane().add(paintComponents(g));. What you said is "add to the content pane the result of calling paintComponents on g. Where did g come from? You can not used a variable until declared. The result of calling paintComponents is void which means the result cannot be used as an argument to a method. Presumably you had compiler errors.
I changed paintComponents to paintComponent. The former is used to control painting of subcomponents and in general should not be overridden.
Swing objects should not be created on the main thread. The details are complicated for a beginner (and described here in detail). Mostly you can just memorize the SwingUtilities.invokeLater pattern used above.

Categories

Resources