View Single Post
  #8 (permalink)  
Old 04-25-2008, 06:23 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
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; } }
Reply With Quote