Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

i need help with this

Posted on 2004-10-08
19
Medium Priority
?
210 Views
Last Modified: 2010-03-31
i dont know if i can put links in this website but just in case if am not allow , email me so i can send you this picture http://www.pumas.unam.mx/ which has to do with my question  , this is my question :

Rather than writing all of my code inside the paint() method, I want to write several methods, each one drawing a different part of this picture, and then call those methods from my paint() method.

how can i do this if you can explain to me .

by the way you can give me the code but instead i will apriciete a well given explanations thanks


0
Comment
Question by:mijangos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 5
  • +1
19 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12265274
This can be done some thing like this


public void paint(Graphics g)
{
           part1(g);
           part2(g);
}


public part1(Graphics g)
{
  //your part1 drawing code
}


public part2(Graphics g)
{
  //your part2 drawing code
}

0
 

Author Comment

by:mijangos
ID: 12268135
can you please write the code or is too much to ask cause i've been trying since the day i've posted this question and is not working .

mijangos
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12268247
The important here is this statement:

     Graphics g = this.getGraphics();

it allows you to call Graphics.g from anywhere, therefore you can define numerous methods and call them whenever they are needed which is not neccessarily in paint() itself. Imagine your drawing methods are residing in a canvas class and this canvas is added to a Frame with some buttons. Then whenever a button is pressed you want to draw something on the canvas, you have two options:

1) call canvas.repaint() if all your methods are called through paint() like  sudhakar_koundinya's code, or
2) within each of you defined method, you added the above statement then you can just call that method directly like
canvas.myPaint1(); note without the need of passing variable g.

You have to choose which option is the best for you.

Also, if your drawing is an animation type then you should use BufferedImage (known as double buffering) to avoid flickers.

That's all.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mijangos
ID: 12269128
thanks for reply my question the problem is that i have to do this project the way my teacher told me , and about the bufferedimage we havent cover that part yet  and as we are beginners he wants us to do exactly this :

 I have to Use the Java Graphics, Color and Font classes to create an applet that draws a self-portrait or picture
 
 And Rather than write all my code inside the paint() method, I need to write several methods, each one drawing a different part of the picture , and then call those methods from my paint() method.


0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12271498
>and about the bufferedimage we havent cover that part yet

BufferedImage is an optional not compulsory.


>And Rather than write all my code inside the paint() method, I need to write several methods

 sudhakar_koundinya's code demonstrated just that - This is option 1 from my earlier comment.


> I have to Use the Java Graphics, Color and Font classes to create an applet that draws a self-portrait or picture

paint() is using Graphics g to draw your graphics contexts onto the screen. Think of g as your brush, so for example you want to draw a blue circle, then your myPaint method contains these statements:

g.setColor(Color.blue);
g.fillOval(50,50,60,60);

now you want to write a line of text using Arial font then:

g.setFont(Font.decode("Arial"));
g.drawString("Hello world",20,20);


It's always good practice to read the JDK's library reference when you need to understand the use of a class.
0
 

Author Comment

by:mijangos
ID: 12280587
thank you guys but my code doesnt work please help me cause my assignment is due for tomorrow can you please write me the code or if i need to pay for this i will am stock with this homework and i dont want to lose this 3 points cause i have so far grade B and if i dont complete this assignments my grade will go down to C ,
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12280798
i Suggest you to  post ur question here http://www.rentacoder.com/

EE is not the right choice for doing homeworks. We can just help to solve problems, if you struck anywhere

Regards
Sudhakar
0
 

Author Comment

by:mijangos
ID: 12281120
well i guess you are rigth you wont do the code if i only pay 10 dlls a  month , thanks for the link i will check it out
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12281154
It have nothing to do with the money... This is just how this site works.

If you try to write this alone and if you just ask to assist you in this - whenever you are stuck with something - we all will. But from the beginning... - sorry - this is academical dishonesty and according to the rules is a no-no.

Venabili
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12281172
:)
0
 

Author Comment

by:mijangos
ID: 12281221
ok i understand i will re write my code and i'll  post it and then you can check it out and tell me where my mistakes are ,
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12281250
that's better
0
 

Author Comment

by:mijangos
ID: 12289620
this is my code by the way can i name my methods the way i want ? or it has to be a method that java needs to recongnize heres my code i made it short

import java.applet.*;
import java.awt.*;

public class SelfPortrait extends Applet
{
      
      public void paint (Graphics guicho)
             
{
      drawHead(guicho);
      
}      
      private void Mijangos (int n, Graphics guicho)
      {
      int      X=50,
            Y=50,
            XI=30,
            YI=10,
            
      }
      
}

and this is the errror message i have :

C:\Documents and Settings\Guicho\My Documents\My Files\HW05\SelfPortrait.java:19: <identifier> expected
            
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289696
I am not understand ing what exactly u r expecting

In method Mijangos,
You should declare variable like this

int     X=50,          Y=50,           XI=30,           YI=10;
0
 
LVL 4

Accepted Solution

by:
funnyveryfunny earned 2000 total points
ID: 12290765
An example code for you:

import java.awt.*;
import java.applet.*;

public class Portrait extends Applet{

 public void paint(Graphics g){
    drawHead(g);
    drawText(g);
  }
 
  public void drawHead(Graphics g){
    g.fillOval(100,100,100,100);
    g.setColor(Color.white);
    g.drawArc(125,155,50,30,0,180);
    g.fillOval(125,120,10,10);
    g.fillOval(165,120,10,10);
  }
 
  public void drawText(Graphics g){
    g.setFont(new Font("Arial",Font.BOLD,20));
    g.setColor(Color.BLUE);
    g.drawString("Read, digest then ask!",200,200);
  }
}

Compile and run it. Set applet size 400x400 in your HTML
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12291690
I hope you do understand what is going on in my code, it meant to be joke with some guidance! I hope you're not using this as your homework, it'd be a self-inflicted wound to your own studying development. : )
0
 

Author Comment

by:mijangos
ID: 12291763
no i wont use it as my homework am trying to do that picture i posted , but you gave me an idea on how to do it :)
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12291802
:)
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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

715 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