NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package test;

/*
* Program EdytorGraficzny - aplikacja z graficznym interfejsem
* - obs�uga zdarze� od klawiatury, myszki i innych element�w GUI.
*
* Autor: Pawe� Rogalinski, ...
* Data: 1. 10, 2015 r.
*/

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


abstract class Figure{

static Random random = new Random();

private boolean selected = false;

public boolean isSelected() { return selected; }
public void select() { selected = true; }
public void select(boolean z) { selected = z; }
public void unselect() { selected = false; }

protected void setColor(Graphics g) {
if (selected) g.setColor(Color.RED);
else g.setColor(Color.BLACK);
}

public abstract boolean isInside(float px, float py);
public boolean isInside(int px, int py) {
return isInside((float) px, (float) py);
}

protected String properties() {
String s = String.format(" Pole: %.0f Obwod: %.0f", computeArea(), computePerimeter());
if (isSelected()) s = s + " [SELECTED]";
return s;
}

abstract String getName();
abstract float getX();
abstract float getY();

abstract float computeArea();
abstract float computePerimeter();

abstract void move(float dx, float dy);
abstract void scale(float s);

abstract void draw(Graphics g);

@Override
public String toString(){
return getName();
}

public static final Comparator<Figure> COMPARE_NAME = new Comparator<Figure>() {
@Override
public int compare(Figure f1, Figure f2) {
return f1.getName().compareTo(f2.getName());
}
};

public static final Comparator<Figure> COMPARE_X = new Comparator<Figure>() {
@Override
public int compare(Figure f1, Figure f2) {
return (int) Math.signum(f1.getX() - f2.getX());
}
};

public static final Comparator<Figure> COMPARE_Y = new Comparator<Figure>() {
@Override
public int compare(Figure f1, Figure f2) {
return (int) Math.signum(f1.getY() - f2.getY());
}
};

public static final Comparator<Figure> COMPARE_AREA = new Comparator<Figure>() {
@Override
public int compare(Figure f1, Figure f2) {
return (int) Math.signum(f1.computeArea() - f2.computeArea());
}
};

}


class Point extends Figure{

protected float x, y;

Point()
{ this.x=random.nextFloat()*400;
this.y=random.nextFloat()*400;
}

Point(float x, float y)
{ this.x=x;
this.y=y;
}

@Override
public boolean isInside(float px, float py) {
// by umo�liwi� zaznaczanie punktu myszk�
// miejsca odleg�e nie wi�cej ni� 6 le�� wewn�trz
return (Math.sqrt((x - px) * (x - px) + (y - py) * (y - py)) <= 6);
}


@Override
String getName() {
return "Point(" + x + ", " + y + ")";
}

@Override
float getX() {
return x;
}

@Override
float getY() {
return y;
}

@Override
float computeArea(){ return 0; }

@Override
float computePerimeter(){ return 0; }

@Override
void move(float dx, float dy){ x+=dx; y+=dy; }

@Override
void scale(float s){ }

@Override
void draw(Graphics g){
setColor(g);
g.fillOval((int)(x-3),(int)(y-3), 6,6);
}

String toStringXY(){ return "(" + x + " , " + y + ")"; }

}


class Circle extends Point{
float r;

Circle(){
super();
r=random.nextFloat()*100;
}

Circle(float px, float py, float pr){
super(px,py);
r=pr;
}

@Override
public boolean isInside(float px, float py) {
return (Math.sqrt((x - px) * (x - px) + (y - py) * (y - py)) <= r);
}

@Override
String getName() {
return "Circle(" + x + ", " + y + ")";
}

@Override
float computeArea(){ return (float)Math.PI*r*r; }

@Override
float computePerimeter(){ return (float)Math.PI*r*2; }

@Override
void scale(float s){ r*=s; }

@Override
void draw(Graphics g){
setColor(g);
g.drawOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r));
}

}


