Hi guys! I wrote program, which read data from file(you can modify this file) and than it paints in to JPanel containing Canvas.But it doesn't work.

class MainWindow:

package my;

import my.MyCanvas;
import my.Rectangle;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

import katka.Circle;
import katka.Shape;

public class MainWindow extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JButton open;
	private JButton save;
	private JButton clear;
	private JButton but;
	private JTextPane text1;
	private MyCanvas canvas;
	private JFileChooser fc;
	private File file;

	public MainWindow() {
		setTitle("Zadanie2");

		canvas = new MyCanvas();
		canvas.setPreferredSize(new Dimension(200, 300));

		fc = new JFileChooser();

		// JTextPane text1 = new JTextPane();
		text1 = new JTextPane();
		text1.setEditable(true);
		text1.setBorder(new TitledBorder("Editor"));
		text1.setPreferredSize(new Dimension(220, 200));

		/*
		 * JScrollPane scrol = new JScrollPane(); scrol.setViewportView(text1);
		 * //scrol.setBounds(10,80,375,350); scrol.setBorder(new
		 * TitledBorder("Editor")); scrol.setPreferredSize(new Dimension(220,
		 * 200)); scrol.add(text1);
		 */

		open = new JButton("Open");
		save = new JButton("Save");
		clear = new JButton("Clear");
		open.addActionListener(this);
		save.addActionListener(this);
		clear.addActionListener(this);

		but = new JButton("Ukaz");
		but.addActionListener(this);

		// canvas.add(new Circle(50, 50, 10));
		// canvas.add(new Circle(10, 50, 20));
		// canvas.add(new Rectangle(10, 10, 20, 20));

		/*
		 * JPanel panel2 = new JPanel(); panel2.setLayout(new BorderLayout());
		 * panel2.add(scrol); panel2.setBorder(new TitledBorder("Editor"));
		 * panel2.setPreferredSize(new Dimension(220, 200));
		 */

		JPanel panel3 = new JPanel();
		panel3.setLayout(new GridLayout(1, 1));
		panel3.setBorder(new TitledBorder("Funkcie"));
		panel3.add(open);
		panel3.add(save);
		panel3.add(clear);
		panel3.add(but);

		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.setBackground(Color.GREEN);
		contentPane.add(canvas, BorderLayout.EAST);
		contentPane.add(text1, BorderLayout.WEST);
		contentPane.add(panel3, BorderLayout.SOUTH);

		setSize(800, 800);

		this.setContentPane(contentPane);

	}

	public void actionPerformed(ActionEvent e) {

		try {

			if (e.getSource() == open) {
				int returnVal = fc.showOpenDialog(MainWindow.this);
				file = fc.getSelectedFile();
				if (returnVal == JFileChooser.APPROVE_OPTION) {

					FileReader read = new FileReader(fc.getSelectedFile());
					text1.read(read, file.toString());

				}

			} else if (e.getSource() == save) {
				int returnVal = fc.showSaveDialog(MainWindow.this);

				file = fc.getSelectedFile();

				if (returnVal == JFileChooser.APPROVE_OPTION) {

				}
				BufferedWriter out = new BufferedWriter(new FileWriter(file));
				text1.write(out);
				out.flush();// pozriet na nete
				out.close();

			}

			else if (e.getActionCommand().equals("Clear")) {
				text1.setText(null);
			}

			else if (e.getSource() == but) {
				show();
			}

		} catch (IOException exception1) {
			JOptionPane.showMessageDialog(null, "File not found!");
		}

		catch (NullPointerException exception2) {
			JOptionPane.showMessageDialog(null, "Nothing to save");
		}

	}

	public void show() {
		
		LinkedList<MyCanvas> objekt = new LinkedList<MyCanvas>();
		int red = 0;
		int green = 0;
		int blue = 0;
		int a = 0,b = 0;
		int w = 0,h = 0,r = 0;
	
		try{
			Scanner scan = new Scanner(file);
			String str = null;
			
			while( scan.hasNext() ){
				String line = scan.nextLine();
				Scanner scan2;
				int index = line.indexOf("{"); 
				if (index != -1){
					str = line.substring(0,index-1);
				}
				index = line.indexOf("Color"); 
				if (index != -1){
				scan2 = new Scanner(line.substring(index+8));
					scan2.useDelimiter("[ ;]");
					red = scan2.nextInt();
					green = scan2.nextInt();
					blue = scan2.nextInt();
				}
				index = line.indexOf(""); 
				if (index != -1){
					scan2 = new Scanner(line.substring(index+14));
					scan2.useDelimiter("[ ;]");
					a = scan2.nextInt();
					b = scan2.nextInt();
				}
				index = line.indexOf("Size"); 
				if (index != -1){
					scan2 = new Scanner(line.substring(index+10));
					scan2.useDelimiter("[ ;]");
					w = scan2.nextInt();
					h = scan2.nextInt();
				}
				index = line.indexOf("Radius"); 
				if (index != -1){
					scan2 = new Scanner(line.substring(index+10));
					scan2.useDelimiter("[ ;]");
					r = scan2.nextInt();
				}
				index = line.indexOf("}"); 
				if (index != -1){
					if ("Rectangle".equals(str)){
						Color c = null;
						if (r != -1){
							c = new Color(red,green,blue);
						}
						objekt.add(new Rectangle(c, a, b, w, h));
					} else {
						Color c = null;
						if (r != -1){
							c = new Color(red,green,blue);
						}
						objekt.add(new Circle(c, a, b,r));
					}
					red = 0;
					green = 0;
					blue = 0;
					a = 10;
					b = 10;
					w = 50;
					h = 50;
					r = 50;
				}
			}

		} catch (Exception exp) {
			//JOptionPane.showMessageDialog(null, exp.getMessage());
			System.out.println(exp.getMessage());
		}
	}

	public static void main(String[] args) {
		MainWindow window = new MainWindow();
		window.setVisible(true);
		window.setLocation(500, 200);
		window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

}

