Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Initializing a component in an application.

Posted on 1997-04-06
1
Medium Priority
?
195 Views
Last Modified: 2010-04-16
I am trying to draw graphics in a frame.  I tried using:
Component drawInto = new Canvas();
Graphics g = drawInto.getGraphics();
g.drawline(X1,Y1,X2,Y2);
There is more code obviously, but since this is an application and not an applet, I don't know how to initialize the component.  The frame comes up, but I don't get a Canvas or a line. If I try paint(Graphics g), the program compiles correctly, but I get a nullpointer error when I try to execute the program.  Any answers?
0
Comment
Question by:FRAYSER
1 Comment
 
LVL 2

Accepted Solution

by:
majkl earned 100 total points
ID: 1219450
Well. The bast way to do graphics is to extend Canvas component :
class MyCanvas extends Canvas
{
//put code for drawing in here
 
}
declare there methods to draw .....
draw in function paint(Graphics g) or update

 public void update (Graphics g)
     {
        paint (g);
     }

to avoid flickering.

Put canvas in frame like this
MyCanvas canvas = new Mycanvas();
canvas.resize(x,y); //x,y wanted size
canvas.move(xx,yy); xx,yy wanted position
//call draw methods
canvas.draw_shape();


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question