class Triangle extends Figure{
Point point1, point2, point3;

Triangle(){
point1 = new Point();
point2 = new Point();
point3 = new Point();
}

Triangle(Point p1, Point p2, Point p3){
point1=p1; point2=p2; point3=p3;
}

@Override
public boolean isInside(float px, float py)
{ float d1, d2, d3;
d1 = px*(point1.y-point2.y) + py*(point2.x-point1.x) +
(point1.x*point2.y-point1.y*point2.x);
d2 = px*(point2.y-point3.y) + py*(point3.x-point2.x) +
(point2.x*point3.y-point2.y*point3.x);
d3 = px*(point3.y-point1.y) + py*(point1.x-point3.x) +
(point3.x*point1.y-point3.y*point1.x);
return ((d1<=0)&&(d2<=0)&&(d3<=0)) || ((d1>=0)&&(d2>=0)&&(d3>=0));
}

@Override
String getName() {
return "Triangle{"+point1.toStringXY()+
point2.toStringXY()+
point3.toStringXY()+"}";
}

@Override
float getX() {
return (point1.x+point2.x+point3.x)/3;
}

@Override
float getY() {
return (point1.y+point2.y+point3.y)/3;
}

@Override
float computeArea(){
float a = (float)Math.sqrt( (point1.x-point2.x)*(point1.x-point2.x)+
(point1.y-point2.y)*(point1.y-point2.y));
float b = (float)Math.sqrt( (point2.x-point3.x)*(point2.x-point3.x)+
(point2.y-point3.y)*(point2.y-point3.y));
float c = (float)Math.sqrt( (point1.x-point3.x)*(point1.x-point3.x)+
(point1.y-point3.y)*(point1.y-point3.y));
float p=(a+b+c)/2;
return (float)Math.sqrt(p*(p-a)*(p-b)*(p-c));
}

@Override
float computePerimeter(){
float a = (float)Math.sqrt( (point1.x-point2.x)*(point1.x-point2.x)+
(point1.y-point2.y)*(point1.y-point2.y));
float b = (float)Math.sqrt( (point2.x-point3.x)*(point2.x-point3.x)+
(point2.y-point3.y)*(point2.y-point3.y));
float c = (float)Math.sqrt( (point1.x-point3.x)*(point1.x-point3.x)+
(point1.y-point3.y)*(point1.y-point3.y));
return a+b+c;
}

@Override
void move(float dx, float dy){
point1.move(dx,dy);
point2.move(dx,dy);
point3.move(dx,dy);
}

@Override
void scale(float s){
Point sr1 = new Point((point1.x+point2.x+point3.x)/3,
(point1.y+point2.y+point3.y)/3);
point1.x*=s; point1.y*=s;
point2.x*=s; point2.y*=s;
point3.x*=s; point3.y*=s;
Point sr2 = new Point((point1.x+point2.x+point3.x)/3,
(point1.y+point2.y+point3.y)/3);
float dx=sr1.x-sr2.x;
float dy=sr1.y-sr2.y;
point1.move(dx,dy);
point2.move(dx,dy);
point3.move(dx,dy);
}

@Override
void draw(Graphics g){
setColor(g);
g.drawLine((int)point1.x, (int)point1.y,
(int)point2.x, (int)point2.y);
g.drawLine((int)point2.x, (int)point2.y,
(int)point3.x, (int)point3.y);
g.drawLine((int)point3.x, (int)point3.y,
(int)point1.x, (int)point1.y);
}

}



class Picture extends JPanel implements KeyListener, MouseListener {

private static final long serialVersionUID = 1L;


Vector<Figure> figures = new Vector<Figure>();



/*
* UWAGA: ta metoda b�dzie wywo�ywana automatycznie przy ka�dej potrzebie
* odrysowania na ekranie zawarto�ci panelu
*
* W tej metodzie NIE WOLNO !!! wywo�ywa� metody repaint()
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Figure f : figures)
f.draw(g);
}


void addFigure(Figure fig)
{ for (Figure f : figures){ f.unselect(); }
fig.select();
figures.add(fig);
repaint();
}


void moveAllFigures(float dx, float dy){
for (Figure f : figures){
if (f.isSelected()) f.move(dx,dy);
}
repaint();
}

void scaleAllFigures(float s){
for (Figure f : figures)
{ if (f.isSelected()) f.scale(s);
}
repaint();
}

public String toString(){
String str = "Rysunek{ ";
for(Figure f : figures)
str+=f.toString() +"n ";
str+="}";
return str;
}


/*
* Impelentacja interfejsu KeyListener - obs�uga zdarze� generowanych
* przez klawiatur� gdy focus jest ustawiony na ten obiekt.
*/
public void keyPressed (KeyEvent evt)
//Virtual keys (arrow keys, function keys, etc) - handled with keyPressed() listener.
{ int dist;
if (evt.isShiftDown()) dist = 10;
else dist = 1;
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
moveAllFigures(0, -dist);
break;
case KeyEvent.VK_DOWN:
moveAllFigures(0, dist);
break;
case KeyEvent.VK_DELETE:
Iterator<Figure> i = figures.iterator();
while (i.hasNext()) {
Figure f = i.next();
if (f.isSelected()) {
i.remove();
}
}
repaint();
break;
}
}

public void keyReleased (KeyEvent evt)
{ }

public void keyTyped (KeyEvent evt)
//Characters (a, A, #, ...) - handled in the keyTyped() listener.
{
char znak=evt.getKeyChar(); //reakcja na przycisku na naci�ni�cie klawisza
switch (znak) {
case 'p':
addFigure(new Point());
break;
case 'c':
addFigure(new Circle());
break;
case 't':
addFigure(new Triangle());
break;

case '+':
scaleAllFigures(1.1f);
break;
case '-':
scaleAllFigures(0.9f);
break;
}
}


