aboutsummaryrefslogtreecommitdiff
path: root/CS2452/GUI_network/Gui.java
blob: 2187030cfc35d4559153d736eee1e40da3c333ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package assign6;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.concurrent.Semaphore;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Gui extends JFrame implements ActionListener, MouseListener
{
	JPanel panel;
	JLabel label;
	BufferedImage image;
	Color color;
	Semaphore write;
	boolean sc;
	
	
	public Gui(String filename, Color color, Semaphore write, boolean sc)
	{
		this.color = color;
		this.sc = sc;
		
		panel = new JPanel(new GridLayout(1,1));
		panel.setBackground(color);
		try
		{
			image = ImageIO.read(new File(filename));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		label = new JLabel(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT)));
		
		panel.add(label);
		panel.addMouseListener(this);
		this.add(panel);
		
		this.write = write;
		
	}

	public void actionPerformed(ActionEvent e)
	{

	}
	
	public void mouseClicked(MouseEvent e)
	{
		write.release();
		changeImage();
	}


	public void mouseEntered(MouseEvent e)
	{
		
	}

	public void mouseExited(MouseEvent e)
	{
		
	}

	public void mousePressed(MouseEvent e)
	{
		
	}

	public void mouseReleased(MouseEvent e)
	{

	}
	
	public void changeImage()
	{
		
		String filename;
		Color color;
		if(sc)
		{
			filename = "img2.jpg";
			color = Color.GREEN;
		}
		else
		{
			filename = "img1.jpg";
			color = Color.BLUE;
		}
		
		try
		{
			image = ImageIO.read(new File(filename));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		label.setIcon(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT)));
		
		panel.setBackground(color);
		panel.repaint();
		
		sc = !sc;
	}
}