paintComponent() not being called - java

Here is a little program that should (in theory) draw an image of a ball on screen.
The problem is that paintComponent seems to not get called. The program consists of two classes.
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ScreenSaver extends JPanel {
private static final long serialVersionUID = 001;
public static void main(String[] args) {
new ScreenSaver();
}
public ScreenSaver() {
new Window(1600, 900, "ScreenSaver", this);
}
//----------------------------------------------------------------------------------
private static BufferedImage ball;
public static BufferedImage getBallSprite() {
try {
File pathToBall = new File("ball.png");
ball = ImageIO.read(pathToBall);
} catch (IOException ex) {
ex.printStackTrace();
}
return ball;
}
}
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends Canvas {
private static final long serialVersionUID = 002;
public Window(int width, int height, String title, ScreenSaver ScreenSaver) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
repaint();
}
public void paintComponent(Graphics g) {
System.out.println("Painting...");
BufferedImage ball = ScreenSaver.getBallSprite();
g.drawImage(ball, 0, 0, 100, 100, this);
}
}
As you can see, I tested if paintComponent was called using a console message. Sadly this was not the case. Can someone explain?

java.awt.Canvas does not inherit from JComponent so paintComponent won't be called automatically. You can create a new custom window instead to create a Swing-centric component
public class MyWindow extends JComponent {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}

It's no wonder that paintComponent is not called, because Canvas has no implementation of paintComponent which you can override. With a canvas you have to overwrite paint for your purposes. In your code you use both a JPanel and a Canvas, which is not necessary at all. Use either of the two.
The following is an example with a Canvas:
import java.awt.*;
import javax.swing.*;
public class ScreenSaver extends Canvas{
public static void main(String[] args) {
JFrame window = new JFrame("Screensaver");
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
ScreenSaver canvas = new ScreenSaver();
canvas.setPreferredSize(new Dimension(1600, 900));
canvas.setBackground(Color.BLACK);
window.add(canvas);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
#Override
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillOval(100, 100, 100, 100);
}
}
The annotation Override above the method to be overwritten ensures that the compiler can issue a warning message if the overwritten method does not exist or there is a typo. I hope this helps you further.

Related

Java questions about coordinates with Graphics

