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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

i need help with this

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
mijangos
Asked:
mijangos
  • 7
  • 5
  • 5
  • +1
1 Solution
 
sudhakar_koundinyaCommented:
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
 
mijangosAuthor Commented:
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
 
funnyveryfunnyCommented:
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
Technology Partners: 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!

 
mijangosAuthor Commented:
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
 
funnyveryfunnyCommented:
>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
 
mijangosAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
mijangosAuthor Commented:
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
 
VenabiliCommented:
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
 
sudhakar_koundinyaCommented:
:)
0
 
mijangosAuthor Commented:
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
 
sudhakar_koundinyaCommented:
that's better
0
 
mijangosAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
funnyveryfunnyCommented:
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
 
funnyveryfunnyCommented:
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
 
mijangosAuthor Commented:
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
 
funnyveryfunnyCommented:
:)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now