|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
[Java] Aiuto per compito
Ciao ragazzi,
ho ancora un aiuto da chiedervi: per completare il compito che sto facendo, ho bisogno che qualcuno mi faccia delle correzzioni nel codice o che mi dica cosa fare. Il punto e' che anche il professore ha delle difficolta' a correggermelo. Vi posto il codice. Vi spiego prima pero' cosa deve fare: e' un' interfaccia GUI dove c'e' un tasto per selezionare le forme da disegnare, poi uno per i colori e uno per cancellare l'ultima disegnata. Il mio problema e' con una forma in particolare: il poligono. Quando lo seleziono, appare una finestra dove devo inserire di quanti punti desidero il poligono. Dopodiche' lo disegno col metodo mouseClicked. Il problema e' che non si disegna (non ci sono punti precisi e specificati nel codice, la posizione dei punti lo decide l'user). Quindi, volevo chiedervi se qualcuno poteva apporvi le sue correzioni. Ne ho provate tantissime, ma non riesco a capire ancora il problema. Un grazie a tutti. ![]() Codice:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.EOFException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.util.NoSuchElementException; public class ShapeDrawJFrame extends JFrame { private final int SHAPETYPES=8; private int shapeType=0; private String shapeTypes[]= {"Line","Oval", "Rectangle", "FilledOval", "FilledRectangle", "Arc", "FilledArc", "Poligon"}; private int numShapes=0; private int XYValues[] = new int[4]; private int numPoints = 0; private int x[] = new int[500]; private int y[] = new int[500]; private int count=1; private MyShape[] shapes = new MyShape[100]; private JComboBox shapesComboBox; private JComboBox colorChoice; private Container container; private JPanel lowerRow; private JPanel upperRow; private JButton undoButton; private JLabel label; private Graphics dragGraphics; private Color dragColor; private int sAngle; private int aAngle; private String startAngle; private String arcAngle; private String topAngle; private String endAngle; private int tAngle=0; private int eAngle=0; private ObjectOutputStream output; private ObjectInputStream input; private int numCount=0; private final static int BLACK = 0, RED = 1, // Some constants to make GREEN = 2, // the code more readable. BLUE = 3, // These numbers code for CYAN = 4, // the different drawing colors. MAGENTA = 5, YELLOW = 6, ORANGE = 7, PINK = 8, GRAY = 9; private boolean beingDragged; private JButton save, load; private JTextField fileName; public ShapeDrawJFrame() { super("Draw Shapes"); // addMouseListener(handler); // addMouseMotionListener(handler); container = getContentPane(); container.setBackground(new Color(255,255,255)); lowerRow = new JPanel(); label = new JLabel("You have select the type of shape to draw:"); lowerRow.add(label); shapesComboBox = new JComboBox(shapeTypes); shapesComboBox.setSelectedIndex(0); shapesComboBox.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { //if the polygon or polyline are chosen reset the numPoints counter if(shapesComboBox.getSelectedIndex()==7) { numPoints = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of the points:")); } } } ); lowerRow.add(shapesComboBox); colorChoice = new JComboBox(); colorChoice.addItem("Black"); colorChoice.addItem("Red"); colorChoice.addItem("Green"); colorChoice.addItem("Blue"); colorChoice.addItem("Cyan"); colorChoice.addItem("Magenta"); colorChoice.addItem("Yellow"); colorChoice.addItem("Orange"); colorChoice.addItem("Pink"); colorChoice.addItem("Gray"); colorChoice.setBackground(Color.white); lowerRow.add(colorChoice); undoButton = new JButton("UNDO"); undoButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { // puts the current position in the array as null for(int count=numShapes+3;count>=numShapes;count--) shapes[count]=null; // if the counter is not in the first position decrement it so that the array // does not run out if(numShapes>0) { numShapes--; } // this sets the polygon shape counter back to zero so that if the undone shape was a polygon // the user can draw another // refresh the screen repaint(); } } ); lowerRow.add(undoButton); container.add(lowerRow, BorderLayout.SOUTH); MouseHandler handler= new MouseHandler(); addMouseListener(handler); addMouseMotionListener(handler); upperRow = new JPanel(); save = new JButton( " Save " ); upperRow.add(save); save.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { openOutputFile(); writeShapes(); closeFile(); } } ); load = new JButton( " Load " ); upperRow.add(load); load.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { openInputFile(); readShapes(); closeFile(); repaint(); } } ); fileName=new JTextField(15); upperRow.add(fileName); container.add(upperRow, BorderLayout.NORTH); setSize(600,600); setVisible( true ); } public void paint( Graphics g) { super.paint(g); for (int sp=0;sp< shapes.length ; sp++) { if(shapes[sp]!=null) { //g.setColor(dragColor); shapes[sp].draw(g); } } } public void createShape(int index) { numShapes++; switch (index) { case 0: shapes[numShapes] = new Line(XYValues,getCurrentColor()); break; case 1: shapes[numShapes] = new Oval(XYValues,getCurrentColor()); break; case 2: shapes[numShapes] = new Rectangle(XYValues,getCurrentColor()); break; case 3: shapes[numShapes] = new FilledOval(XYValues,getCurrentColor()); break; case 4: shapes[numShapes] = new FilledRectangle(XYValues,getCurrentColor()); break; case 5: String startAngle = JOptionPane.showInputDialog("Enter startAngle:"); int sAngle = Integer.parseInt( startAngle ); String arcAngle = JOptionPane.showInputDialog("Enter arcAngle:"); int aAngle = Integer.parseInt( arcAngle ); if(sAngle>=0 && aAngle>=0) { shapes[numShapes] = new Arc(XYValues,sAngle,aAngle,getCurrentColor()); break; } case 6: String topAngle = JOptionPane.showInputDialog("Enter startAngle:"); int tAngle = Integer.parseInt( topAngle ); String endAngle = JOptionPane.showInputDialog("Enter arcAngle:"); int eAngle = Integer.parseInt( endAngle ); if(tAngle>=0 && eAngle>=0) { shapes[numShapes] = new FilledArc(XYValues,tAngle,eAngle,getCurrentColor()); break;} case 7: shapes[numShapes] = new Polygon(x,y,numPoints,getCurrentColor()); break; } } private Color getCurrentColor() { // Check the colorChoice menu to find the currently // selected color, and return the appropriate color // object. int currentColor = colorChoice.getSelectedIndex(); Color colorPlot=new Color(0,0,0); switch (currentColor) { case 0: colorPlot=Color.BLACK; break; case 1: colorPlot=Color.RED; break; case 2: colorPlot=Color.GREEN; break; case 3: colorPlot=Color.BLUE; break; case 4: colorPlot=Color.CYAN; break; case 5: colorPlot=Color.MAGENTA; break; case 6: colorPlot=Color.YELLOW; break; case 7: colorPlot=Color.ORANGE; break; case 8: colorPlot=Color.PINK; break; case 9: colorPlot=Color.GRAY; break; default: colorPlot=Color.BLACK; break; } return colorPlot; } private class MouseHandler implements MouseMotionListener, MouseListener{ public void mousePressed (MouseEvent event) { XYValues[0] = event.getX(); XYValues[1] = event.getY(); //dragGraphics = container.getGraphics(); //dragColor = getCurrentColor(); //dragGraphics.setColor(dragColor); } public void mouseDragged(MouseEvent event) { XYValues[2]=event.getX(); XYValues[3]=event.getY(); if(shapesComboBox.getSelectedIndex()!=5) { if(shapesComboBox.getSelectedIndex()!=6) { if(shapesComboBox.getSelectedIndex()!=7) { createShape(shapesComboBox.getSelectedIndex()); numShapes--; repaint(); } } } } public void mouseReleased (MouseEvent event) { XYValues[2] = event.getX(); XYValues[3] = event.getY(); createShape(shapesComboBox.getSelectedIndex()); repaint( ); } public void mouseEntered(MouseEvent event){} public void mouseClicked(MouseEvent event){ if(shapesComboBox.getSelectedIndex()==7) { if(numCount<=numPoints) { numCount++; x[numCount]=event.getX(); y[numCount]=event.getY(); System.out.println("in click " + numCount + numPoints + " "+ x[numCount] + " " + y[numCount]); createShape(shapesComboBox.getSelectedIndex()); repaint(); if(numCount<numPoints) numShapes--; if(numCount==numPoints) numCount=0; } } } public void mouseExited(MouseEvent event){} public void mouseMoved(MouseEvent event){} } public void setX(int n, int x) { if (x < 10) this.x[n] = 10; else if (x > (getWidth() - 10)) this.x[n] = (getWidth() - 10); else this.x[n] = x; } public void setY(int n, int y) { if (y < 55) this.y[n] = 53; else if (y > (getHeight() - 40)) this.y[n] = (getHeight() - 40); else this.y[n] = y; } public int getValX(int n) { return x[n]; } public int getValY(int n) { return y[n]; } public void openOutputFile() { try { output = new ObjectOutputStream(new FileOutputStream(fileName.getText())); } catch (IOException ioException ) { System.err.println("error output opening file"); } } public void openInputFile() { try { input = new ObjectInputStream(new FileInputStream(fileName.getText())); } catch (IOException ioException ) { System.err.println("error input opening file"); } } public void closeFile() { try { if(input != null) input.close(); if(output != null) output.close(); } catch (IOException ioException ) { System.err.println("error closing file"); System.exit(1); } } public void writeShapes() { for(int sp=0; sp<shapes.length;sp++) { try { if(shapes[sp]!=null) { output.writeObject(shapes[sp]); } } catch (IOException ioException) { System.err.println("error writing to file"); return; } catch (NoSuchElementException elementException) { System.err.println("Invalid Input. Please try again"); } } } public void readShapes() { for(int i=0;i<numShapes;i++) shapes[i]= null; numShapes =0; try { while(true) { shapes[numShapes] = (MyShape) input.readObject(); numShapes++; } } catch (EOFException endOfFileException) { return; } catch (ClassNotFoundException classNotFoundException) { System.err.println("unable to create object"); } catch (IOException ioException) { System.err.println("error during read from file"); } } } Codice:
import java.awt.*; import javax.swing.*; import java.io.Serializable; public abstract class MyShape implements Serializable { private int x1, x2, y1, y2; private Color color=Color.WHITE; public MyShape () {setX1(0); setY1(0); setX2(0); setY2(0);} public MyShape (int val1, int val2, int val3, int val4) { setX1(val1); setY1(val2); setX2(val3); setY2(val4);} public MyShape (int val1, int val2, int val3, int val4, Color col) { setX1(val1); setY1(val2); setX2(val3);setY2(val4); setColor(col);} public MyShape (int vals[]) {setX1(vals[0]); setY1(vals[1]); setX2(vals[2]); setY2(vals[3]); } public MyShape (int vals[], Color col) {setX1(vals[0]); setY1(vals[1]); setX2(vals[2]); setY2(vals[3]);setColor(col); } public void setX1(int val) {x1 = ( val >= 0 ? val : 0 );} public void setX2(int val) {x2 = ( val >= 0 ? val : 0 );} public void setY1(int val) {y1 = ( val >= 0 ? val : 0 );} public void setY2(int val) {y2 = ( val >= 0 ? val : 0 );} public void setColor(Color col) {this.color = col;} public Color getColor() {return color;} public int getX1() {return x1;} public int getX2() {return x2;} public int getY1() {return y1;} public int getY2() {return y2;} public abstract void draw (Graphics g); } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public abstract class Poly extends MyShape { private int x[]=new int[600]; private int y[]=new int[600]; private int points; private Color color; // saves the passed arrays so the are available to the poly shapes public Poly( int allX[],int allY[], int numPoints, Color col ) { points=numPoints; for(int j=0;j<points;j++) { x[j]=allX[j]; y[j]=allY[j]; } color=col; points=numPoints; } public int[] getX() { return x; } public int[] getY() { return y; } public int getPoints() { return points; } public Color getColor() { return color; } public abstract void draw(Graphics g); } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import static java.lang.Math.*; public class Polygon extends Poly { public Polygon( int allX[],int allY[], int numPoints, Color col ) { //send the x and y arrays and the number of points, and color to the poly class super(allX,allY,numPoints,col); } // Actually draws the polygon public void draw( Graphics g ) { g.setColor( getColor()); g.drawPolygon( getX(), getY(),getPoints()); } } Codice:
import javax.swing.JFrame; public class ShapeDraw { //execute application public static void main(String args[]) { ShapeDrawJFrame shapeDrawJFrame = new ShapeDrawJFrame(); shapeDrawJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); shapeDrawJFrame.setVisible( true ); }} Ultima modifica di mariade : 06-05-2007 alle 18:09. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Ciao, potresti postare il codice vero e proprio, cioè i file java, così da far prima a guardarli?
![]() |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Si lo vorrei poter compilare per vedere come va, si può?
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
|
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Ho capito quello che fa, ti ho chiesto se puoi uppare i file .java, così me lo provo per vedere dove sta l'inghippo.
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
|
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Va beh non ci siamo ancora capiti, volevo già i file belli e pronti, va beh fa niente dai me li prendo dal codice che hai postato
![]() |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Non riesco a compilare non trova queste classi:
Codice:
switch (index) { case 0: shapes[numShapes] = new Line(XYValues,getCurrentColor()); break; case 1: shapes[numShapes] = new Oval(XYValues,getCurrentColor()); break; case 2: shapes[numShapes] = new Rectangle(XYValues,getCurrentColor()); break; case 3: shapes[numShapes] = new FilledOval(XYValues,getCurrentColor()); break; case 4: shapes[numShapes] = new FilledRectangle(XYValues,getCurrentColor()); break; case 5: Non le trovo nemmeno nel javadoc. |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Non le ho inserite, credevo non fossero necessarie:
Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; /* import static java.lang.Math.*; */ public abstract class BoundedShape extends MyShape { public BoundedShape () {super();} public BoundedShape (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4);} public BoundedShape (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4,col); } public BoundedShape (int val[]) {super(val); } public BoundedShape (int val[], Color col) {super(val, col); } public int getUpperLeftX() { return Math.min(getX1(), getX2()); } public int getUpperLeftY() { return Math.min(getY1(), getY2()); } public int getWidth() {int w=Math.abs(getX1() - getX2()); return w;} public int getHeight() {int h=Math.abs(getY1() - getY2()); return h;} } Codice:
import java.awt.*; import javax.swing.*; import java.awt.geom.*; import java.awt.Color; import java.awt.Graphics; public class FilledArc extends BoundedShape { private int topAngle=0; private int endAngle=0; public FilledArc () {super();} public FilledArc (int val1, int val2, int val3, int val4) { super(val1,val2,val3,val4); topAngle=0; endAngle=0; } public FilledArc (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4, col); } public FilledArc (int val[],int tAngle, int eAngle, Color col) { super(val, col); topAngle= tAngle; endAngle = eAngle; } public void setTopAngle(int tAngle) {topAngle = ( tAngle >= 0 ? tAngle : 0 );} public int getTopAngle() {return topAngle;} public void setEndAngle(int eAngle) {endAngle = ( eAngle >= 0 ? eAngle : 0 );} public int getEndAngle() {return endAngle;} public void draw (Graphics g) { g.setColor(getColor()); g.fillArc(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight(),getTopAngle(),getEndAngle()); } } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class FilledOval extends BoundedShape { public FilledOval () {super();} public FilledOval (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4);} public FilledOval (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4,col);} public FilledOval (int val[]) {super(val);} public FilledOval (int val[], Color col) {super(val,col);} public void draw (Graphics g) { g.setColor(getColor()); g.fillOval(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight());} } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class FilledRectangle extends BoundedShape { public FilledRectangle () {super();} public FilledRectangle (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4);} public FilledRectangle (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4, col);} public FilledRectangle (int val[]) {super(val);} public FilledRectangle (int val[], Color col) {super(val, col);} public void draw (Graphics g) { g.setColor(getColor()); g.fillRect(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight()); } } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class Line extends MyShape { public Line () {super();} public Line (int x1, int y1, int x2, int y2) {super(x1,y1,x2,y2); } public Line (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4,col); } public Line (int val[]) {super(val);} public Line (int val[],Color col) {super(val,col);} public void draw (Graphics g) { g.setColor(getColor()); g.drawLine(getX1(),getY1(),getX2(), getY2()); } } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class Oval extends BoundedShape { public Oval () {super();} public Oval (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4);} public Oval (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4,col);} public Oval (int val[]) {super(val);} public Oval (int val[], Color col) {super(val,col);} public void draw (Graphics g) { g.setColor(getColor()); g.drawOval(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight());} } Codice:
import java.awt.*; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class Rectangle extends BoundedShape { public Rectangle () {super();} public Rectangle (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4);} public Rectangle (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4, col);} public Rectangle (int val[]) {super(val);} public Rectangle (int val[], Color col) {super(val, col);} public void draw (Graphics g) { g.setColor(getColor()); g.drawRect(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight());} } |
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Ci manca Arc
![]() |
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Fa niente, l'ho commentata e compilato, ora controllo un po' di pazienza, spero di beccarti l'errore
![]() |
![]() |
![]() |
![]() |
#14 | |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
![]() Codice:
import java.awt.*; import javax.swing.*; import java.awt.geom.*; import java.awt.Color; import java.awt.Graphics; public class Arc extends BoundedShape { private int startAngle; private int arcAngle; public Arc () {super();} public Arc (int val1, int val2, int val3, int val4) { super(val1,val2,val3,val4); startAngle= 0; arcAngle = 0; } public Arc (int val1, int val2, int val3, int val4, Color col) {super(val1,val2,val3,val4, col); } public Arc (int val[],int sAngle, int aAngle, Color col) { super(val, col); startAngle= sAngle; arcAngle = aAngle; } public void setStartAngle(int sAngle) {startAngle = ( sAngle >= 0 ? sAngle : 0 );} public int getStartAngle() {return startAngle;} public void setArcAngle(int aAngle) {arcAngle = ( aAngle >= 0 ? aAngle : 0 );} public int getArcAngle() {return arcAngle;} public void draw (Graphics g) { g.setColor(getColor()); g.drawArc(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight(),getStartAngle(),getArcAngle()); } } ![]() |
|
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Ecco qui
Codice:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.EOFException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.util.NoSuchElementException; public class ShapeDrawJFrame extends JFrame { private final int SHAPETYPES=8; private int shapeType=0; private String shapeTypes[]= {"Line","Oval", "Rectangle", "FilledOval", "FilledRectangle", "Arc", "FilledArc", "Poligon"}; private int numShapes=0; private int XYValues[] = new int[4]; private int numPoints = 0; private int x[] = new int[500]; private int y[] = new int[500]; private int count=1; private MyShape[] shapes = new MyShape[100]; private JComboBox shapesComboBox; private JComboBox colorChoice; private Container container; private JPanel lowerRow; private JPanel upperRow; private JButton undoButton; private JLabel label; private Graphics dragGraphics; private Color dragColor; private int sAngle; private int aAngle; private String startAngle; private String arcAngle; private String topAngle; private String endAngle; private int tAngle=0; private int eAngle=0; private ObjectOutputStream output; private ObjectInputStream input; private int numCount=0; private final static int BLACK = 0, RED = 1, // Some constants to make GREEN = 2, // the code more readable. BLUE = 3, // These numbers code for CYAN = 4, // the different drawing colors. MAGENTA = 5, YELLOW = 6, ORANGE = 7, PINK = 8, GRAY = 9; private boolean beingDragged; private JButton save, load; private JTextField fileName; public ShapeDrawJFrame() { super("Draw Shapes"); // addMouseListener(handler); // addMouseMotionListener(handler); container = getContentPane(); container.setBackground(new Color(255,255,255)); lowerRow = new JPanel(); label = new JLabel("You have select the type of shape to draw:"); lowerRow.add(label); shapesComboBox = new JComboBox(shapeTypes); shapesComboBox.setSelectedIndex(0); shapesComboBox.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { //if the polygon or polyline are chosen reset the numPoints counter if(shapesComboBox.getSelectedIndex()==7) { numPoints = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of the points:")); } } } ); lowerRow.add(shapesComboBox); colorChoice = new JComboBox(); colorChoice.addItem("Black"); colorChoice.addItem("Red"); colorChoice.addItem("Green"); colorChoice.addItem("Blue"); colorChoice.addItem("Cyan"); colorChoice.addItem("Magenta"); colorChoice.addItem("Yellow"); colorChoice.addItem("Orange"); colorChoice.addItem("Pink"); colorChoice.addItem("Gray"); colorChoice.setBackground(Color.white); lowerRow.add(colorChoice); undoButton = new JButton("UNDO"); undoButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { // puts the current position in the array as null for(int count=numShapes+3;count>=numShapes;count--) shapes[count]=null; // if the counter is not in the first position decrement it so that the array // does not run out if(numShapes>0) { numShapes--; } // this sets the polygon shape counter back to zero so that if the undone shape was a polygon // the user can draw another // refresh the screen repaint(); } } ); lowerRow.add(undoButton); container.add(lowerRow, BorderLayout.SOUTH); MouseHandler handler= new MouseHandler(); addMouseListener(handler); addMouseMotionListener(handler); upperRow = new JPanel(); save = new JButton( " Save " ); upperRow.add(save); save.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { openOutputFile(); writeShapes(); closeFile(); } } ); load = new JButton( " Load " ); upperRow.add(load); load.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { openInputFile(); readShapes(); closeFile(); repaint(); } } ); fileName=new JTextField(15); upperRow.add(fileName); container.add(upperRow, BorderLayout.NORTH); setSize(600,600); setVisible( true ); } public void paint( Graphics g) { super.paint(g); for (int sp=0;sp< shapes.length ; sp++) { if(shapes[sp]!=null) { //g.setColor(dragColor); shapes[sp].draw(g); } } } public void createShape(int index) { numShapes++; switch (index) { case 0: shapes[numShapes] = new Line(XYValues,getCurrentColor()); break; case 1: shapes[numShapes] = new Oval(XYValues,getCurrentColor()); break; case 2: shapes[numShapes] = new Rectangle(XYValues,getCurrentColor()); break; case 3: shapes[numShapes] = new FilledOval(XYValues,getCurrentColor()); break; case 4: shapes[numShapes] = new FilledRectangle(XYValues,getCurrentColor()); break; case 5: String startAngle = JOptionPane.showInputDialog("Enter startAngle:"); int sAngle = Integer.parseInt( startAngle ); String arcAngle = JOptionPane.showInputDialog("Enter arcAngle:"); int aAngle = Integer.parseInt( arcAngle ); if(sAngle>=0 && aAngle>=0) { shapes[numShapes] = new Arc(XYValues,sAngle,aAngle,getCurrentColor()); break; } case 6: String topAngle = JOptionPane.showInputDialog("Enter startAngle:"); int tAngle = Integer.parseInt( topAngle ); String endAngle = JOptionPane.showInputDialog("Enter arcAngle:"); int eAngle = Integer.parseInt( endAngle ); if(tAngle>=0 && eAngle>=0) { shapes[numShapes] = new FilledArc(XYValues,tAngle,eAngle,getCurrentColor()); break;} case 7:System.out.println(numPoints); shapes[numShapes] = new Polygon(x,y,numPoints,getCurrentColor()); break; } } private Color getCurrentColor() { // Check the colorChoice menu to find the currently // selected color, and return the appropriate color // object. int currentColor = colorChoice.getSelectedIndex(); Color colorPlot=new Color(0,0,0); switch (currentColor) { case 0: colorPlot=Color.BLACK; break; case 1: colorPlot=Color.RED; break; case 2: colorPlot=Color.GREEN; break; case 3: colorPlot=Color.BLUE; break; case 4: colorPlot=Color.CYAN; break; case 5: colorPlot=Color.MAGENTA; break; case 6: colorPlot=Color.YELLOW; break; case 7: colorPlot=Color.ORANGE; break; case 8: colorPlot=Color.PINK; break; case 9: colorPlot=Color.GRAY; break; default: colorPlot=Color.BLACK; break; } return colorPlot; } private class MouseHandler implements MouseMotionListener, MouseListener{ public void mousePressed (MouseEvent event) { XYValues[0] = event.getX(); XYValues[1] = event.getY(); //dragGraphics = container.getGraphics(); //dragColor = getCurrentColor(); //dragGraphics.setColor(dragColor); } public void mouseDragged(MouseEvent event) { XYValues[2]=event.getX(); XYValues[3]=event.getY(); if(shapesComboBox.getSelectedIndex()!=5) { if(shapesComboBox.getSelectedIndex()!=6) { if(shapesComboBox.getSelectedIndex()!=7) { createShape(shapesComboBox.getSelectedIndex()); numShapes--; repaint(); } } } } public void mouseReleased (MouseEvent event) { XYValues[2] = event.getX(); XYValues[3] = event.getY(); //createShape(shapesComboBox.getSelectedIndex()); repaint( ); } public void mouseEntered(MouseEvent event){} public void mouseClicked(MouseEvent event){ if(shapesComboBox.getSelectedIndex()==7){ if(numCount<(numPoints-1)){ x[numCount]=event.getX(); y[numCount]=event.getY(); //System.out.println(numCount+" "+x[numCount]); // System.out.println("in click " + numCount + numPoints + " "+ x[numCount] + " " + y[numCount]); //createShape(shapesComboBox.getSelectedIndex()); //repaint(); numCount++; //if(numCount==numPoints) numCount=0; } else{ numShapes--; x[numCount]=event.getX(); y[numCount]=event.getY(); System.out.println("---->"+numCount+" "+numPoints); createShape(shapesComboBox.getSelectedIndex()); numCount=0; } } } public void mouseExited(MouseEvent event){} public void mouseMoved(MouseEvent event){} } public void setX(int n, int x) { if (x < 10) this.x[n] = 10; else if (x > (getWidth() - 10)) this.x[n] = (getWidth() - 10); else this.x[n] = x; } public void setY(int n, int y) { if (y < 55) this.y[n] = 53; else if (y > (getHeight() - 40)) this.y[n] = (getHeight() - 40); else this.y[n] = y; } public int getValX(int n) { return x[n]; } public int getValY(int n) { return y[n]; } public void openOutputFile() { try { output = new ObjectOutputStream(new FileOutputStream(fileName.getText())); } catch (IOException ioException ) { System.err.println("error output opening file"); } } public void openInputFile() { try { input = new ObjectInputStream(new FileInputStream(fileName.getText())); } catch (IOException ioException ) { System.err.println("error input opening file"); } } public void closeFile() { try { if(input != null) input.close(); if(output != null) output.close(); } catch (IOException ioException ) { System.err.println("error closing file"); System.exit(1); } } public void writeShapes() { for(int sp=0; sp<shapes.length;sp++) { try { if(shapes[sp]!=null) { output.writeObject(shapes[sp]); } } catch (IOException ioException) { System.err.println("error writing to file"); return; } catch (NoSuchElementException elementException) { System.err.println("Invalid Input. Please try again"); } } } public void readShapes() { for(int i=0;i<numShapes;i++) shapes[i]= null; numShapes =0; try { while(true) { shapes[numShapes] = (MyShape) input.readObject(); numShapes++; } } catch (EOFException endOfFileException) { return; } catch (ClassNotFoundException classNotFoundException) { System.err.println("unable to create object"); } catch (IOException ioException) { System.err.println("error during read from file"); } } } ![]() Il problema principale era comunque che disegnvi il poligono per ogni punti, e in più non partivi dall'indice 0 per pololare l'array di punti ma dall'1. |
![]() |
![]() |
![]() |
#16 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Ho corretto e visto gli errori! Ti ringrazio infinitamente, non sai che peso mi hai tolto!!!!
![]() ![]() Grazie tante davvero! ![]() |
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Figurati, siamo qui apposta
![]() Ultima modifica di MEMon : 06-05-2007 alle 20:31. |
![]() |
![]() |
![]() |
#18 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
solo una domanda:
dato che lo stesso codice l'ho adattato anche per un fillPolygon e drawPolyline, mi fa solo un piccolo problema, cioe' se io disegno un poligono e poi voglio disegnare un polyline, nel momento in cui finisco di cliccare l'ultimo punto di quest'ultima figura, succede che la precedente scompare. Perche'? |
![]() |
![]() |
![]() |
#19 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Eh non saprei bisogna che mi fai vedere cosa hai fatto...
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
questo e' il codice come l'ho conciato
![]() Codice:
public void mouseClicked(MouseEvent event) { if (shapesComboBox.getSelectedIndex() == 7 || shapesComboBox.getSelectedIndex() == 8 || shapesComboBox.getSelectedIndex() == 9) { if (numCount < (numPoints - 1)) { x[numCount] = event.getX(); y[numCount] = event.getY(); //System.out.println(numCount+" "+x[numCount]); // System.out.println("in click " + numCount + numPoints + " "+ x[numCount] + " " + y[numCount]); //createShape(shapesComboBox.getSelectedIndex()); //repaint(); numCount++; //if(numCount==numPoints) numCount=0; } else { numShapes--; x[numCount] = event.getX(); y[numCount] = event.getY(); System.out.println("---->" + numCount + " " + numPoints); createShape(shapesComboBox.getSelectedIndex()); numCount = 0; repaint(); } } } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:23.