Solved

i need help with this

Posted on 2004-10-08
19
200 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 104
Error trying to install RTMT Win7 5 59
object oriented programming comparison 5 69
configure dependency in POM for new database 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now