/*
* Implementacja interfejsu MouseListener - obs�uga zdarze� generowanych przez myszk�
* gdy kursor myszki jest na tym panelu
*/
public void mouseClicked(MouseEvent e)
// Invoked when the mouse button has been clicked (pressed and released) on a component.
{ int px = e.getX();
int py = e.getY();
for (Figure f : figures)
{ if (e.isAltDown()==false) f.unselect();
if (f.isInside(px,py)) f.select( !f.isSelected() );
}
repaint();
}

public void mouseEntered(MouseEvent e)
//Invoked when the mouse enters a component.
{ }

public void mouseExited(MouseEvent e)
//Invoked when the mouse exits a component.
{ }


public void mousePressed(MouseEvent e)
// Invoked when a mouse button has been pressed on a component.
{
}

public void mouseReleased(MouseEvent e)
//Invoked when a mouse button has been released on a component.
{ }

}



public class GraphicEditor extends JFrame implements ActionListener{


private static final long serialVersionUID = 3727471814914970170L;


private final String DESCRIPTION = "OPIS PROGRAMUnn" + "Aktywna klawisze:n"
+ " strzalki ==> przesuwanie figurn"
+ " SHIFT + strzalki ==> szybkie przesuwanie figurn"
+ " +,- ==> powiekszanie, pomniejszanien"
+ " DEL ==> kasowanie figurn"
+ " p ==> dodanie nowego punktun"
+ " c ==> dodanie nowego kolan"
+ " t ==> dodanie nowego trojkatan"
+ "nOperacje myszka:n" + " klik ==> zaznaczanie figurn"
+ " ALT + klik ==> zmiana zaznaczenia figurn"
+ " przeciaganie ==> przesuwanie figur";


protected Picture picture;

private JMenu[] menu = { new JMenu("Figury"),
new JMenu("Edytuj")};

private JMenuItem[] items = { new JMenuItem("Punkt"),
new JMenuItem("Kolo"),
new JMenuItem("Trojkat"),
new JMenuItem("Wypisz wszystkie"),
new JMenuItem("Przesun w gore"),
new JMenuItem("Przesun w dol"),
new JMenuItem("Powieksz"),
new JMenuItem("Pomniejsz"),
};

private JButton buttonPoint = new JButton("Punkt");
private JButton buttonCircle = new JButton("Kolo");
private JButton buttonTriangle = new JButton("Trojkat");


public GraphicEditor()
{ super ("Edytor graficzny");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

for (int i = 0; i < items.length; i++)
items[i].addActionListener(this);

// dodanie opcji do menu "Figury"
menu[0].add(items[0]);
menu[0].add(items[1]);
menu[0].add(items[2]);
menu[0].addSeparator();
menu[0].add(items[3]);

// dodanie opcji do menu "Edytuj"
menu[1].add(items[4]);
menu[1].add(items[5]);
menu[1].addSeparator();
menu[1].add(items[6]);
menu[1].add(items[7]);

// dodanie do okna paska menu
JMenuBar menubar = new JMenuBar();
for (int i = 0; i < menu.length; i++)
menubar.add(menu[i]);
setJMenuBar(menubar);

picture=new Picture();
picture.addKeyListener(picture);
picture.setFocusable(true);
picture.addMouseListener(picture);
picture.setLayout(new FlowLayout());

buttonPoint.addActionListener(this);
buttonCircle.addActionListener(this);
buttonTriangle.addActionListener(this);

picture.add(buttonPoint);
picture.add(buttonCircle);
picture.add(buttonTriangle);

setContentPane(picture);
setVisible(true);
}

public void actionPerformed(ActionEvent evt) {
Object zrodlo = evt.getSource();

if (zrodlo == buttonPoint)
picture.addFigure(new Point());
if (zrodlo == buttonCircle)
picture.addFigure(new Circle());
if (zrodlo == buttonTriangle)
picture.addFigure(new Triangle());

if (zrodlo == items[0])
picture.addFigure(new Point());
if (zrodlo == items[1])
picture.addFigure(new Circle());
if (zrodlo == items[2])
picture.addFigure(new Triangle());
if (zrodlo == items[3])
JOptionPane.showMessageDialog(null, picture.toString());

if (zrodlo == items[4])
picture.moveAllFigures(0, -10);
if (zrodlo == items[5])
picture.moveAllFigures(0, 10);
if (zrodlo == items[6])
picture.scaleAllFigures(1.1f);
if (zrodlo == items[7])
picture.scaleAllFigures(0.9f);

picture.requestFocus(); // przywrocenie ogniskowania w celu przywrocenia
// obslugi zadarez� pd klawiatury
repaint();
}

public static void main(String[] args)
{ new GraphicEditor();
}

}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.