Exercice G09 (v3) File: Shape.java Project: G09-MiniDraw_v3 1GGE

Similar documents
Exercice G02 (v2) File: MovingBall.java Project: G02-Smileys_v2 1GGE

The Nervous Shapes Example

/** Creates new form NewJFrame */ public NewJFrame() { initcomponents(); initblogsearch(); //initializes Index List box }

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

TECHNICAL DOCUMENTATION

Object- Oriented Programming: Inheritance

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

Assignment 2. Application Development

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Control Statements: Part Pearson Education, Inc. All rights reserved.

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Classes internes, Classes locales, Classes anonymes

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

グラフを表すデータ構造 JAVA での実装

Préparation au concours ACM TP 2

APPLICATION DEVELOPMENT

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

curl -sl sh

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

S Single Responsability Principle

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

* To change this license header, choose License Headers in Project Properties.

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

First Name: AITI 2004: Exam 2 July 19, 2004

Chapter 9 - Object-Oriented Programming: Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Node. Node getleft() Returns the left Node child of this Node (might be null)

Final Exam May 21, 2003

Programming: You will have 6 files all need to be located in the dir. named PA4:

Lampiran A. SOURCE CODE PROGRAM

* To change this license header, choose License Headers in Project Properties.

Java Programming Summer 2008 LAB. Thursday 8/21/2008

2IS45 Programming

CS1150 Principles of Computer Science Objects and Classes

Role-Coll Role Based Collaboration Software

Chapter 6: Inheritance

Property Editors and Customizers. COMP434 Software Design. Property Editors. Property Editors and. Property Editors.

Reusing Classes. Hendrik Speleers

We are on the GUI fast track path

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

VHDL par Ahcène Bounceur VHDL

Answer on question #61311, Programming & Computer Science / Java

First Name: AITI 2004: Exam 2 July 19, 2004

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Héritage (2) Programmation Orientée Objet. Jean-Christophe Routier Licence mention Informatique Université Lille Sciences et Technologies

C18a: Abstract Class and Method

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

INSTITUTO POLITÉCNICO NACIONAL ESCUELA SUPERIOR DE CÓMPUTO

AP CS Unit 12: Drawing and Mouse Events

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

ILLUSTRATION DES EXCEPTIONS

Tutorial 3: Shortest path Artificial Intelligence

Practice Midterm 1. Problem Points Score TOTAL 50

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Data Structure Design II Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Bringing Life to Swing Desktop Applications

Computer Science II - Test 2

TP 3 des architectures logicielles Séance 3 : Architecture n-tiers distribuée à base d EJB. 1 Préparation de l environnement Eclipse

TWO-DIMENSIONAL FIGURES

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

COMP16121 Sample Code Lecture 1

Tutorial 4: Flow Artificial Intelligence

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

Inheritance (Deitel chapter 9)

COP 3530 HW6 - Solutions

Advanced Placement Computer Science. Inheritance and Polymorphism

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

Swing and you are winning

THE UNIVERSITY OF AUCKLAND

Inheritance and Polymorphism

Object-oriented Programming. Object-oriented Programming

CSCI 201L Midterm Written Fall % of course grade

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

PlaniSphere. Creating a plug-in for PlaniSphere.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Class. A class is an unit of Java programs; that is, Java programs consist only of classes. School. Department. Student. Course.

Test, beauty, cleanness. d après le cours de Alexandre Bergel

SampleApp.java. Page 1

Graphes: Manipulations de base et parcours

Read me carefully before making your connections!

// autor igre Ivan Programerska sekcija package mine;

AP * Calculus Review. Area and Volume

Parameter / Paramètre. (1) Assay values provided by EUROCELL Diagnostics, France. Valeurs fournies par EUROCELL Diagnostics, France.

Transcription:

Exercice G09 (v3) File: Shape.java Project: G09-MiniDraw_v3 1GGE public class Shape private Point from; private Point to; private Color color; public Shape(int px1, int py1, int px2, int py2, Color pcolor) from = new Point(pX1, py1); to = new Point(pX2, py2); color = pcolor; public Shape(Point pfrom, Point pto, Color pcolor) from = pfrom; to = pto; color = pcolor; public Point getfrom() return from; public void setfrom(point pfrom) from = pfrom; public Point getto() return to; public void setto(point pto) to = pto; public void setcolor(color pcolor) color = pcolor; public Color getcolor() return color; public int gettop() return Math.min(from.y, to.y); public int getbottom() return gettop()+getheight()-1; public int getleft() return Math.min(from.x, to.x); public int getright() return getleft()+getwidth()-1; public int getwidth() return Math.abs(from.x - to.x)+1; public int getheight() return Math.abs(from.y - to.y)+1; public double getarea() return 0; // identique pour une ligne // Ne rien faire // on arrondit pour ne pas avoir trop de décimales int a = (int) getarea(); return "[" + from.x + "," + from.y + "] -> [" + to.x + "," + to.y + "] / a=" + a + "px 2 "; Solution 1 / 10

Exercice G09 (v3) File: Ellipse.java Project: G09-MiniDraw_v3 1GGE public class Ellipse extends Shape public Ellipse(int px1, int py1, int px2, int py2, Color pcolor) super(px1, py1, px2, py2, pcolor); public Ellipse(Point pfrom, Point pto, Color pcolor) super(pfrom, pto, pcolor); public double getarea() return Math.PI * getwidth() / 2.0 * getheight() / 2.0; g.setcolor(getcolor()); g.drawoval(getleft(), gettop(), getwidth(), getheight()); return "Ellipse " + super.tostring(); Solution 2 / 10

Exercice G09 (v3) File: IsoTriangle.java Project: G09-MiniDraw_v3 1GGE public class IsoTriangle extends Shape public IsoTriangle(int px1, int py1, int px2, int py2, Color pcolor) super(px1, py1, px2, py2, pcolor); public IsoTriangle(Point pfrom, Point pto, Color pcolor) super(pfrom, pto, pcolor); public double getarea() return getwidth() * getheight() / 2; int x0 = getfrom().x; int y0 = getfrom().y; int dx = getto().x - x0; int dy = getto().y - y0; // facteur indiquant dans quel sens dessiner le triangle int fx, fy; if (dx * dy < 0) fx = 1; fy = 0; else fx = 0; fy = 1; g.setcolor(getcolor()); g.drawline(x0, y0, x0 + dx * fx, y0 + dy * fy); g.drawline(x0, y0, x0 + dx / (1 + fx), y0 + dy / (1 + fy)); g.drawline(x0 + dx * fx, y0 + dy * fy, x0 + dx / (1 + fx), y0 + dy / (1 + fy)); return "IsoTriangle " + super.tostring(); Solution 3 / 10

Exercice G09 (v3) File: Line.java Project: G09-MiniDraw_v3 1GGE public class Line extends Shape public Line(int px1, int py1, int px2, int py2, Color pcolor) super(px1, py1, px2, py2, pcolor); public Line(Point pfrom, Point pto, Color pcolor) super(pfrom, pto, pcolor); g.setcolor(getcolor()); g.drawline(getfrom().x, getfrom().y, getto().x, getto().y); return "Line " + super.tostring(); Solution 4 / 10

Exercice G09 (v3) File: Rectangle.java Project: G09-MiniDraw_v3 1GGE public class Rectangle extends Shape public Rectangle(int px1, int py1, int px2, int py2, Color pcolor) super(px1, py1, px2, py2, pcolor); public Rectangle(Point pfrom, Point pto, Color pcolor) super(pfrom, pto, pcolor); public double getarea() return getwidth() * getheight(); g.setcolor(getcolor()); g.drawrect(getleft(), gettop(), getwidth(), getheight()); return "Rectangle " + super.tostring(); Solution 5 / 10

Exercice G09 (v3) File: RightTriangle.java Project: G09-MiniDraw_v3 1GGE public class RightTriangle extends Shape public RightTriangle(int px1, int py1, int px2, int py2, Color pcolor) super(px1, py1, px2, py2, pcolor); public RightTriangle(Point pfrom, Point pto, Color pcolor) super(pfrom, pto, pcolor); public double getarea() return getwidth() * getheight() / 2; int x = getleft(); int y = gettop(); int w = getwidth(); int h = getheight(); // facteur indiquant dans quel sens dessiner le triangle int fy, fx; if (getfrom().y > getto().y) fy = 1; else fy = 0; if (getfrom().x > getto().x) fx = 1; else fx = 0; g.setcolor(getcolor()); g.drawline(x + w * fx, y + h * fy, x + w * (1 - fx), y + h * fy); g.drawline(x + w * (1 - fx), y + h * fy, x + w * (1 - fx), y + h * (1 - fy)); g.drawline(x + w * (1 - fx), y + h * (1 - fy), x + w * fx, y + h * fy); return "RightTriangle " + super.tostring(); Solution 6 / 10

Exercice G09 (v3) File: Shapes.java Project: G09-MiniDraw_v3 1GGE public class Shapes private ArrayList<Shape> alshapes = new ArrayList<>(); public void add(shape s) alshapes.add(s); public void clear() alshapes.clear(); public int getnumberofshapes(color color) int n = 0; for (int i = 0; i < alshapes.size(); i++) if (alshapes.get(i).getcolor().equals(color)) n++; return n; public int getindexofbiggestshape() if (alshapes.isempty()) return -1; // signale une liste vide int s = 0; double max = alshapes.get(0).getarea(); for (int i = 1; i < alshapes.size(); i++) if (alshapes.get(i).getarea() > max) max = alshapes.get(i).getarea(); s = i; return s; public void sortbyarea() Shape min, current; int posmin; int n = alshapes.size(); for (int i = 0; i <= n - 2; i++) min = alshapes.get(i); posmin = i; for (int j = i + 1; j <= n - 1; j++) current = alshapes.get(j); if (current.getarea() < min.getarea()) min = current; posmin = j; if (posmin!= i) alshapes.set(posmin, alshapes.get(i)); alshapes.set(i, min); // Chaque figure se dessine elle-même for (int i = 0; i < alshapes.size(); i++) alshapes.get(i).draw(g); public Object[] toarray() return alshapes.toarray(); Solution 7 / 10

Exercice G09 (v3) File: DrawPanel.java Project: G09-MiniDraw_v3 1GGE public class DrawPanel extends javax.swing.jpanel private Shapes shapes = null; public DrawPanel() initcomponents(); public void setshapes(shapes s) shapes = s; public void paintcomponent(graphics g) // Effacer le dessin int w = getwidth(); int h = getheight(); g.setcolor(color.white); g.fillrect(0, 0, w, h); // Dessiner les figures si présentes if (shapes!= null) shapes.draw(g); // Skipped:... initcomponents... // Variables declaration - do not modify//gen-begin:variables // End of variables declaration//gen-end:variables Solution 8 / 10

Exercice G09 (v3) File: MainFrame.java Project: G09-MiniDraw_v3 1GGE public class MainFrame extends javax.swing.jframe private Shapes shapes = new Shapes(); private Shape newshape = null; // Couleur à utiliser pour de nouvelles lignes private Color drawcolor = Color.BLUE; public MainFrame() initcomponents(); // La liste des shapes est fixée directement dans la JList typelist: // 0=Line / 1=Rectangle / 2=Ellipse / 3=IsoTriangle / 4=RightTriangle // au début on sélectionne directement la ligne typelist.setselectedindex(0); drawpanel.setshapes(shapes); public void updateview() shapeslist.setlistdata(shapes.toarray()); nbrshapeslabel.settext(string.valueof(shapes.getnumberofshapes(color.blue))); // selectionne la figure la plus "grande" // déselectionne si laliste est vide int i = shapes.getindexofbiggestshape(); shapeslist.setselectedindex(i); repaint(); // Skipped:... initcomponents... private void drawpanelmousedragged(java.awt.event.mouseevent evt) //GEN-FIRST:event_drawPanelMouseDragged if (newshape == null) return; newshape.setto(evt.getpoint()); //GEN-LAST:event_drawPanelMouseDragged private void drawpanelmousepressed(java.awt.event.mouseevent evt) //GEN-FIRST:event_drawPanelMousePressed int i = typelist.getselectedindex(); if (i == -1) return; // plus rien de sélectionné! if (i == 0) newshape = new Line(evt.getPoint(), evt.getpoint(), drawcolor); else if (i == 1) newshape = new Rectangle(evt.getPoint(), evt.getpoint(), drawcolor); else if (i == 2) newshape = new Ellipse(evt.getPoint(), evt.getpoint(), drawcolor); else if (i == 3) newshape = new IsoTriangle(evt.getPoint(), evt.getpoint(), drawcolor); else newshape = new RightTriangle(evt.getPoint(), evt.getpoint(), drawcolor); shapes.add(newshape); //GEN-LAST:event_drawPanelMousePressed private void drawpanelmousereleased(java.awt.event.mouseevent evt) //GEN-FIRST:event_drawPanelMouseReleased if (newshape == null) return; newshape.setto(evt.getpoint()); newshape = null; //GEN-LAST:event_drawPanelMouseReleased private void changecolorbuttonactionperformed(java.awt.event.actionevent evt)//gen-first:event_changecolorbuttonactionperformed //GEN-HEADEREND:event_changeColorButtonActionPerformed drawcolor = JColorChooser.showDialog(this, "Choix d'une couleur", drawcolor); //GEN-LAST:event_changeColorButtonActionPerformed private void drawpanelmouseclicked(java.awt.event.mouseevent evt)//gen-first:event_drawpanelmouseclicked //GEN-HEADEREND:event_drawPanelMouseClicked if (evt.getclickcount() >= 2) shapes.clear(); //GEN-LAST:event_drawPanelMouseClicked private void sortbuttonactionperformed(java.awt.event.actionevent evt)//gen-first:event_sortbuttonactionperformed //GEN-HEADEREND:event_sortButtonActionPerformed shapes.sortbyarea(); //GEN-LAST:event_sortButtonActionPerformed // Skipped:... Look & Feel // Variables declaration - do not modify//gen-begin:variables private javax.swing.jbutton changecolorbutton; private DrawPanel drawpanel; private javax.swing.jlabel jlabel1; private javax.swing.jpanel jpanel1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jscrollpane jscrollpane2; private javax.swing.jlabel nbrshapeslabel; private javax.swing.jlist shapeslist; Solution 9 / 10

Exercice G09 (v3) File: MainFrame.java Project: G09-MiniDraw_v3 1GGE private javax.swing.jbutton sortbutton; private javax.swing.jlist typelist; // End of variables declaration//gen-end:variables Solution 10 / 10