• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Graphics to a Canvas

 
Jonathon Robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

}

}
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jonathon Robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Jonathon Robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch... my brain did that type of thing on several posts this morning. Seems it's already ready for the Christmas holiday!
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please tell me why that works?


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

Read the tutorial!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!