-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson 8 homework done #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scrag87
wants to merge
1
commit into
lesson7
Choose a base branch
from
lesson8
base: lesson7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package java_core.lesson8.homework.Figures; | ||
|
||
public enum Figures { | ||
LINE, | ||
CIRCLE, | ||
OVAL, | ||
SQUARE, | ||
RECTANGLE | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package java_core.lesson8.homework.Figures; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
public class Line extends JComponent { | ||
int x, y, x1, y1; | ||
|
||
public Line(int x, int y, int x1, int y1) { | ||
this.x = x; | ||
this.y = y; | ||
this.x1 = x1; | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
public int getX1() { | ||
return x1; | ||
} | ||
|
||
public void setX1(int x1) { | ||
this.x1 = x1; | ||
} | ||
|
||
public int getY1() { | ||
return y1; | ||
} | ||
|
||
public void setY1(int y1) { | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
super.paintComponent(g); | ||
Graphics2D g2d = (Graphics2D) g; | ||
|
||
g2d.setColor(Color.blue); | ||
|
||
g2d.drawLine(this.x, this.y, this.x1, this.y1); | ||
|
||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package java_core.lesson8.homework.Figures; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
public class Oval extends JComponent { | ||
int x, y, x1, y1; | ||
|
||
public Oval(int x, int y, int x1, int y1) { | ||
this.x = x; | ||
this.y = y; | ||
this.x1 = x1; | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
public int getX1() { | ||
return x1; | ||
} | ||
|
||
public void setX1(int x1) { | ||
this.x1 = x1; | ||
} | ||
|
||
public int getY1() { | ||
return y1; | ||
} | ||
|
||
public void setY1(int y1) { | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
super.paintComponent(g); | ||
Graphics2D g2d = (Graphics2D) g; | ||
|
||
g2d.setColor(Color.blue); | ||
|
||
|
||
g2d.drawOval(this.x, this.y, this.x1 - this.x, this.y1 - this.y); | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package java_core.lesson8.homework.Figures; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
public class Rectangle extends JComponent { | ||
int x, y, x1, y1; | ||
|
||
public Rectangle(int x, int y, int x1, int y1) { | ||
this.x = x; | ||
this.y = y; | ||
this.x1 = x1; | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
public int getX1() { | ||
return x1; | ||
} | ||
|
||
public void setX1(int x1) { | ||
this.x1 = x1; | ||
} | ||
|
||
public int getY1() { | ||
return y1; | ||
} | ||
|
||
public void setY1(int y1) { | ||
this.y1 = y1; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
super.paintComponent(g); | ||
Graphics2D g2d = (Graphics2D) g; | ||
|
||
g2d.setColor(Color.blue); | ||
|
||
|
||
g2d.drawRect(this.x, this.y, this.x1 - this.x, this.y1 - this.y); | ||
|
||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package java_core.lesson8.homework; | ||
|
||
import java_core.lesson8.homework.Figures.Figures; | ||
import java_core.lesson8.homework.Figures.Line; | ||
import java_core.lesson8.homework.Figures.Oval; | ||
import java_core.lesson8.homework.Figures.Rectangle; | ||
|
||
import javax.swing.*; | ||
import java.awt.Color; | ||
import java.awt.*; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
import java.util.ArrayList; | ||
|
||
public class Window extends JFrame { | ||
static Figures figures = Figures.LINE; | ||
static Point p1 = new Point(0, 0); | ||
static Point p2 = new Point(0, 0); | ||
static JFrame frame = new JFrame("Painter"); | ||
static JPanel panel = new JPanel(); | ||
static ArrayList<JComponent> list = new ArrayList<>(); | ||
|
||
public static void main(String[] args) { | ||
//Creating the Frame | ||
|
||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.setSize(500, 500); | ||
|
||
//Creating the MenuBar and adding components | ||
JMenuBar mb = new JMenuBar(); | ||
|
||
JMenu m1 = new JMenu("Figures"); | ||
JMenu m2 = new JMenu("Actions"); | ||
mb.add(m1); | ||
mb.add(m2); | ||
//Figures menu | ||
JMenuItem m11 = new JMenuItem("Line"); | ||
JMenuItem m12 = new JMenuItem("Rectangle"); | ||
JMenuItem m13 = new JMenuItem("Oval"); | ||
m1.add(m11); | ||
m1.add(m12); | ||
m1.add(m13); | ||
|
||
//Actions menu | ||
JMenuItem m21 = new JMenuItem("Save"); | ||
JMenuItem m22 = new JMenuItem("Reset"); | ||
m2.add(m21); | ||
m2.add(m22); | ||
|
||
|
||
JPanel panel = new JPanel(); // the panel is not visible in output | ||
JLabel label = new JLabel(""); | ||
JButton line = new JButton("Line"); | ||
JButton rect = new JButton("Rectangle"); | ||
JButton oval = new JButton("Oval"); | ||
JButton save = new JButton("Save"); | ||
JButton reset = new JButton("Reset"); | ||
|
||
panel.add(label); // Components Added using Flow Layout | ||
|
||
panel.add(line); | ||
panel.add(oval); | ||
panel.add(rect); | ||
panel.add(save); | ||
panel.add(reset); | ||
|
||
// Text Area at the Center | ||
JLayeredPane jLayeredPane = new JLayeredPane(); | ||
|
||
//Adding Components to the frame. | ||
frame.getContentPane().add(BorderLayout.SOUTH, panel); | ||
frame.getContentPane().add(BorderLayout.NORTH, mb); | ||
frame.getContentPane().add(BorderLayout.CENTER, jLayeredPane); | ||
|
||
jLayeredPane.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mousePressed(MouseEvent e) { | ||
System.out.println(e); | ||
label.setText("x: " + e.getX() + " y: " + e.getY()); | ||
p1.x = e.getX(); | ||
p1.y = e.getY(); | ||
System.out.println("p1: " + p1.toString()); | ||
} | ||
|
||
@Override | ||
public void mouseMoved(MouseEvent e) { | ||
// super.mouseMoved(e); | ||
System.out.println(e); | ||
} | ||
|
||
@Override | ||
public void mouseDragged(MouseEvent e) { | ||
System.out.println(e); | ||
} | ||
|
||
@Override | ||
public void mouseReleased(MouseEvent e) { | ||
System.out.println(e); | ||
label.setText("x: " + e.getX() + " y: " + e.getY()); | ||
p2.x = e.getX(); | ||
p2.y = e.getY(); | ||
System.out.println("p2: " + p2.toString()); | ||
// System.out.println(list.size()); | ||
switch (figures) { | ||
case OVAL: | ||
System.out.println("OVAL"); | ||
list.add(new Oval(p1.x, p1.y, p2.x, p2.y)); | ||
frame.getContentPane().add(BorderLayout.CENTER, new Oval(p1.x, p1.y, p2.x, p2.y)); | ||
break; | ||
|
||
case RECTANGLE: | ||
System.out.println("RECTANGLE"); | ||
list.add(new Rectangle(p1.x, p1.y, p2.x, p2.y)); | ||
frame.getContentPane().add(BorderLayout.CENTER, new Rectangle(p1.x, p1.y, p2.x, p2.y)); | ||
break; | ||
case LINE: | ||
System.out.println("LINE"); | ||
list.add(new Line(p1.x, p1.y, p2.x, p2.y)); | ||
frame.getContentPane().add(BorderLayout.CENTER, new Line(p1.x, p1.y, p2.x, p2.y)); | ||
break; | ||
|
||
default: | ||
throw new IllegalStateException("Unexpected value"); | ||
} | ||
SwingUtilities.updateComponentTreeUI(frame); | ||
/* for (int i = 0; i < list.size(); i++) { | ||
* frame.getContentPane().add(BorderLayout.CENTER, list.get(i)); | ||
}*/ | ||
|
||
} | ||
|
||
}); | ||
|
||
oval.addActionListener(e -> { | ||
System.out.println("oval"); | ||
figures = Figures.OVAL; | ||
}); | ||
|
||
rect.addActionListener(e -> { | ||
System.out.println("rect"); | ||
figures = Figures.RECTANGLE; | ||
|
||
}); | ||
|
||
line.addActionListener(e -> { | ||
System.out.println("line"); | ||
figures = Figures.LINE; | ||
}); | ||
|
||
reset.addActionListener(e -> { | ||
System.out.println("reset"); | ||
//jLayeredPane.repaint(); | ||
//frame.getContentPane().remove(jLayeredPane); | ||
//frame.getContentPane().add(new JLayeredPane()).setVisible(true); | ||
//frame.getContentPane().add(jLayeredPane); | ||
//add(BorderLayout.CENTER, jLayeredPane); | ||
list.clear(); | ||
//jLayeredPane.getGraphics().clearRect(0, 0, jLayeredPane.getWidth(), jLayeredPane.getHeight()); | ||
//SwingUtilities.updateComponentTreeUI(frame); | ||
|
||
|
||
}); | ||
|
||
save.addActionListener(e -> { | ||
System.out.println("save"); | ||
// jLayeredPane.setForeground(Color.yellow); | ||
frame.setBackground(Color.orange); | ||
SwingUtilities.updateComponentTreeUI(frame); | ||
SwingUtilities.updateComponentTreeUI(jLayeredPane); | ||
}); | ||
|
||
frame.setVisible(true); | ||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.