Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008, 01:05 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
Two problem for my rssi calculation program
I have downloaded a program, which can calculate rssi signal on a grid sheet.

1. I want to build a button, when I press the button, it pop up another window which show a graph on the data the program just calculated.

Can this be done ?

2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?


Here is my codes in the attachment.
Attached Files
File Type: zip Simulation_tool.zip (45.7 KB, 3 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 05:38 PM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
1. I want to build a button, when I press the button, it pop up another window which show a graph on the data the program just calculated.

Can this be done ?
Yes. You can find out about buttons, listeners and dialogs here: Trail: Creating a GUI with JFC/Swing. For the grid and plotting data use a JPanel, override the/its paintComponent method and do your custom drawing in it. For more about drawing see the last link (Performing Custom Painting) on the page (link above) and also the first link under the Other UI-Related Trails header, viz, "2D Graphics".

2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?

Yes. Draw the background image first and then draw your grid.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 07:56 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
Quote:
Originally Posted by hardwired View Post
Yes. You can find out about buttons, listeners and dialogs here: Trail: Creating a GUI with JFC/Swing. For the grid and plotting data use a JPanel, override the/its paintComponent method and do your custom drawing in it. For more about drawing see the last link (Performing Custom Painting) on the page (link above) and also the first link under the Other UI-Related Trails header, viz, "2D Graphics".

Yes. Draw the background image first and then draw your grid.
I want that graph to pop up in another window though


How do I draw that background image into the program ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 08:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
I want that graph to pop up in another window though
Launch a JDialog and add the graphic component (JPanel) to it.
Somewhere in_or_called_from_your_event_code:
Code:
GraphicPanel graphicPanel = new GraphicPanel(image, data); JDialog dialog = new JDialog(desired_args); dialog.add(graphicPanel); // configure dialog as desired dialog.setVisible(true);
How do I draw that background image into the program ?
Code:
class GraphicPanel extends JPanel { BufferedImage bgImage; int[][] data; GraphicPanel(BufferedImage image, int[] data) { bgImage = image; this.data = data; } protected void paintComponent(Graphics g) { super.paintComponent(g); int x = 10; int y = 10; g.drawImage(bgImage, x, y, this); // draw your grid lines... // draw your data points/lines as desired... } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 09:13 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
Oh there is still error, can you please have a look in the program ?
Sorry I am new to java hope someone can solve my problem here...

Q1. Here is where I want to plot graph.

Code:
jButton3.setBounds(new Rectangle(8, 5, 70, 22)); jButton3.setMargin(new Insets(0, 0, 0, 0)); jButton3.setText("Plot"); jButton3.addActionListener(new MainSim_jButton3_actionAdapter(this));
I build a button here, but,

Code:
class MainSim_jButton3_actionAdapter implements ActionListener { }
This class is still empty I do not know how to implement the plotting thing...


Q2. Here is where I want to insert the image.

Code:
public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); //Here it draw the white part. g.setColor(Color.gray); if (fpPoints != null && fpPoints.length > 0) { for (int i = 0; i < fpPoints.length; i++) { g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i); } for (int j = 0; j < fpPoints[0].length; j++) { g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight()); } } //And here it draw the gray lines. super.paint(g); String p = ""; Color cp = Color.black; java.util.Random r = new java.util.Random(10); if (highlight != null) { for (int i = 0; i < highlight.size(); i++) { FpPoint fpp1 = (FpPoint) highlight.get(i); if (!p.equalsIgnoreCase(fpp1.getFPRss())) { //g.setColor(new Color((int)(Math.random()*2)*255, (int)(Math.random()*2)*255, (int)(Math.random()*2)*255)); g.setColor(new Color( (int) (r.nextFloat() * 2) * 255, (int) (r.nextFloat() * 2) * 255, (int) (r.nextFloat() * 2) * 255)); if (g.getColor().equals(Color.white)) { g.setColor(Color.gray); } p = fpp1.getFPRss(); } //g.fillOval(MainSim.m2pixel(fpp1.getPointYm()) - 4, MainSim.m2pixel(fpp1.getPointXm()) - 4, 8, 8); g.fillOval(MainSim.m2pixel(fpp1.getPointYm()) - pix_grid / 2, MainSim.m2pixel(fpp1.getPointXm()) - pix_grid / 2, pix_grid, pix_grid); } } if ( apsdata != null ){ for ( int i = 0 ; i < apsdata.size() ; i ++){ ApsData ad = (ApsData) apsdata.get(i); g.setColor(Color.black); g.fillRect(MainSim.m2pixel(ad.getXm()), MainSim.m2pixel(ad.getYm()), 26, 16); g.setColor(Color.white); g.drawString(String.valueOf(ad.getRssiAvg()), MainSim.m2pixel(ad.getXm())+3, MainSim.m2pixel(ad.getYm()) + 13); } } }

Last edited by iamchoilan : 04-24-2008 at 09:15 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-25-2008, 01:58 AM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Random; import javax.swing.*; public class SimTest implements ActionListener { Random seed = new Random(); JDialog dialog; public void actionPerformed(ActionEvent e) { int[] data = getData(); DataPanel dataPanel = new DataPanel(data); if(dialog == null) { dialog = new JDialog(new Frame(), "data plot", false); dialog.setSize(400,400); dialog.setLocationRelativeTo(null); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } dialog.add(dataPanel); dialog.setVisible(true); } private int[] getData() { int[] data = new int[8]; for(int i = 0; i < data.length; i++) { data[i] = seed.nextInt(101); } return data; } private JPanel getContent() { JButton button = new JButton("test"); button.addActionListener(this); JPanel panel = new JPanel(new GridBagLayout()); panel.add(button, new GridBagConstraints()); return panel; } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new SimTest().getContent()); f.setSize(200,75); f.setLocation(200,200); f.setVisible(true); } } class DataPanel extends JPanel { int[] data; final int PAD = 20; public DataPanel(int[] data) { this.data = data; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw ordinate. g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // Draw abcissa. g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); double xInc = (double)(w - 2*PAD)/(data.length-1); double scale = (double)(h - 2*PAD)/getMax(); // Draw lines. g2.setPaint(Color.blue); for(int i = 0; i < data.length-1; i++) { double x1 = PAD + i*xInc; double y1 = h - PAD - scale*data[i]; double x2 = PAD + (i+1)*xInc; double y2 = h - PAD - scale*data[i+1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for(int i = 0; i < data.length; i++) { double x = PAD + i*xInc; double y = h - PAD - scale*data[i]; g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); } } private int getMax() { int max = -Integer.MAX_VALUE; for(int i = 0; i < data.length; i++) { if(data[i] > max) max = data[i]; } return max; } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-25-2008, 05:06 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
Thanks a lot, now I have to fit the data in it, where should the data go in the graph ?


And how do I implement the second question ?

Thanks.

Last edited by iamchoilan : 04-25-2008 at 06:07 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-25-2008, 06:23 PM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
Code:
jButton3.addActionListener(new SimTest(this));
From the looks of this line the error would likely say that the jvm cannot find a constructor in the SimTest class that takes an argument of the type referred to by the this keyword. If I try compiling
Code:
SimTest simTest = new SimTest("Hello World");
I get
Code:
C:\jexp>javac test.java test.java:6: cannot find symbol symbol : constructor SimTest(java.lang.String) location: class SimTest SimTest simTest = new SimTest("Hello World"); ^ 1 error
The jvm cannot find a SimTest constructor that takes a String argument.
The SimTest class (post #6) has no explicit constructor so the jvm provides a default, no–argument constructor.

And how do I implement the second question ?
From the original post #1:
2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?
or from #5
Q2. Here is where I want to insert the image.
Sorry, I forgot about the image.
Code:
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Random; import javax.imageio.ImageIO; import javax.swing.*; public class SimTest implements ActionListener { BufferedImage image; Random seed = new Random(); JDialog dialog; public SimTest(BufferedImage image) { this.image = image; } public void actionPerformed(ActionEvent e) { int[] data = getData(); DataPanel dataPanel = new DataPanel(data, image); if(dialog == null) { dialog = new JDialog(new Frame(), "data plot", false); dialog.setSize(400,400); dialog.setLocationRelativeTo(null); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } dialog.add(dataPanel); dialog.setVisible(true); } private int[] getData() { int[] data = new int[8]; for(int i = 0; i < data.length; i++) { data[i] = seed.nextInt(101); } return data; } private JPanel getContent() { JButton button = new JButton("test"); button.addActionListener(this); JPanel panel = new JPanel(new GridBagLayout()); panel.add(button, new GridBagConstraints()); return panel; } public static void main(String[] args) throws IOException { String path = "images/hawk.jpg"; BufferedImage image = ImageIO.read(new File(path)); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new SimTest(image).getContent()); f.setSize(200,75); f.setLocation(200,200); f.setVisible(true); } } class DataPanel extends JPanel { int[] data; BufferedImage image; final int PAD = 20; public DataPanel(int[] data, BufferedImage image) { this.data = data; this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw image centered. int x0 = (w - image.getWidth())/2; int y0 = (h - image.getHeight())/2; g2.drawImage(image, x0, y0, this); // Draw ordinate. g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // Draw abcissa. g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); double xInc = (double)(w - 2*PAD)/(data.length-1); double scale = (double)(h - 2*PAD)/getMax(); // Draw lines. g2.setPaint(Color.blue); for(int i = 0; i < data.length-1; i++) { double x1 = PAD + i*xInc; double y1 = h - PAD - scale*data[i]; double x2 = PAD + (i+1)*xInc; double y2 = h - PAD - scale*data[i+1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for(int i = 0; i < data.length; i++) { double x = PAD + i*xInc; double y = h - PAD - scale*data[i]; g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); } } private int getMax() { int max = -Integer.MAX_VALUE; for(int i = 0; i < data.length; i++) { if(data[i] > max) max = data[i]; } return max; } }
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-25-2008, 09:00 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
Thanks a lot, I think the first Question solved now when I click on the button it pop up a graph, but I need it to get my data and plot according to it.


But for Question 2, I think I did not make it clear enough, I want the image behind the grid of my program, but not the graph...

About the grid of my program, here is it.

Code:
public void paint(Graphics g) { g.setColor(Color.white); //Here it draw the white background sheet of it, and I want to change this to an image. g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.gray); //Here it put in the gray grids. if (fpPoints != null && fpPoints.length > 0) { for (int i = 0; i < fpPoints.length; i++) { g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i); } for (int j = 0; j < fpPoints[0].length; j++) { g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight()); } }

I don't know if this is clear enough, sorry that I am pretty new to java. I attached the whole thing I am working on here, please have a look.

Thanks a lot.
Attached Files
File Type: zip Simulation_tool.zip (79.2 KB, 0 views)
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-25-2008, 11:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
Code:
public void paint(Graphics g) { //Here it draw the white background, // and I want to change this to an image. // g.setColor(Color.white); // g.fillRect(0, 0, getWidth(), getHeight()); // Okay, then draw the image at this place in your code: g.drawImage(yourImage, x, y, this); g.setColor(Color.gray); //Here it put in the gray grids. if (fpPoints != null && fpPoints.length > 0) { for (int i = 0; i < fpPoints.length; i++) { g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i); } for (int j = 0; j < fpPoints[0].length; j++) { g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight()); } }
For custom drawing it is better to override the paintComponent method as recommended in the JComponent class api (see the paint method).
If you choose to do so be sure to call super.paintComponent(g) before drawing. This will cause the background to be filled with the background color and will avoid artifacts. This is not necessary when overriding the paint method.

The basic idea in drawing is to draw the bottom layer first and the top layer last. You can draw yout image followed by your grid and the grid will appear between your eye and the image.

I attached the whole thing I am working on here, please have a look.
I looked at the first zip file but was unable to do much with it.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-25-2008, 11:57 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
HallArea.java:169: cannot find symbol
symbol : variable ff
location: class fpsim.HallArea
g.drawImage(ff.jpeg, 0, 0, this);


while my image is ff.jpeg, in the same folder...
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-26-2008, 01:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 936
hardwired is on a distinguished road
Code:
g.drawImage(ff.jpeg, 0, 0, this);
Note how the image was loaded in post #8: use ImageIO to read a file constructed from/with the image path. This will return a BufferedImage which you can pass to the drawImage method. You must load the image before you can draw it.
Code:
classs Pseudo extends JPanel } BufferedImage image; public Pseudo() { String pathToImage = "ff.jpg"; try { image = ImageIO.read(new File(pathToImage)); } catch(IOException e) { // recover } } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); } }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-27-2008, 08:56 PM
Member
 
Join Date: Apr 2008
Posts: 9
iamchoilan is on a distinguished road
One last question.
First please read the piece of attached code.

I attached your plot graph method into it, it runs smoothly, thanks.

Now here is the question.

I want it to show a graph that contain the number of appearance of null/very strong/strong/normal/weak/very weak record.

So I put in 6 in the 421 row, stating that there is 6 record to present in the graph.

Next I want the data read into the array be the numbers of 'null' appears, numbers of 'very strong' appears, etc. So basically I want to put in a count++ thing into the methods in FpPoint.java somewhere around row 82 to row 143, and then the method will return all the counts and then be put into the array of the plot graph method.

I still need some help to implement this final question about the plot graph method.

Thanks a lot.
Attached Files
File Type: zip Simulation_tool.zip (79.9 KB, 1 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Calculation .... danny000 New To Java 1 04-15-2008 03:42 PM
Problem with my first Struts program....please help me sireesha Web Frameworks 4 04-04-2008 07:33 AM
Problem with my program HelloWorld trill New To Java 1 08-05-2007 06:32 PM
problem with recursive binary search program imran_khan New To Java 3 08-02-2007 04:08 PM
getting problem in compiling java program? sathish04021984 New To Java 3 07-30-2007 10:26 AM


All times are GMT +3. The time now is 09:22 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org