[B]class Rectangle:[/B]

package my;

import java.awt.*;

public class Rectangle extends MyCanvas {
	private int height;
	private int weight;
	private int cornerX;
	private int cornerY;
	private Color color;
	
	public Rectangle(Color color, int height, int weight, int cornerX, int cornerY) {
		super();
		this.height = height;
		this.weight = weight;
		this.cornerX = cornerX;
		this.cornerY = cornerY;
		this.color = color;
	}
	
	public Rectangle(String data){
		
	}
	
	public void draw(Graphics g){
		g.setColor(color);
		g.drawRect(height, weight, cornerX, cornerY);
	}
	
	
}

[B]Class Circle:[/B]

package my;

import java.awt.*;

public class Circle extends MyCanvas {
	private int r;
	private int middleX;
	private int middleY;
	private Color color;
	
	
	public Circle(Color color,int r, int middleX,int middleY) {
		super();
		this.r = r = 10;
		this.middleX = middleX = 10;
		this.middleY = middleY = 10;
		this.color = color;
	}
	
	public void draw(Graphics g){
		g.setColor(color);
		g.drawOval(middleX, middleY, 2*r, 2*r);
	}
	
}

[B]Class MyCanvas:[/B]

package my;

import java.awt.*;
import java.util.LinkedList;

public class MyCanvas extends Canvas{
	
	private static final long serialVersionUID = 1L;
	private LinkedList<Circle> circles;
	private LinkedList<Rectangle> rectangles;
	
	public MyCanvas(){
		circles = new LinkedList<Circle>();
		rectangles = new LinkedList<Rectangle>();
	}
	
	public void add(Circle circle){
		circles.add(circle);
		
	}
	
	public void add(Rectangle rectangle){
		rectangles.add(rectangle);
	}
	
	public void paint(Graphics g){
        for(int i=0; i<circles.size(); i++) {
            circles.get(i).draw(g);
        }
        
        for(int i=0; i<rectangles.size(); i++) {
            rectangles.get(i).draw(g);
        }
	}
	
}

The input from file looks like this:

rectangle {

     color = 255 0 255;

topLeft = 10 10;

size = 10 20;

}

 

rectangle {

topLeft = 40 50;

size = 20 20;

}

 

circle {

     centre = 40 40;

     radius = 20;

     color = 0 0 255

}

Thanks

Can you please clearly explain what you trying to do because you lost me on your attempt to add Rectangle or Circle to LinkedList of type MyCanvas while inside this class you try to stored the object in one of new linked lists

//from MainWindow show()
LinkedList<MyCanvas> objekt = new LinkedList<MyCanvas>();

//from MyCanvas
private LinkedList<Circle> circles;
private LinkedList<Rectangle> rectangles;

public MyCanvas(){
circles = new LinkedList<Circle>();
rectangles = new LinkedList<Rectangle>();
}

PS: Would be nice that you remove these imports from code import katka.Circle; import katka.Shape; if you do not want to get your classmate in trouble with your teacher
PS2: Please use code tags [code] YOUR CODE HERE [/code] as you put shame on me "kamos" by not using them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.