This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes East JPanel and graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "East JPanel and graphics" Watch "East JPanel and graphics" New topic
Author

East JPanel and graphics

Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
I am trying to add a 500x 500 'fill3DRect' on an East JPanel within a JFrame. I can see part of the rectangle, but most of it is clipped on the right side of the window. If I put the graphic in the West JPanel, the whole thing shows. Can anyone tell me how to get the 500x500 graphic showing in the East JPanel?

Here are the classes I am working on:



Thanks in advance,

Adam

EDIT by mw: Added Code Tags.
[ December 12, 2005: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Moving to the Swing forum...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

Avoid changing the state of a component in its paint methods. In your case,
I think changing the preferred size is what you want. And to have the frame "try"
to give components their preferred size, use pack().

BTW, if you are using the current version of Java, you could write the preferred size method as:


There is no emoticon for what I am feeling!
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
Thanks! I'll have to try that out. Some of the commands you use are new to me. I am still learning. I just finished a class on Java and found it very interesting.

Explain your statement:
Avoid changing the state of a component in its paint methods.


Are you referring to changing the size? Why does it matter?

I am just trying to learn more about it....
Keep in mind that I am still in the new learning phase of Java so please be easy on me
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
Can you explain what this does?
-----------------------------------------------------------

public Dimension getPreferredSize()
{
return new Dimension(500, 500);
}
-----------------------------------------------------------

That by itself did it. What exactly is this doing? The method is never called from the main class....
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
Also, regarding your statement of not changing components within the paint method, is it bad practice to perform alot of calculations in it? I need to plot a series of drawlines within a loop in order to plot on the graph. Each loop iteration will require several steps of calculations.

Is there a better way to perform all the calculations in another class or something?
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
Here is my latest code simply drawing a parabola - for testing.

If I wanted to show the parabola slowly being drawn, I need to put a pause in the for loop drawing it. However, when putting the pause in with the following, it hangs. This is a modification of my graphing program. I was going to try to create a ballistics graph with this one:

try
{
Thread.sleep (100);
}
catch (InterruptedException error)
{
//nothing to enter here
}

**********************************************
Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BallisticTravel extends JFrame //implements ActionListener
{
JPanel inputJPanel, displayJPanel;

public BallisticTravel()
{
super.setTitle("Graphing Calculator");

inputJPanel = new JPanel();

BallisticGraph graphPanel;
graphPanel = new BallisticGraph(true);
graphPanel.setSize(500, 500);
graphPanel.setVisible(true);
add(graphPanel, "West");

}


public static void main(String args[])
{
BallisticTravel main_window = new BallisticTravel();
main_window.setSize(700, 600);
main_window.setVisible(true);

//terminates application when JFrame is closed.
main_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}



*******************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BallisticGraph extends JPanel
{
public boolean setupWindow;
private long xScale, yScale, xMax, yMax, xAxisGridPlotFactor, yAxisGridPlotFactor;
private long xAxisGridPlot, yAxisGridPlot, plot;
private int x, y, xBegin = 0, yBegin = 0, xEnd = 0, yEnd = 0;

public BallisticGraph(boolean setupWindow)
{
this.setupWindow = true;
}

public Dimension getPreferredSize()
{
return new Dimension(500, 500);
}

public void paintComponent(Graphics g1d)
{
Graphics2D g = (Graphics2D)g1d;
super.paintComponent(g);


if (setupWindow == true)
{

Color gray51 = new Color(130,130,130);
g.setColor(gray51);
g.fill3DRect(0,0,500,500, false);

System.out.println("setup window");


xScale = 1;
yScale = 1;
xMax = 10;
yMax = 10;

xAxisGridPlotFactor = xMax/xScale;
yAxisGridPlotFactor = yMax/yScale;

xAxisGridPlot = 250/xAxisGridPlotFactor;
yAxisGridPlot = 250/yAxisGridPlotFactor;

Color black = new Color(0,0,0);
g.setColor(black);
//g.fill3DRect(245,245,10,10, false);


//draw X grid lines
plot = 250;
for (x = 250; x >= 0; x -= xAxisGridPlot)
{
g.drawLine((int)Math.round(plot),0, (int)Math.round(plot), 500);
plot -= xAxisGridPlot;
System.out.println(x);
}

plot = 250;
for (x = 250; x <= 500; x += xAxisGridPlot)
{
g.drawLine((int)Math.round(plot),0, (int)Math.round(plot), 500);
plot += xAxisGridPlot;
System.out.println(x);
}

//draw Y grid lines
plot = 250;
for (x = 250; x >= 0; x -= yAxisGridPlot)
{
g.drawLine(0,(int)Math.round(plot),500,(int)Math.round(plot));
plot -= yAxisGridPlot;
System.out.println(x);
}

plot = 250;
for (x = 250; x <= 500; x += yAxisGridPlot)
{
g.drawLine(0,(int)Math.round(plot),500,(int)Math.round(plot));
plot += yAxisGridPlot;
System.out.println(x);
}

//draw parabola
for (x = 0; x <= 250; x++)
{
yBegin = x*x/60;
xBegin = x;


/*try
{
Thread.sleep (100);
}
catch (InterruptedException error)
{
//nothing to enter here
}
*/

g.drawLine(xBegin+250, 250-yBegin, xEnd+250, 250-yEnd);
g.drawLine(250-xBegin, 250-yBegin, 250-xEnd, 250-yEnd);

yEnd = yBegin;
xEnd = xBegin;


}
}
}
}
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Adam Wilkins:

Explain your statement:
Avoid changing the state of a component in its paint methods.

Are you referring to changing the size? Why does it matter?


Yes, I was referring to your changing the size, but my comment is general. You should avoid changing the state of the comment in the paint methods because you have little control over how or when they are called: they are often called multiple times in a row to repaint sections of the component. And more to the point, there are often better way to solve what you are trying to do.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Adam Wilkins:
Can you explain what this does?
-----------------------------------------------------------
public Dimension getPreferredSize() {return new Dimension(500, 500);}
-----------------------------------------------------------
That by itself did it. What exactly is this doing? The method is never called from the main class....


Well, you never called the paintComponent method either, eh? Swing is
a framework, implying that you sometimes derive from certain
classes and override some of their methods to modify their behaviour,
but then *never* call those methods directly. That's because they are
called by code elsewhere in the framework. This is one of the challenges
to using a framework -- knowing what little cog on the big machine to tweak,
but the alternative is to reinvent the machine! In this particular case,
Component has a trio of methods: getMinimumSize, getPreferredSize and
getMaximumSize, which may of may not be called by containers' layout
managers in the process of laying out your GUI. In our case,
calling JFrame's pack method triggers a cascade of calls that includes
querying our component about its preferred size. But don't take my word
for it, place System.out.println's before and after the call to pack and
in the getPreferredSize method. You'll also see it's called when you
use the mouse to stretch or shrink the window.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Adam Wilkins:
Also, regarding your statement of not changing components within the paint method, is it bad practice to perform alot of calculations in it? I need to plot a series of drawlines within a loop in order to plot on the graph. Each loop iteration will require several steps of calculations.

Is there a better way to perform all the calculations in another class or something?

It's not necessary, if they're associated with painting at least. And *when* you do the
calculations may be more important than *where* (in what method) you place
the code. I would write the code as simply as possibly. If the performance
and behaviour are accpetable, you're done!

On the other hand it's often a good idea to avoid *redoing* calculations
and avoiding doing anything too time consuming in the EDT (event dispatch
thread -- what is doing painting and GUI event handling). One thing
you might try is adding a boolean flag that indicates when you need
to recalculate. In your paintComponent method you could calculate only
if that flag indicates you need to, then store the results and render.
Later calls that find the flag indicates there is no need to recalculate
can simply redo the rendering.

When doing a lot of number crunching, you may want to put that in a
non-GUI thread if that is making the GUI slow or jumpy, but that can
be a tricky thing to do if you haven't worked with threads before.
[ December 13, 2005: Message edited by: Jeff Albrechtsen ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I haven't stared too hard at your latest code (almost time for me to get to
work!) but I have a tip for doing simple animation, which is
what you are doing in your component. Don't put sleep in a paint method!
Painting is usually double-buffered, meaning that your code is rendering
to an off-screen buffer. When that is complete, the buffer is copyied
to your monitor in a single step. This is done to avoid flashing during
repainting, but it means that sleeping during painting only slows things
down, but doesn't cause intermediate results to appear. Instead check out
javax.swing.Timer.

In your listener's actionPerformed method you can increment the state of your
component and call its repaint method. The repeated repaints will result
in animation.

Good Luck!
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
Jeff, I salute you!!! You have been a tremendous help here. I am learning things from you I didn't learn in class.

Thank you so much!!!

I might not have time to mess with these programs for a couple of days, but when I do, I will definitely re-read everything you have stated here. 80% of it makes sense to me at this point. I will definitely be back

Thanks again!!
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Trying to extend JPanel or JFrame so i can have a Jpeg picture on the Frame or Panel so i can add that and than have some graffics objects overtop the panel or frame.
any ideas.
roba
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: East JPanel and graphics
 
Similar Threads
Using GradientPaint and Swing Components
Line chart
Having touble adding graphics to JPanel
Problem with the paint method
Graphics 2d Issue