• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Adding Graphics to a Canvas

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}

}
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic