Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2004

Tip: Looking for answers? Try searching our database.

Drawing

Thread view: 
CR - 28 Nov 2004 09:52 GMT
I have a little problem with drawing in a JPanel. What I want to be able to
do is the following:

Setup an extended JPanel with the appropiate properties and then add it to
my frame and make visible (no problem here). Next I want to be able to call
a method of the extended JPanel from outside the panel that will take two
arrays as parameters and draw them using drawPolyline.

I can't seem to figure out how to do this. So far I have only been able to
draw to to a panel by adding to the paintComponent method, but this isn't
very flexible when I would like for the contents of the panel to change by
the press of a JButton for instance.

Hope you can help.
Andrew Thompson - 28 Nov 2004 10:46 GMT
> Setup an extended JPanel with the appropiate properties and then add it to
> my frame and make visible (no problem here). Next I want to be able to call
> a method of the extended JPanel from outside the panel that will take two
> arrays as parameters and draw them using drawPolyline.
>
> I can't seem to figure out how to do this.

int[][] shape1;
int[][] shape2;

public void setShapes(int[][] shape1, int[][]shape2) {
 this.shape1 = shape1;
 this.shape2 = shape2;
}

public void paintComponent() {
 if (shape1!=null) {
   g.drawPolyline(shape1[0], shape1[1], shape1.length);
 }
 // ....
}

> Hope you can help.

Plesaew note that questions of this nature are best dealt
with on a different group.
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

CR - 28 Nov 2004 11:52 GMT
Thanks for you reply. I tried out your suggestion but I seem to be running
into the same problem. I cant call the method within the panel from the
outside by using primaryPanel.setShapes(parameters);.

> Plesaew note that questions of this nature are best dealt
> with on a different group.
> <http://www.physci.org/codes/javafaq.jsp#cljh>

I am sorry if this is the wrong group. It just seems to be the most relevant
since it is dealing with swing components.
VisionSet - 28 Nov 2004 11:58 GMT
> Thanks for you reply. I tried out your suggestion but I seem to be running
> into the same problem. I cant call the method within the panel from the
> outside by using primaryPanel.setShapes(parameters);.

Why not, it is a public method?

Please provide your calling code.

--
Mike W
CR - 28 Nov 2004 12:21 GMT
Okay the code is below. I have a ProgramSetup class and a DrawPanel class.
The ProgramSetup class has two methods - a makeGUI() and a calculate(). The
makeGUI() just initializes the JFrame and DrawPanel. The calculate class
puts some x coordinates into an int[] xvalues and some y coordinates into an
int[] yvalues. xvalues and yvalues are then passed to the setShapes method
of the DrawPanel. This is where it stalls. It simply won't compile as it
says "Cannot resolve symbol". I think its a problem with inheritance as the
compiler is looking in the super class JPanel, but I am not sure. Take a
look and tell me what you think.

import javax.swing.*;
import java.util.*;

public class ProgramSetup
{

   private double temp;
   private Random random;
   private int[] yvalues;
   private int[] xvalues;
   private int times = 750;
   private JPanel primaryPanel;

   public ProgramSetup()
   {
       makeGUI();
   }

   public void calculate()
   {
       random = new Random();
       xvalues = new int[times];
       yvalues = new int[times];

       temp = 0;

       for(int j = 0; j != times; j++)
       {
           xvalues[j] = j;
       }

       for(int j = 0; j != times; j++)
       {

               yvalues[j] = (int) (temp - 1 - 5 * random.nextGaussian());
               temp = yvalues[j];

       }

       primaryPanel.setShapes(xvalues, yvalues);

   }

   public void makeGUI()
   {
       JFrame frame = new JFrame("Graph");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       primaryPanel = new DrawPanel();

       frame.getContentPane().add(primaryPanel);
       frame.pack();
       frame.setVisible(true);
   }

}

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

public class DrawPanel extends JPanel
{

   private int[] xvalues;
   private int[] yvalues;
   private int times;

   public DrawPanel()
   {

       setup();
   }

   public void setShapes(int[] xvalues, int[] yvalues)
   {
       this.xvalues = xvalues;
       this.yvalues = yvalues;
   }

   public void setup()
   {

       setPreferredSize(new Dimension(800,450));
       setBackground(Color.red);
   }

   public void paintComponent(Graphics e)
   {
       super.paintComponent(e);

       e.translate(50,400);

       e.setColor(Color.white);

       if (xvalues!=null)
       {
           e.drawPolyline(xvalues, yvalues, xvalues.length);
       }

   }

}

>> Thanks for you reply. I tried out your suggestion but I seem to be
>> running
[quoted text clipped - 7 lines]
> --
> Mike W
Andrew Thompson - 28 Nov 2004 12:23 GMT
>> Plesaew note that questions of this nature are best dealt
>> with on a different group.
>> <http://www.physci.org/codes/javafaq.jsp#cljh>
>
> I am sorry if this is the wrong group. It just seems to be the most relevant
> since it is dealing with swing components.

The other group is better for people who are just starting in
Java, and (one would hope that) anyone who is unable to do
something this straightforward is just starting in Java.

Why don't you supply an SSCCE* of what your doing.
* <http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

CR - 28 Nov 2004 12:48 GMT
> The other group is better for people who are just starting in
> Java, and (one would hope that) anyone who is unable to do
> something this straightforward is just starting in Java.

I haven't been programming in JAVA for more than 3 months so you are
assuming right. So I will keep future questions in the java.help group.

> Why don't you supply an SSCCE* of what your doing.

Look at my response to VisionSet. I feel its an SSCCE besides the fact that
it wont compile. But thats the problem I am having.
Andrew Thompson - 28 Nov 2004 14:12 GMT
>> The other group is better for people who are just starting in
>> Java, and (one would hope that) anyone who is unable to do
>> something this straightforward is just starting in Java.
>
> I haven't been programming in JAVA

Technically, it is Java (a name, rather than a capitalised acronym).

>.. for more than 3 months so you are
> assuming right. So I will keep future questions in the java.help group.

That would probably be best until you get the hang of the groups,
though you seem to be doing that rather quickly.

>> Why don't you supply an SSCCE* of what your doing.
>
> Look at my response to VisionSet. I feel its an SSCCE

Close.  Note that you could move all the imports to the top,
demote the second class to 'default' (see code) and compile it
all in the same file.  

>..besides the fact that
> it wont compile. But thats the problem I am having.

Look closely at the comments I put in, they explain further..
<sscce>
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class ProgramSetup
{

   private double temp;
   private Random random;
   private int[] yvalues;
   private int[] xvalues;

   /** For sticking with my original advice, I'll add two
   arrays of arrays. */
   private int[][] shape1;
   private int[][] shape2;

   private int times = 750;

   /** this is actually a sub-class of JPanel. If you declare
   it as an JPanel, Java will assume that only JPanel methods
   are present, rather than the extra ones you defined for
   setShapes() etcetera. */
   // private JPanel primaryPanel;
   private DrawPanel primaryPanel;

   public ProgramSetup()
   {
       makeGUI();
   }

   public void calculate()
   {
       random = new Random();
       xvalues = new int[times];
       yvalues = new int[times];

       shape1 = new int[2][];
       shape1[0] = xvalues;
       shape1[1] = yvalues;

       shape2 = new int[2][];
       shape2[0] = xvalues;
       shape2[1] = yvalues;

       temp = 0;

       for(int j = 0; j != times; j++)
       {
           xvalues[j] = j;
       }

       for(int j = 0; j != times; j++)
       {
               yvalues[j] = (int) (temp - 1 - 5 * random.nextGaussian());
               temp = yvalues[j];

       }

       primaryPanel.setShapes(shape1, shape2);
   }

   public void makeGUI()
   {
       JFrame frame = new JFrame("Graph");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       primaryPanel = new DrawPanel();

       frame.getContentPane().add(primaryPanel);
       frame.pack();
       frame.setVisible(true);
   }
}

class DrawPanel extends JPanel
{
   private int[] xvalues;
   private int[] yvalues;
   private int times;

   int[][] shape1, shape2;

   public DrawPanel()
   {
       setup();
   }

   /** Note that this method currently accepts two arrays of
   arrays (often referred to as multi-dimensional arrays).
   It might make more sense to hand two single arrays of
   Point objects.  Each Point object possesses an x and y
   attribute. Though that is another story. */
   public void setShapes(int[][] shape1, int[][] shape2)
   {
       this.shape1 = shape1;
       this.shape2 = shape2;
   }

   public void setup()
   {
       setPreferredSize(new Dimension(800,450));
       setBackground(Color.red);
   }

   public void paintComponent(Graphics e)
   {
       super.paintComponent(e);

       e.translate(50,400);

       e.setColor(Color.white);    

       if (xvalues!=null)
       {
           e.drawPolyline(xvalues, yvalues, xvalues.length);
       }
   }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

CR - 28 Nov 2004 22:59 GMT
Thanks a lot. Your comments were very useful and I finally got it working.
Andrew Thompson - 29 Nov 2004 04:33 GMT
> Thanks a lot. Your comments were very useful and I finally got it working.

You're welcome.  Glad you got it sorted.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.