Hi guys,
I am making this compiler on Java so that would be multi platform. I love CodeRunner and its simplicity, but I know that there is not something like it on Windows and Linux (as far as I know). Also CodeRunner cost few $$.
Anyways, I was wondering if you could help out.
Here my mess so far:
JCompiler
Tab
At the moment I am working on the tabs. I made my own, but is quite a chore, so I might just replace them with Tabbed Panes later.
If you have any advice or want to help would be appreciated.
I am making this compiler on Java so that would be multi platform. I love CodeRunner and its simplicity, but I know that there is not something like it on Windows and Linux (as far as I know). Also CodeRunner cost few $$.
Anyways, I was wondering if you could help out.
Here my mess so far:
JCompiler
Code:
import java.awt.Font;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.GroupLayout.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
public class JCompiler extends JFrame implements KeyListener, ActionListener, MouseListener
{
String text;
JTextArea textArea = new JTextArea(50, 0);
JTextArea terminal = new JTextArea(20, 0);
JButton compileBtn = new JButton("Compile");
JButton jarBtn = new JButton("Jar");
JButton newBtn = new JButton("+");
public static Tab[] tabs;
int noClasses = 0;
int newId = 0;
int id;
public static boolean[] isOver;
Font font = new Font("Monaco", Font.PLAIN, 12);
FileWriter out;
File file;
String fileName, man;
String selectedFile = "";
public JCompiler()
{
// GRAPHICS ----------------------------------------
textArea.setFont(font);
terminal.setFont(font);
//"class Untitled\n{\n\tpublic static void main(String[] args)\n\t{\n\t\tSystem.out.println(\"Hello world\");\n\t}\n}"
textArea.setText("");
//terminal.setText("");
JScrollPane scrollingArea = new JScrollPane(textArea);
JScrollPane scrollTerminal = new JScrollPane(terminal);
newBtn.setBounds(14, 1, 40, 25);
compileBtn.setBounds(502, 1, 90, 25);
jarBtn.setBounds(420, 1, 90, 25);
//run btn
//stop btn
addTab();
scrollingArea.setBounds(20, 40, 565, 630);
scrollTerminal.setBounds(20, 670, 565, 100);
add(scrollingArea, BorderLayout.CENTER);
add(scrollTerminal);
add(compileBtn);
add(jarBtn);
add(newBtn);
newBtn.addActionListener(this);
textArea.addKeyListener(this);
compileBtn.addActionListener(this);
jarBtn.addActionListener(this);
//exec();
}
// KEY EVENT ----------------------------------------
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
//--
}
public void keyTyped(KeyEvent e)
{
//--
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == compileBtn)
{
saveFile("Empty.java");
String[] files = {"Empty.java"};
compile(files);
}
else if (e.getSource() == jarBtn)
{
saveFile("Untitled.java");
String[] files = {"Untitled.java"};
compile(files);
String mainClass = "Untitled";
String[] classes = {"Untitled.class"};
manifest(mainClass);
String jarFiles = "Untitled.class";
jar("j.jar", jarFiles);
}
else if (e.getSource() == newBtn)
{
addTab();
}
else if (e.getSource() == tabs)
{
System.out.println("test");
}
}
public void addTab()
{
noClasses++;
if (noClasses == 4)
{
newBtn.setEnabled(false);
newBtn.setBounds(14+noClasses*91, 1, 40, 25);
}
else
{
newBtn.setBounds(14+noClasses*91, 1, 40, 25);
}
isOver = new boolean[noClasses];
if (noClasses > 1)
{
Tab[] tempTabs = tabs.clone();
tabs = new Tab[noClasses];
int in = 0;
for (Tab t : tempTabs)
{
tabs[in] = tempTabs[in];
in++;
}
}
else
{
tabs = new Tab[noClasses];
}
tabs[newId] = new Tab("new class", newId);
tabs[newId].selected = true;
for (int i = 0; i < noClasses && i < 4; i++)
{
tabs[i].setBounds(20 + i * 91, 1, 90, 25);
}
tabs[newId].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
//repaint();
}
public void mouseEntered(MouseEvent me)
{
repaint();
}
public void mouseExited(MouseEvent me)
{
repaint();
}
});
tabs[newId].cross.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
removeTab(me.getComponent().getParent());
repaint();
}
});
add(tabs[newId]);
System.out.println("new tab" + newId);
newId++;
}
public void removeTab(Component t)
{
remove(t);
repaint();
}
public void saveFile(String fileName)
{
try
{
file = new File(fileName);
out = new FileWriter(file);
out.write(textArea.getText());
out.close();
}
catch (Exception e)
{
}
}
public void compile(String[] fileToCompile)
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
if(compilationResult == 0)
{
System.out.println("Compilation Successful.");
}
else
{
System.out.println("Compilation Failed.");
}
}
public void manifest(String mainClass)
{
man = "Main-Class: " + mainClass + "\n\n";
try
{
file = new File("Manifest.txt");
out = new FileWriter(file);
out.write(man);
out.close();
}
catch (Exception e)
{
}
}
public void jar(String jarName, String files)
{
try
{
Process p = Runtime.getRuntime().exec("jar cfm " + jarName + " Manifest.txt " + files);
p.waitFor();
}
catch (Exception e)
{
}
}
// to be sorted
public void exec()
{
try
{
Process p = Runtime.getRuntime().exec("java -jar j.jar");
p.waitFor();
OutputStream stdin = p.getOutputStream();
InputStream stderr = p.getErrorStream();
InputStream stdout = p.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
terminal.setText(reader.readLine());
}
catch (Exception e)
{
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
//System.out.println("pressed");
}
public void mouseReleased(MouseEvent e)
{
//System.out.println("released");
}
// MAIN ----------------------------------------
public static void main(String[] args)
{
JFrame win = new JCompiler();
win.setTitle("JCompiler v0.1");
win.setLayout(null);
win.setSize(600, 800);
win.setLocation(450, 0);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setBackground(Color.WHITE);
win.setResizable(false);
win.setVisible(true);
}
}
Tab
Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.InputEvent;
public class Tab extends JButton
{
public boolean selected;
public boolean mouseOver;
private String label;
private Dimension size = new Dimension(90, 25);
public int id;
public CloseCross cross;
public Tab(String label, int id)
{
super(label);
this.label = label;
this.id = id;
selected = false;
setSize(size.width, size.height);
setLayout(null);
cross = new CloseCross();
cross.setBounds(70, 3, 16, 16);
this.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
mouseOver = true;
}
public void mouseExited(MouseEvent e)
{
mouseOver = false;
}
});
add(cross);
}
public Dimension getPreferredSize()
{
return new Dimension(getWidth(), getHeight());
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (!selected)
{
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
g.setColor(Color.WHITE);
g.fillRect(1, 1, getWidth()-2, getHeight()-2);
g.setColor(Color.BLACK);
//int length = label.length();
g.drawString(label, 5, 18);
}
else
{
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
g.setColor(Color.WHITE);
g.fillRect(1, 1, getWidth()-2, getHeight()-1);
g.setColor(Color.BLACK);
//int length = label.length();
g.drawString(label, 5, 18);
}
}
class CloseCross extends JButton
{
private Dimension size = new Dimension(16, 16);
public CloseCross()
{
super("");
setSize(16, 16);
this.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
mouseOver = true;
}
public void mouseExited(MouseEvent e)
{
mouseOver = false;
}
});
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (!mouseOver)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
}
else
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.GRAY);
g.fillOval(0, 0, 15, 15);
g.setColor(Color.WHITE);
g.drawString("x", 4, 11);
}
}
}
}
At the moment I am working on the tabs. I made my own, but is quite a chore, so I might just replace them with Tabbed Panes later.
If you have any advice or want to help would be appreciated.