Jump to content
razvandragos29

Select Shape [java]

Recommended Posts

Posted (edited)

Salut am de facut un mini paint la facultate .

Creati un editor grafic simplu, cu 3 butoane: de adaugare in fereastra a unui cerc, de adaugare a unui patrat si de stergere a unei forme (cerc sau patrat) -forma ce se poate selecta cu mouse-ul.

 /**

* Created by on 4/6/2015.

*/

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.geom.Ellipse2D;

import java.util.ArrayList;

/**

* Created by Angheluta on 4/6/2015.

*/

public class Main extends JFrame implements ActionListener,MouseListener{

JButton b1;

JButton b2;

JButton b3;

int x,y,x1,y1;

int x3,y3;

String nume=" ";

ArrayList<Dreptunghi> dreptunghis = new ArrayList<Dreptunghi>();

ArrayList<cerc> cercs =new ArrayList<cerc>();

public Main(){

b1 =new JButton("Dreptunghi");

b1.setBounds(10,20,100,20);

b1.addActionListener(this);

b2 =new JButton("Cerc");

b2.setBounds(120,20,100,20);

b2.addActionListener(this);

b3 =new JButton("Sterge");

b3.setBounds(220,20,100,20);

b3.addActionListener(this);

addMouseListener(this);

add(b1);

add(b2);

add(b3);

setLayout(null);

setSize(600,600);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void paint(Graphics g) {

if(nume.equals("Dreptunghi")) {

for (Dreptunghi d : dreptunghis) {

d.paint(g);

}

}

if(nume.equals("Cerc")) {

for(cerc c : cercs) {

c.paint(g);

}

}

}

@M2G

Edited by razvandragos29
Posted

Salut am mai lucrat la chestia asta insa tot am anumite probleme:

nu pot sa fac icna stergerea si atunci cand vreu sa verific daca locul unde am dat click face parte dintr-un dreptunghi merge doar pentru ultimu dreptunghi desenat pentru cele desenate anterior nu merge .. Am rescris si o parte din cod si acum cred ca este mai clean ..

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JButton;

import javax.swing.JFrame;

public class Paint extends JFrame implements ActionListener,MouseListener{

JButton dreptunghi;

JButton contine;

int deseneazaDreptunghi=0 ;

int continePuncte =1;

int x;

int y;

int width;

int height;

int x1;

int y1;

int x2;

int y2;

Dreptunghi d ;

public Paint(){

setTitle("Paint");

setLayout(null);

setSize(600,600);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

dreptunghi = new JButton("Deseneaza Dreptunghi");

dreptunghi.setBounds(10,20,140,20);

dreptunghi.addActionListener(this);

add(dreptunghi);

contine = new JButton("Contine");

contine.setBounds(160, 20, 100, 20);

contine.addActionListener(this);

add(contine);

d = new Dreptunghi();

this.addMouseListener(this);

setVisible(true);

}

public void paint(Graphics g){

if(deseneazaDreptunghi == 1) {

System.out.println("Intrat in if de desenat");

d.paint(g);

}

if(continePuncte == 1){

System.out.println("Intrat in if de contine puncte");

d.paint(g);

}

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource() == dreptunghi) {

deseneazaDreptunghi=1;

continePuncte=0;

}

if(e.getSource() == contine){

continePuncte=1;

deseneazaDreptunghi=0;

}

}

public static void main(String args[]){

Paint p = new Paint();

}

}

import java.awt.Graphics;

public class Dreptunghi {

int x ;

int y;

int width;

int height;

public Dreptunghi() {

this.x=0;

this.y=0;

this.width=0;

this.height=0;

}

public boolean containts(int x, int y) {

return Math.abs(2*(x-this.x)-height) < height && Math.abs(2*(y - this.y) - width)< width;

}

public void paint(Graphics g){

g.drawRect(x, y, width, height);

}

public void setValues(int x,int y,int width,int height){

this.x=x;

this.y=y;

this.width=width;

this.height=height;

}

}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...