I am trying to create an application that divides the client area of the screen into 4 equally sized canvases. EAch canvas should have a different color. I must create at least one of the colors. Also for every two mouse clicks in a particular canvas, that canvas should draw a line between the two mouse click points.
here are my .java files.
Lab6Draw.java=
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.*;
public class Lab6Draw{
public static void main(String[] args) throws Exception{
Grid frame = new Grid();
frame.setTitle("Lab6 - Application #1");
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}
Grid.java =
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.Color.*;
public class Grid extends JFrame{
public Grid(){
JPanel p1 = new JPanel(new FlowLayout());
Color color = new Color(50,100,75);
p1.setBackground(color);
p1.setForeground(color);
p1.add(new newPanel());
JPanel p2 = new JPanel(new FlowLayout());
p2.setBackground(Color.red);
p2.setForeground(Color.blue);
JPanel p3 = new JPanel(new FlowLayout());
p3.setBackground(Color.blue);
p3.setForeground(Color.red);
JPanel p4 = new JPanel(new FlowLayout());
p4.setBackground(Color.green);
p4.setForeground(Color.yellow);
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(2,2,1,1));
p5.add(p1);
p5.add(p2);
p5.add(p3);
p5.add(p4);
this.add(p5);
}
}
Drawing.java =
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.Color.*;
class Drawing extends JFrame {
public Drawing(){
add(new newPanel());
}
}
newPanel.java =
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
class newPanel extends JPanel
implements MouseListener{
public int x1= -1;
public int y1= -1;
public int x2= -1;
public int y2= -1;
public newPanel(){
// Register listener for the mouse event
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (x1 == -1){
x1 = e.getX();
y1 = e.getY();
}
else{
x2 = e.getX();
y2 = e.getY();
}
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(x1,y1,x2,y2);
}
}