Hi guys! I am new to java and I have a little problem with ma Java code. My application should read and paint an Image from file. The information about the image are in text format and the image can be composed of rectangles and circles. An example:

rectangle {

topLeft = 40 50;

size = 20 20;

}

circle {

centre = 40 40;

radius = 20;

color = 0 0 255

}

We have to use Canvas to paint an object. And the problem is, that I don't know how to paint from these text information an object like rectangle or circle. Can somebody help me?

rectangle {

topLeft = 40 50;

size = 20 20;

}

circle {

centre = 40 40;

radius = 20;

color = 0 0 255

}

what is this supposed to be? a quick tutorial on 'how to generate exceptions'?
be a bit more specific if possible on what you want to do, and especially on what you have done.
also, what exactly have you already done, and what has the above to do with the use of Scanner?

Ok, I don' t know how to read these text information from file or from user input(JTextARea) (where I edit the rectangles and circles) that I can paint it in Canvas in my JPanel.

I have done this:

Class Rectangle


package my;


import java.awt.*;


public class Rectangle {
private int height;
private int weight;
private int cornerX;
private int cornerY;
//private String color;


public Rectangle(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.getColor(color));
g.drawRect(height, weight, cornerX, cornerY);
}



}


class Circle:


package my;


import java.awt.*;


public class Circle {
private int r;
private int middleX;
private int middleY;
//private String color;


public Circle(int r, int middleX,int middleY) {
super();
this.r = r;
this.middleX = middleX;
this.middleY = middleY;
//this.color = color;
}


public void draw(Graphics g){
//g.setColor(Color.getColor(color));
g.drawOval(middleX, middleY, 2*r, 2*r);
}


}


class MyCanvas for painting rectangles and circles:


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);
}
}


}


class MainWindow:


package my;


import java.awt.*;
import java.awt.event.*;
import java.io.*;


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


public class MainWindow extends JFrame implements ActionListener{


private static final long serialVersionUID = 1L;
private JButton open;
private JButton save;
private JButton clear;
private MyCanvas canvas;
private JTextArea text1;
private JFileChooser fc;



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


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


fc = new JFileChooser();


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


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


text1 = new JTextArea("",10,10);
text1.setEditable(true);


JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(text1);
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);


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


setSize(700,600);


this.setContentPane(contentPane);


}


public void actionPerformed(ActionEvent e) {


try{


if (e.getSource() == open) {
int returnVal = fc.showOpenDialog(MainWindow.this);
File 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 file = fc.getSelectedFile();


if (returnVal == JFileChooser.APPROVE_OPTION) {


}
BufferedWriter out = new BufferedWriter(new FileWriter(file));
text1.write(out);
out.flush();
out.close();


}



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



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


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



}


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


}

Edited 3 Years Ago by happygeek: fixed formatting

if this

rectangle {

topLeft = 40 50;

size = 20 20;

}

circle {

centre = 40 40;

radius = 20;

color = 0 0 255

}

is the input file

there is a certain logic in it, the construction of both parts is the same, and it is linked to either a rectangle or a circle by rectangle{ or cercle{

if your first read is rectangle, read the next to lines in as Strings and by simply using indexOf(" "); you'll be able to find the seperate values for the top-left and the size,
do the same for circle

It doesn't matter on the order of objects - rectangles and circles.
Okey , so I have to read from file line by line. IndexOf() is used for severance the objects?...Can you write me a short example of using IndexOf()?

Thanks

It doesn't matter on the order of objects

I never said it did, but, for instance, you read the first line in as a String object, say:

String line = // read your first line
if ( line.indexOf("rectangle") == -1){
  /* here you do what you would do for a circle
   if indexOf returns -1, that means that the searchString
   in this case "rectangle" is not found in line
   now use indexOf(" ") for the next lines to see where the
   spaces are and where to start reading the values
 */
}
else{
 /* treat the next lines as if it was a rectangle
     since indexOf returned something else than -1,
     this means it returned the index of "rectangle" in the
     original String
  */
}

Thanks for your response and I did as suggested and it indeed got me going.

This question has already been answered. Start a new discussion instead.