473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

passing parameters to paint(graphics)

I think my question ultimately stems from the fact that I don't fully understand classes: how they work together, when to make them, etc.

So I have a class called Creature. These guys have properties that will determine how they are drawn, but right now they just contain an (x,y) for where they are to be drawn. I would just like to draw a circle where each is located. Easy enough, I suppose, but I'm not getting it.

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.         Creature dude = new Creature(20, 40);
  7.         MakeWorld world = new MakeWorld();
  8.     }
  9. }
and:

Expand|Select|Wrap|Line Numbers
  1.     import java.awt.*;
  2.     import javax.swing.*;
  3.  
  4. public class MakeWorld {
  5.  
  6.      MakeWorld(){
  7.  
  8.         JFrame frame = new JFrame("title");
  9.  
  10.         frame.getContentPane().add(new MyComponent());
  11.         frame.getContentPane().setBackground(Color.WHITE);
  12.  
  13.         frame.setSize(Globals.FRAME_WIDTH, Globals.FRAME_HEIGHT);
  14.         frame.setVisible(true);
  15.  
  16.     }
  17.  
  18.  
  19.     class MyComponent extends JComponent
  20.     {
  21.         @Override
  22.         public void paint(Graphics g)
  23.         {
  24.  
  25.            Graphics2D g2d = (Graphics2D)g;
  26.  
  27.             g2d.fillOval(10, 20, 2, 2);
  28.  
  29.         }
  30.     }
So, I am able to draw an oval. How could I make it work so I could do something like:

g2d.fillOval(dude.x, dude.y, 2, 2)?

It doesn't seem like I can add any parameters to paint(); so I can't even reference "dude".

Is there a way I could make a function like:

dude.draw() that I could call outside of the paint() function?

Thanks,

bd
Feb 16 '09 #1
1 6902
JosAH
11,448 Expert 8TB
Why don't you pass a Graphics(2D) object to your Dudes and let them draw on the Graphics object.

kind regards,

Jos
Feb 17 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: edward hage | last post by:
The following program is an applet that reads a number from a file 10 times per second. It needs to be shown graphically. The file monitors a procesvariable and changes continuously. It is read...
4
by: Evan Dekker | last post by:
hi all im not why this code doesn't work: ///////// import java.applet.*; import java.awt.*; public class TestApplet extends Applet { public void init() {
4
by: timwap | last post by:
I hava a Java Applet that produces a DTMF tone at a certain time of day. There are 4 parameters to the Applet, they are hour = Hour of triggered event 00~23 min = minute of triggered event 00~59...
4
by: Franck | last post by:
Hello, Sorry if that question has already been raised... I'm looking for the exact equivalent of Java paint(Graphics g) method in c# in order to paint a specific component and all its children...
7
by: Trollpower | last post by:
Hello NG, i need to know how i can pass parameters to the loginpage if i use authentication mode Forms. I need to pass different paramaters, such as a different redirection url, strings and...
4
by: Dafü | last post by:
Hello! I have an application that paint graphics in VB.NET. Each graphic is painted after a long calculation process. My problem is when another windows pass over my graphics or I resize the...
2
by: Carl Heller | last post by:
Working in VS2003, .Net 1.1 I'm working on a project where I compare data between two databases. This is a lengthy process, and very data intensive, so I decided to create a class, and thread...
11
by: cty0000 | last post by:
I have some quiestion... I want to draw line,point,rectangles and etc... on the from So I code like this.. public update() { g = this.CreateGraphics(); g.FillRectangle(Brushes.White, x1,...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.