Forums Register Login

Adding Graphics to a Canvas

+Pie Number of slices to send: Send
Why is there no red rectangle showing up when I run this program? I am using a JFrame and a Canvas. Here is my source code:

package javagame;

public class Launcher {

public static void main(String[] args) {
Frame d = new Frame();

}
}

=============================================================

package javagame;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Frame {

public JFrame frame;
public Canvas canvas;
public Graphics g;

public Frame() {
createDisplay();
}

public void createDisplay() {
frame = new JFrame("Title");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

canvas = new Canvas();
canvas.setPreferredSize(new Dimension(300, 300));

frame.add(canvas);

g = canvas.getGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 300, 300);

}

}
+Pie Number of slices to send: Send
It probably is, but it is doing it in less than a millisecond and then getting rewritten with a blank. Canvas is an AWT component and JFrame is a Swing component, light weight component, and as such it is recommended not to mix Heavy weight, AWT, and light weight, Swing, components. In Swing you would use a JPanel and override the paintComponents Method:

So you would get something like this:
+Pie Number of slices to send: Send
You should override the paintComponent(...) method (without the "s") of the JPanel.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.
+Pie Number of slices to send: Send
Thanks for the reply! But, it is still not working. I am still not able to paint graphics on the screen. I only get a blank window which I believe is just my JFrame. Here is my source code:

+Pie Number of slices to send: Send
Wait never mind, I just saw your reply about dropping the "s" off of the components. Now it is working. Could you please tell me why that works?
+Pie Number of slices to send: Send
Good catch... my brain did that type of thing on several posts this morning. Seems it's already ready for the Christmas holiday!
+Pie Number of slices to send: Send
 

Could you please tell me why that works?



I provided you with a link to learn the basics of custom painting.

Read the tutorial!!!
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 703 times.
Similar Threads
Error with Timer.start();
Why drawString method cannot be print out?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but instead of JFrame I want Frame.
urgent help needed
Help with a "common" error.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:51:31.