/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.Robot;
import java.awt.image.RenderedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import main.components.Line;
import main.components.MyPanel;
import main.components.Oval;
import main.components.Rectangle;
import main.components.Shape;
/**
*
* @author codefire
*/
public class MainFrame extends javax.swing.JFrame {
private final MyPanel panel;
private boolean firstClick = true;
private int x, y, _x, _y;
/**
* Creates new form MainFrame
*/
public MainFrame() {
initComponents();
panel = (MyPanel) drawPanel;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
MainPanel = new javax.swing.JPanel();
drawPanel = new main.components.MyPanel();
jButtonClearDraws = new javax.swing.JButton();
jButtonReset = new javax.swing.JButton();
jButtonUndoShape = new javax.swing.JButton();
jButtonSaveResult = new javax.swing.JButton();
jRadioButtonLine = new javax.swing.JRadioButton();
jRadioButtonRectangle = new javax.swing.JRadioButton();
jRadioButtonOval = new javax.swing.JRadioButton();
jButtonSaveWorkspace = new javax.swing.JButton();
jButtonOpenWorkspace = new javax.swing.JButton();
jTextFieldWorkspace = new javax.swing.JTextField();
jLabelWorkspace = new javax.swing.JLabel();
jTextFieldImage = new javax.swing.JTextField();
jLabelImage = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
drawPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
drawPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
drawPanelMouseClicked(evt);
}
});
drawPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
drawPanelMouseMoved(evt);
}
});
javax.swing.GroupLayout drawPanelLayout = new javax.swing.GroupLayout(drawPanel);
drawPanel.setLayout(drawPanelLayout);
drawPanelLayout.setHorizontalGroup(
drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 566, Short.MAX_VALUE)
);
drawPanelLayout.setVerticalGroup(
drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jButtonClearDraws.setText("Clear");
jButtonClearDraws.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonClearDrawsActionPerformed(evt);
}
});
jButtonReset.setText("Reset");
jButtonReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonResetActionPerformed(evt);
}
});
jButtonUndoShape.setText("<- Undo");
jButtonUndoShape.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonUndoShapeActionPerformed(evt);
}
});
jButtonSaveResult.setText("Save image");
jButtonSaveResult.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSaveResultActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButtonLine);
jRadioButtonLine.setText("Line");
buttonGroup1.add(jRadioButtonRectangle);
jRadioButtonRectangle.setText("Rectangle");
buttonGroup1.add(jRadioButtonOval);
jRadioButtonOval.setText("Oval");
jButtonSaveWorkspace.setText("Save workspace");
jButtonSaveWorkspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSaveWorkspaceActionPerformed(evt);
}
});
jButtonOpenWorkspace.setText("Open workspace");
jButtonOpenWorkspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOpenWorkspaceActionPerformed(evt);
}
});
jTextFieldWorkspace.setText("workspace");
jLabelWorkspace.setText("Workspace's name");
jTextFieldImage.setText("image");
jLabelImage.setText("Image's name");
javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
MainPanel.setLayout(MainPanelLayout);
MainPanelLayout.setHorizontalGroup(
MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(drawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButtonClearDraws, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonUndoShape, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonSaveResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButtonLine)
.addComponent(jRadioButtonRectangle)
.addComponent(jRadioButtonOval)
.addComponent(jButtonOpenWorkspace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonSaveWorkspace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextFieldWorkspace)
.addComponent(jLabelWorkspace)
.addComponent(jLabelImage)
.addComponent(jTextFieldImage))
.addContainerGap())
);
MainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonClearDraws, jButtonOpenWorkspace, jButtonReset, jButtonSaveResult, jButtonSaveWorkspace, jButtonUndoShape});
MainPanelLayout.setVerticalGroup(
MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainPanelLayout.createSequentialGroup()
.addComponent(drawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(MainPanelLayout.createSequentialGroup()
.addComponent(jRadioButtonLine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButtonRectangle)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButtonOval)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonReset)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonClearDraws)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonUndoShape)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabelImage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonSaveResult)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabelWorkspace)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldWorkspace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonSaveWorkspace)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonOpenWorkspace))))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void drawPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_drawPanelMouseClicked
if (firstClick) {
x = evt.getX();
y = evt.getY();
this.setTitle(String.format("x_first: %s, y_first: %s, x_second: 0, y_second: 0", x, y));
} else {
_x = evt.getX();
_y = evt.getY();
this.setTitle(String.format("x_first: %s, y_first: %s, x_second: %s, y_second: %s", x, y, _x, _y));
if(jRadioButtonLine.isSelected()){
panel.addShape(new Line(x, y, _x, _y));
}
if(jRadioButtonOval.isSelected()){
if(x > _x && y > _y){
panel.addShape(new Oval(_x, _y, x - _x, y - _y));
}
else if(x > _x && y < _y){
panel.addShape(new Oval(_x, y, x - _x, _y - y));
}
else if(x < _x && y > _y){
panel.addShape(new Oval(x, _y, _x - x, y - _y));
}
else{
panel.addShape(new Oval(x, y, _x - x, _y - y));
}
}
if(jRadioButtonRectangle.isSelected()){
if(x > _x && y > _y){
panel.addShape(new Rectangle(_x, _y, x - _x, y - _y));
}
else if(x > _x && y < _y){
panel.addShape(new Rectangle(_x, y, x - _x, _y - y));
}
else if(x < _x && y > _y){
panel.addShape(new Rectangle(x, _y, _x - x, y - _y));
}
else{
panel.addShape(new Rectangle(x, y, _x - x, _y - y));
}
}
x = y = _x = _y = 0;
jButtonUndoShape.setEnabled(true);
}
firstClick = !firstClick;
}//GEN-LAST:event_drawPanelMouseClicked
private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed
x = y = _x = _y = 0;
firstClick = true;
}//GEN-LAST:event_jButtonResetActionPerformed
private void drawPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_drawPanelMouseMoved
if (firstClick) {
this.setTitle(String.format("x_first: %s, y_first: %s, x_second: 0, _y: 0", evt.getX(), evt.getY()));
} else {
this.setTitle(String.format("x_first: %s, y_first: %s, x_second: %s, y_second: %s", x, y, evt.getX(), evt.getY()));
}
}//GEN-LAST:event_drawPanelMouseMoved
private void jButtonClearDrawsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearDrawsActionPerformed
panel.clear();
}//GEN-LAST:event_jButtonClearDrawsActionPerformed
private void jButtonUndoShapeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUndoShapeActionPerformed
if (!panel.undo()) {
jButtonUndoShape.setEnabled(false);
}
}//GEN-LAST:event_jButtonUndoShapeActionPerformed
private void jButtonSaveResultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveResultActionPerformed
try {
Robot robot = new Robot();
Image image = robot.createScreenCapture(
new java.awt.Rectangle(
this.getX() + 13, this.getY() + 35,
this.getWidth() - 157, this.getHeight() - 48
)
);
ImageIO.write((RenderedImage) image, "png", new File(jTextFieldImage.getText() + ".png"));
} catch (AWTException | IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButtonSaveResultActionPerformed
private void jButtonSaveWorkspaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveWorkspaceActionPerformed
try {
FileWriter save = new FileWriter(jTextFieldWorkspace.getText() + ".txt");
for(Shape shape : MyPanel.shapes){
save.write(shape.getClass().getName() + "\n");
save.write(shape.getX() + "\n");
save.write(shape.getY() + "\n");
if(shape instanceof Line){
Line tmpLine = new Line();
tmpLine = (Line) shape;
save.write(tmpLine.getX_() + "\n");
save.write(tmpLine.getY_() + "\n");
}
if(shape instanceof Oval){
Oval tmpOval = new Oval();
tmpOval = (Oval) shape;
save.write(tmpOval.getWidth()+ "\n");
save.write(tmpOval.getHeight()+ "\n");
}
if(shape instanceof Rectangle){
Rectangle tmpRect = new Rectangle();
tmpRect = (Rectangle) shape;
save.write(tmpRect.getWidth()+ "\n");
save.write(tmpRect.getHeight()+ "\n");
}
}
save.flush();
save.close();
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButtonSaveWorkspaceActionPerformed
private void jButtonOpenWorkspaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenWorkspaceActionPerformed
try {
BufferedReader reader = new BufferedReader(new FileReader(jTextFieldWorkspace.getText() + ".txt"));
panel.clear();
int tmp_x, tmp_y, tmp_width, tmp_height;
while(reader.ready()){
switch (reader.readLine()) {
case "main.components.Rectangle":
tmp_x = Integer.parseInt(reader.readLine());
tmp_y = Integer.parseInt(reader.readLine());
tmp_width = Integer.parseInt(reader.readLine());
tmp_height = (Integer.parseInt(reader.readLine()));
panel.addShape(new Rectangle(tmp_x, tmp_y, tmp_width, tmp_height));
break;
case "main.components.Oval":
tmp_x = Integer.parseInt(reader.readLine());
tmp_y = Integer.parseInt(reader.readLine());
tmp_width = Integer.parseInt(reader.readLine());
tmp_height = (Integer.parseInt(reader.readLine()));
panel.addShape(new Oval(tmp_x, tmp_y, tmp_width, tmp_height));
break;
case "main.components.Line":
tmp_x = Integer.parseInt(reader.readLine());
tmp_y = Integer.parseInt(reader.readLine());
tmp_width = Integer.parseInt(reader.readLine());
tmp_height = (Integer.parseInt(reader.readLine()));
panel.addShape(new Line(tmp_x, tmp_y, tmp_width, tmp_height));
break;
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButtonOpenWorkspaceActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame().setVisible(true);
jRadioButtonLine.setSelected(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel MainPanel;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JPanel drawPanel;
private javax.swing.JButton jButtonClearDraws;
private javax.swing.JButton jButtonOpenWorkspace;
private javax.swing.JButton jButtonReset;
private javax.swing.JButton jButtonSaveResult;
private javax.swing.JButton jButtonSaveWorkspace;
private javax.swing.JButton jButtonUndoShape;
private javax.swing.JLabel jLabelImage;
private javax.swing.JLabel jLabelWorkspace;
private static javax.swing.JRadioButton jRadioButtonLine;
private javax.swing.JRadioButton jRadioButtonOval;
private javax.swing.JRadioButton jRadioButtonRectangle;
private javax.swing.JTextField jTextFieldImage;
private javax.swing.JTextField jTextFieldWorkspace;
// End of variables declaration//GEN-END:variables
}