If I create a JFrame 800x600 pixels and draw a line from (0,0) to (800,600) it doesn't go from corner to corner, so, where is the (0,0) and where is the (800,600)?
Here is the code
import java.awt.Graphics;
import javax.swing.JFrame;
public class Point0_0test extends JFrame {
public Point0_0test() {
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.drawLine(0, 0, 800, 600);
}
public static void main(String[] args) {
Point0_0test test = new Point0_0test();
test.setVisible(true);
}
}
Here you can see what appears when the program is running
If you want a drawing area that's 800 x 600 pixels, then set a drawing area that's 800 x 600 pixels. Who cares how big the frame is?
Here's a simple drawing GUI that I created. I made it 400 x 300 pixels so it would fit in the answer easier.
Here's the code. It's a minimal, runnable example for setting the size of the drawing area.
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SimpleDrawingArea implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleDrawingArea());
}
#Override
public void run() {
JFrame frame = new JFrame("Simple Drawing Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(400, 300));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(4f));
g2d.drawLine(0, 0, 400, 300);
}
}
}
The JFrame size and coordinates count the size of the decorations, such as the top part of the window contains the bar with the close button, the rest contain the extra outline that is added on Windows(Ubuntu, at least, doesn't seem to add an extra outline). In order to get a line like you would want to, you should use JFrame.getInsets(), which returns the size of the GUI that decorates the JFrame, like this:
import java.awt.Insets;
#Override
public void paint(Graphics g) {
super.paint(g);
Insets in = getInsets();
g.drawLine(in.left, in.top, 800-in.right, 600-in.bottom);
}
Edit: this means that you don't have an actual 800x600 space. The Insets class seems to be "created" when setVisible(true) is called, as far as I can tell. So this would be how the code for that looks like:
import javax.swing.JFrame;
import java.awt.Insets;
import java.awt.Graphics;
public class InsetsTest extends JFrame {
public InsetsTest() {
super();
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
Insets insets= getInsets();
setSize(800+insets.right+insets.left,600+insets.top+insets.bottom);
setLocationRelativeTo(null);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Insets in = getInsets();
g.drawLine(in.left, in.top, 800+in.left, 600+in.top);
}
public static void main(String[] args) {
InsetsTest test = new InsetsTest();
test.setVisible(true);
}
}```
The window size should be defined without OS specific window decoration.
Try to add
this.setUndecorated(true);
before the
this.setVisible(true);

Repaint() does not work after adding more components to JPanel in Swing, Java

I am starting with Java and want to make a simple pong game to get into the ways of displaying stuff in java. I have created 2 classes that both extend JPanel and call the repaint() function every 16.6 milliseconds. I added both to a JPanel which I added to the frame, but only the component I added first displays.
I've tried to revalidate() after repaint() in both classes and made sure that the Timer actually works and that it's actionPerformed() method is actually called.
This is the main method:
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Pong");
//...
JPanel mainPanel = new JPanel();
mainPanel.add(new Player());
mainPanel.add(new Ball(frameWidth/2, frameHeight/2 -40));
frame.add(mainPanel);
}
}
This is the important code for the classes (It's basically the same for both of them):
public class Player extends JPanel {
public Player(){
setPreferredSize(new Dimension(Main.frameWidth, Main.frameHeight));
setBackground(Color.BLACK);
new Timer(1000/60, new ActionListener(){
public void actionPerformed(ActionEvent e){
update();
repaint();
}
}).start();
}
//...
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(x, y, width, height);
}
}
(Of coure I left things like #Override or unneseccary functions out to shorten the code)
This code only paints the Player, altough I want it to display all the components of mainPanel.
This is an example that you can (hopefully) run. I had to split it up into 3 files, since I suck at anonymus classes:
Main.java
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
JPanel mainPanel = new JPanel();
mainPanel.add(new Player());
mainPanel.add(new Ball());
frame.add(mainPanel);
frame.setVisible(true);
}
}
///////
Player.java
//////
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Player extends JPanel{
private int x = 20, y = 300, width = 20, height = 100;
public Player(){
setPreferredSize(new Dimension(800, 800));
setBackground(Color.BLACK);
new Timer(1000/60, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
repaint();
}
}).start();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(x, y, width, height);
}
}
//////
Ball.java
//////
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ball extends JPanel{
private int x = 20, y = 300, width = 20, height = 100;
public Ball(){
setPreferredSize(new Dimension(800, 800));
setBackground(Color.BLACK);
new Timer(1000/60, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
repaint();
}
}).start();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(x, y, width, height);
}
}
In method paintComponent() of class Player, you paint the exact same Rectangle each time. In order to achieve animation, each time you paint the rectangle you need to change either its size or location or both. What do you expect the Player to do? Should it move up and down along the left edge of the window? Have you seen the lesson entitled How to Use Swing Timers which is part of Oracle's Java Tutorial?
EDIT
I see now that Player hides Ball, because of the default layout manager of JPanel. The below code is essentially the code you posted but I set GridLayout as the layout manager for mainPanel. Also, a java source code file may contain more than one class but exactly one class must be public. Hence in the below code only class Main is public.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(0, 2));
mainPanel.add(new Player());
mainPanel.add(new Ball());
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
class Player extends JPanel {
public Player() {
setBorder(BorderFactory.createLineBorder(Color.RED, 2, false));
setPreferredSize(new Dimension(800, 800));
setBackground(Color.BLACK);
new Timer(1000 / 60, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}).start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(20, 100, 20, 100);
}
}
class Ball extends JPanel {
public Ball() {
setBorder(BorderFactory.createLineBorder(Color.CYAN, 2, false));
setPreferredSize(new Dimension(800, 800));
setBackground(Color.BLACK);
new Timer(1000 / 60, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}).start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(300, 300, 10, 10);
}
}
And here is a screen capture of the GUI...
I just realized that if I extend my window manually, the second JPanel shows up under the one responsible for the Player! This means that I'll need to set the Panels position somehow, right?
#Abra

I have no idea what I'm doing - Paint Methods, etc

I have been struggling with Paint methods, and paint components, and extends JFrame, and have been trying all sorts of ways to get a simple rectangle to draw. here is a class called Window:
import javax.swing.*;
import java.awt.Graphics;
public class Window extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
Window()
{
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
g.drawRect(300,300,300,300);
}
}
then class Main
public class Main {
public static void main(String args[])
{
Window mainWindow = new Window();
mainWindow.setBounds(100,100,300,300);
}
}
The sole purpose of this program is just draw a damn rectangle. I have no idea what I could be doing wrong, and I have been trying to drawRect or drawString for several days now, to no avail. I also tried with a panel.
Don't override paint of top level containers like JFrame, this is the quickest way to end up with a world of weird and unexpected results.
Between the frames actual surface and the use, there is a JRootPane, a contentPane and possibly a glassPane...
All of these can interface/erase what you've painted in the paint method.
Instead, start with a JPanel and override it's paintComponent. Create an instance of this and place it on an instance of a JFrame when you want to show it.
Have a look at Painting in AWT and Swing and Performing Custom Painting for more details
Also, beware that when painting, 0x0 is the top/left of your component, so in your example, you start painting at 300x300, but your frame is only 300x300, so you're actually painting of the screen
As an example:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestPaint {
public static void main(String[] args) {
new TestPaint();
}
public TestPaint() {
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("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
public PaintPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
g2d.dispose();
}
}
}
could you try this code
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(400, 400);
f.add(new Main());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
g.fillRect (5, 15, 50, 75);
}
}

Why is graphics not showing on my JFrame / JPanel?

When I run this code. The result is a window with the name. Fully blank. I tried editing background color and adding graphics (rectangle) etc but the same result keeps occurring .
Question: This ends up as a white screen on a window. No graphics or background color. Even though I added it to panel and added panel. How do I fix this?
Main.java
package ball.tec.main;
import javax.swing.JFrame;
import ball.tec.frame.Frame;
public class Main {
public static void main(String[] args) {
String Version = "0.1.2";
Frame f = new Frame();
f.setVisible(true);
f.add();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setSize(1500, 1000);
f.setTitle("RedBall V: " + Version);
}
}
Frame.java
package ball.tec.frame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame{
private static final long serialVersionUID = 1L;
public boolean debug = false;
//Creating panel object
JPanel panel = new JPanel();
//Graphics displayed
public void paintComponent(Graphics g) {
//Firstly Nothing pops up
g.setColor(Color.RED);
g.drawRect(20, 40, 10, 10);
//And this doesn't work.
this.setBackground(Color.RED);
}
//Add everything to 'panel'
public void add() {
add(panel);
//Even if I put it here it doesn't work ;-;
this.setBackground(Color.RED);
this.pack();
}
}
You don't use paintComponent() in a JFrame. What you probably intended to do was create a third class, extending JPanel. Add your paintComponent() there.
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(20, 40, 10, 10);
this.setBackground(Color.RED);
}
}
f.add(new MyPanel());

How to update java canvas dynamically?

Problem: I am trying to update the canvas with new painting objects based on user action. The canvas dosent get updated.
What i have done: The user interacts with the DnD action,The transferrable object reaches the canvas, Calls an update graphics method created by me. And the method simply uses the aldready created graphics 2d object and draws images using it.I have checkd the DnD action,the object is properly recived at canvas class and i was able to print them out using System.out.println.
A sample code,that has a similar function to that of mine,
Paint class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class PaintPanel extends JPanel{
private Graphics2D drawImage;
public PaintPanel()
{
}
#Override
public void paint(Graphics g) {
drawImage = (Graphics2D) g;
drawImage.setColor(Color.WHITE);
drawImage.fillRect(0, 0, getWidth(), getHeight());
}
public void updateGraphics(int length,int width)
{
drawImage.setColor(Color.black);
drawImage.drawRect(100, 150, length, width);
repaint();
}
}
mainframe class:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainPaint extends JFrame{
public MainPaint()
{
setTitle("test paint");
setSize(400,400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan,BorderLayout.CENTER);
add(testButon,BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50,50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MainPaint();
}
}
Graphics2D drawImage; //source of the problem!
Don't attempt to cache a Graphics (or Graphics2D) instance! Instead:
Add the new objects to a list
Call repaint().
In paintComponent(Graphics) draw the list of objects.
An alternative to that is to use a BufferedImage as the drawing object. See this answer for an example.
Update - SSCCE based on latest code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPaint extends JFrame {
public MainPaint() {
setTitle("test paint");
setSize(400, 400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan, BorderLayout.CENTER);
add(testButon, BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50, 50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args) {
new MainPaint();
}
}
class PaintPanel extends JPanel {
private int x, y;
private Color color = null;
public PaintPanel() {
setBackground(Color.ORANGE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D drawImage = (Graphics2D) g;
if (color != null) {
drawImage.setColor(color);
drawImage.drawRect(100, 150, x, y);
}
}
public void updateGraphics(int length, int width) {
color = Color.RED;
x = length;
y = width;
repaint();
}
}
Note
There are still a number of things about that code that need changing. I decided to stop at the earliest variant that worked to display the rectangle on button click.
I think you need to call the validate() method.

Categories