Solved

Randomly scaling fillOval

Posted on 2004-10-01
22
284 Views
Last Modified: 2013-11-22
I have:

page.fillOval(x-4, y-4, 8, 8);

in the API docs I read:

fillOval
public abstract void fillOval
                          (int x,
                           int y,
                           int width,
                           int height)Fills an oval bounded by the specified rectangle with the current color.

Parameters:
x - the x coordinate of the upper left corner of the oval to be filled.
y - the y coordinate of the upper left corner of the oval to be filled.
width - the width of the oval to be filled.
height - the height of the oval to be filled.

---------------
So how might I approach randomly scaling this?

Something like:
page.fillOval*(1 + (int)(100*Math.random()));  //to scale randomly from [1-100]?????
0
Comment
Question by:smkJackstraw
  • 12
  • 6
  • 4
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12205113
*Where* are you scaling it?
0
 
LVL 92

Expert Comment

by:objects
ID: 12205139
double scaling = Math.random();
page.fillOval(x, y, w*scaling, h*scaling);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12205148
Something like:


int width = getWidth();
int height = getHeight();
int scaledWidth = (int)(Math.random() * getWidth());
int scaledHeight = (int)(Math.random() * getHeight());

fillOval(width - scaledWidth, height - scaledHeigth, scaledWidth, scaledHeight);
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 12205153
>>int scaledWidth = (int)(Math.random() * getWidth());

should have been

int scaledWidth = (int)(Math.random() * width);

and same for height
0
 

Author Comment

by:smkJackstraw
ID: 12205165
So I can't scale the object as a whole, but rather need to scale the fillOval perameters?
0
 
LVL 92

Expert Comment

by:objects
ID: 12205166
if you want to centre the scaled oval then simply offset the x,y.

0
 
LVL 92

Expert Comment

by:objects
ID: 12205171
to scale the ovject as a whole you would apply a scaling transformation to the Graphics context
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12205175
What does 'the object' refer to?
0
 
LVL 92

Expert Comment

by:objects
ID: 12205176
g2d.scale(xscale, yscale);
g2d.fillOval(...
0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 12205180
public void paintComponent(Graphics g)
{
   Graphics2D g2d = (Graphics2D) g.create();
   g2d.scale(xscale, yscale);
   g2d.fillOval(x, y, w, h);
   g2d.dispose();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12205198
any other painting you also do with that context will then also be scaled
0
 

Author Comment

by:smkJackstraw
ID: 12205235
Is there a way to add points after people respond to questions?  You both deserve the points.  I can't help but laugh watching you guys/gals battle to deliver new insite into my learning.  Give me a few minutes to digest...lol!
0
 
LVL 92

Expert Comment

by:objects
ID: 12205240
sorry, 500 is the max allowed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12205255
500's quite enough for this type of question ;-)
0
 

Author Comment

by:smkJackstraw
ID: 12205440
Here's where I've landed:

private int x = 50, y = 50;
private boolean mouseHere = false;
private Color c = Color.red;
private int width = getWidth();
private int height = getHeight();
private int scaleRange = 100;
private int scaledWidth = ((1 + (int)(scaleRange*Math.random())) * width);
private int scaledHeight = ((1 + (int)(scaleRange*Math.random())) * height);
private int newX, newY;

//  Random scaling of fillOval at mouse location
//  rescale in range [1-100]
public void randomScale(MouseEvent event, Graphics newOval)
{
      newX = event.getX();
      newY = event.getY();
      newOval.fillOval(x - newX, y - newY, scaledWidth, scaledHeight);
}

// Definition of the mouse listener as an inner class
//Note:  E:\depaul\docs\api\java\awt\event\AWTEventListener.html
private class ClickListener implements MouseListener
{
      // Record the location of the mouse click event
      public void mouseClicked(MouseEvent event)
      {
            x = event.getX();
            y = event.getY();
            randomColor();
            //randomScale();
            repaint();
      }
}

Now this leads me to my previous question "How do I call one method from another method in the same class?"  which I'm slowly getting my head around.  The //randomScale(); give a compile error because I have parameters in the randomScale method.  How do I call this method?

I believe I can drop the x = event.getX(); and y = event.getY(); once I can call randomScale();.

Last is there a resourse online to study examples of these types of calls?
0
 
LVL 92

Expert Comment

by:objects
ID: 12205456
you need to do your painting in your paint() method as in your previous q
0
 
LVL 92

Expert Comment

by:objects
ID: 12205464
you also need to do some reorganising a little.

your mouse listener needs to update the dimensions of the oval you need to paint, and then call repaint()
the actual painting should be done in the paintComponent() method

see the code I posted in the previous question as it should be similkiar to that.
0
 
LVL 92

Expert Comment

by:objects
ID: 12205467
0
 

Author Comment

by:smkJackstraw
ID: 12206059
Where does the .dispose come form in the previous example?  I'll be working on this over the weekend.  Having a hard time with the organization and understanding what your saying.  Pts...forthcoming.
0
 
LVL 92

Expert Comment

by:objects
ID: 12206074
dispose() fress up the resources used by the graphics context.
As it created a new instance of the context (using create()) it is also responsible for releasing the resources once they are no longer needed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213028
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12213890
(:
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
Error in @AspectJ Based AOP with Spring 2 13
eclipse buid path vs tomcat lib path 10 23
throw exception 21 44
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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