|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Problema di compilazione (Java)
Ciao ragazzi, volevo un vostro aiuto a capire dov'e' l'errore. Qui c'e' il codice, quando do' il comando javac da dos mi appare questo errore:
"C:\java>javac ShapeDrawJPanel.java ShapeDrawJPanel.java:56: cannot find symbol symbol : method getContentPane() location: class ShapeDrawJPanel getContentPane().setBackground(new Color(255,255,255)); ^" Volevo capire appunto cosa e' che sbaglio. Grazie mille! import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShapeDrawJPanel extends JPanel { private final int SHAPETYPES=3; private int shapeType=0; private String shapeTypes[]= {"Line","Oval", "Rectangle"}; private int numShapes=0; private int XYValues[] = new int[4]; private Shape[] shapes = new Shape[100]; private JLabel label; private JComboBox shapesComboBox; private JCheckBox filled; private Container container; private JPanel lowerRow; public ShapeDrawJPanel() { addMouseListener( new MouseClickHandler()); lowerRow = new JPanel(); label = new JLabel("You have to select the type of shape to draw"); lowerRow.add(label); shapesComboBox = new JComboBox(shapeTypes); shapesComboBox.setSelectedIndex(0); lowerRow.add(shapesComboBox); container.add(lowerRow, BorderLayout.SOUTH); 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) { shapes[sp].draw(g); }}} public void createShape(int index) { numShapes++; switch (index) { case 0: shapes[numShapes] = new Line(XYValues); break; case 1: shapes[numShapes] = new Oval(XYValues); break; case 2: shapes[numShapes] = new Rectangle(XYValues); break; }} private class MouseClickHandler extends MouseAdapter { public void mousePressed (MouseEvent event) { XYValues[0] = event.getX(); XYValues[1] = event.getY(); } } public void mouseReleased (MouseEvent event) { getContentPane().setBackground(new Color(255,255,255)); XYValues[2] = event.getX(); XYValues[3] = event.getY(); createShape(shapesComboBox.getSelectedIndex()); repaint(); }} |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Mi sembra che getContentPane() non sia un metodo visibile da JPanel!!!
Ecco perchè ti dà errore. |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
java ShapeDraw per l'esattezza, che sarebbe il test class per ShapeDrawJFrame( l'ho cambiato). L'errore e': " C:\java>java ShapeDraw Exception in thread "main" java.lang.NullPointerException at ShapeDrawJFrame.<init>(ShapeDrawJFrame.java:37) at ShapeDraw.main(ShapeDraw.java:10)" Non capisco cosa devo fare. Vi riporto i due codici corretti: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShapeDrawJFrame extends JFrame { private final int SHAPETYPES=3; private int shapeType=0; private String shapeTypes[]= {"Line","Oval", "Rectangle"}; private int numShapes=0; private int XYValues[] = new int[4]; private Shape[] shapes = new Shape[100]; private JLabel label; private JComboBox shapesComboBox; private JCheckBox filled; private Container container; private JPanel lowerRow; private JButton button; public ShapeDrawJFrame() { addMouseListener( new MouseClickHandler()); getContentPane().setBackground(new Color(255,255,255)); lowerRow = new JPanel(); label = new JLabel("You have to select the type of shape to draw"); lowerRow.add(label); button = new JButton("UNDO"); lowerRow.add(button); shapesComboBox = new JComboBox(shapeTypes); shapesComboBox.setSelectedIndex(0); lowerRow.add(shapesComboBox); container.add(lowerRow, BorderLayout.SOUTH); 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) { shapes[sp].draw(g); }}} public void createShape(int index) { numShapes++; switch (index) { case 0: shapes[numShapes] = new Line(XYValues); break; case 1: shapes[numShapes] = new Oval(XYValues); break; case 2: shapes[numShapes] = new Rectangle(XYValues); break; }} private class MouseClickHandler extends MouseAdapter { public void mousePressed (MouseEvent event) { XYValues[0] = event.getX(); XYValues[1] = event.getY(); } } public void mouseReleased (MouseEvent event) { XYValues[2] = event.getX(); XYValues[3] = event.getY(); createShape(shapesComboBox.getSelectedIndex()); repaint(); } } ********** 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 ); }} Grazie tante in anticipo! |
|
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
L'errore è su questa linea:
container.add(lowerRow, BorderLayout.SOUTH); ma il container non è stato inizializzato è null. Prova a fare : container = getContentPane() prima di quell'istruzione P.S Quando posti il codice, includilo nei tag "[code]" e "chiudi [code]" esempio: Codice:
public ShapeDrawJFrame() {
addMouseListener( new MouseClickHandler());
getContentPane().setBackground(new Color(255,255,255));
|
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
addMouseListener( new MouseClickHandler()); container = getContentPane() container.setBackground(new Color(255,255,255)); ma l'errore non va via... |
|
|
|
|
|
|
#6 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
|
|
|
|
|
|
#8 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
A me quelle due classi non le compila neanche.
Ma ci mancano altri pezzi? |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
si, ci mancano:
1. import java.awt.*; import javax.swing.*; public abstract class Shape { private int x1, x2, y1, y2; private boolean isFilled = false; public Shape () {setX1(0); setY1(0); setX2(0); setY2(0);} public Shape (int val1, int val2, int val3, int val4) { setX1(val1); setY1(val2); setX2(val3); setY2(val4);} public Shape (int val1,int val2,int val3,int val4,boolean fill) {setX1(val1); setY1(val2);setX2(val3);setY2(val4); isFilled = fill;} public Shape (int vals[]) {setX1(vals[0]); setY1(vals[1]); setX2(vals[2]); setY2(vals[3]); } public Shape (int vals[], boolean fill) {setX1(vals[0]); setY1(vals[1]); setX2(vals[2]); setY2(vals[3]); isFilled = fill; } 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 setFilled(boolean fill) {isFilled=fill;} public int getX1() {return x1;} public int getX2() {return x2;} public int getY1() {return y1;} public int getY2() {return y2;} public boolean getFilled() {return isFilled;} public abstract void draw (Graphics g); } 2. import java.awt.*; import javax.swing.*; public abstract class BoundedShape extends Shape { 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, boolean fill) {super(val1,val2,val3,val4, fill);} public BoundedShape (int val[]) {super(val); } public BoundedShape (int val[], boolean fill) {super(val, fill); } public int getUpperLeftX() { return Math.min(getX1(), getX2()); } public int getUpperLeftY() { return Math.min(getY1(), getY2()); } public int getWidth() {return Math.abs(getX1() - getX2());} public int getHeight() {return Math.abs(getY1() - getY2());} } 3. import java.awt.*; import javax.swing.*; public class Line extends Shape { public Line () {super();} public Line (int val1, int val2, int val3, int val4) {super(val1,val2,val3,val4); } public Line (int val[]) {super(val);} public void draw (Graphics g) {g.drawLine(getX1(),getY1(),getX2(), getY2()); } } 4. import java.awt.*; import javax.swing.*; 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, boolean fill) {super(val1,val2,val3,val4, fill);} public Oval (int val[]) {super(val);} public Oval (int val[], boolean fill) {super(val, fill);} public void draw (Graphics g) { if(getFilled()) g.drawOval(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight()); else g.fillOval(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight()); } } 5. import java.awt.*; import javax.swing.*; 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, boolean fill) {super(val1,val2,val3,val4, fill);} public Rectangle (int val[]) {super(val);} public Rectangle (int val[], boolean fill) {super(val, fill);} public void draw (Graphics g) { if(getFilled()) g.drawRect(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight()); else g.fillRect(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight()); } } |
|
|
|
|
|
#10 | |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Quote:
azz
|
|
|
|
|
|
|
#11 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Cosi a me Funziona
Codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShapeDrawJFrame extends JFrame {
private final int SHAPETYPES=3;
private int shapeType=0;
private String shapeTypes[]= {"Line","Oval", "Rectangle"};
private int numShapes=0;
private int XYValues[] = new int[4];
private Shape[] shapes = new Shape[100];
private JLabel label;
private JComboBox shapesComboBox;
private JCheckBox filled;
private Container container;
private JPanel lowerRow;
private JButton button;
public ShapeDrawJFrame() {
addMouseListener( new MouseClickHandler());
container = getContentPane();
container.setBackground(new Color(255,255,255));
lowerRow = new JPanel();
label = new JLabel("You have to select the type of shape to draw");
lowerRow.add(label);
button = new JButton("UNDO");
lowerRow.add(button);
shapesComboBox = new JComboBox(shapeTypes);
shapesComboBox.setSelectedIndex(0);
lowerRow.add(shapesComboBox);
container.add(lowerRow, BorderLayout.SOUTH);
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) {
shapes[sp].draw(g);
}
}
}
public void createShape(int index) {
numShapes++;
switch (index) {
case 0: shapes[numShapes] = new Line(XYValues); break;
case 1: shapes[numShapes] = new Oval(XYValues); break;
case 2: shapes[numShapes] = new Rectangle(XYValues); break;
}
}
private class MouseClickHandler extends MouseAdapter {
public void mousePressed (MouseEvent event) {
XYValues[0] = event.getX();
XYValues[1] = event.getY();
}
public void mouseReleased (MouseEvent event) {
XYValues[2] = event.getX();
XYValues[3] = event.getY();
createShape(shapesComboBox.getSelectedIndex());
repaint();
}
}
}
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
GRAZIE MILLE!!! ANCHE A ME FUNZIONA ORA CON LE TUE CORREZIONI.
ORA PERO' , DATO CHE SI TRATTA DI UN PROGETTO SCOLASTICO, DEVO FINIRE DI COMPLETARLO, NEL CASO AVESSI PROBLEMI...TI FACCIO SAPERE cIAO
|
|
|
|
|
|
#13 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Di Nulla.
Siamo qui apposta Ok, Fammi sapere. |
|
|
|
|
|
#14 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Ciao,
come detto prima, sto cercando di completare l'applicazione: 1. devo aggiungere piu' forme (Arcs, polygons and polylines). 2. devo aggiungere un ComboBox per scegliere i colori. 3. devo aggiungere un CheckBox per selezionare se la figura la voglio piena o no. ti ggiungo quello che ho fatto fino ad adesso, volevo piu' o meno un aiuto, una guida per aggiungere queste ultime cose. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShapeDrawJFrame extends JFrame { private final int SHAPETYPES=3; private int shapeType=0; private String shapeTypes[]= {"Line","Oval", "Rectangle", "Arcs", "Polygons",}; private int numShapes=0; private int XYValues[] = new int[4]; private MyShape[] shapes = new Shape[100]; private JCheckBox filled; private boolean isFilled; private JComboBox shapesComboBox; private JComboBox ColorChoice; private Container container; private JPanel lowerRow; private JButton button; public ShapeDrawJFrame() { addMouseListener( new MouseClickHandler()); container = getContentPane(); container.setBackground(new Color(255,255,255)); lowerRow = new JPanel(); shapesComboBox = new JComboBox(shapeTypes); shapesComboBox.setSelectedIndex(0); lowerRow.add(shapesComboBox); colorChoice = new JComboBox(); colorChoice.setSelectedIndex(0); colorChoice.addItem("Black"); colorChoice.addItem("Red"); colorChoice.addItem("Green"); colorChoice.addItem("Blue"); colorChoice.addItem("Cyan"); colorChoice.addItem("Magenta"); colorChoice.addItem("Yellow"); colorChoice.addItem("White"); colorChoice.setBackground(Color.white); lowerRow.add(colorChoice); filled = new JCheckBox(boolean.isFilled); lowerRow.add(filled); button = new JButton("UNDO"); lowerRow.add(button); container.add(lowerRow, BorderLayout.SOUTH); 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) { shapes[sp].draw(g); } } } public void createShape(int index) { numShapes++; switch (index) { case 0: shapes[numShapes] = new Line(XYValues); break; case 1: shapes[numShapes] = new Oval(XYValues); break; case 2: shapes[numShapes] = new Rectangle(XYValues); break; case 3: shapes[numShapes] = new Arcs(XYValues); break; case 4: shapes[numShapes] = new Polygons(XYValues); break; } } private class MouseClickHandler extends MouseAdapter { public void mousePressed (MouseEvent event) { XYValues[0] = event.getX(); XYValues[1] = event.getY(); } public void mouseReleased (MouseEvent event) { XYValues[2] = event.getX(); XYValues[3] = event.getY(); createShape(shapesComboBox.getSelectedIndex()); repaint(); } } } |
|
|
|
|
|
#15 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Ok, stai andando avanti bene.
Però l'applicazione devi farla tu, così avrai più soddisfazione quando avrai finito. P.P.S Quando posti il codice, includilo nei tag "[code]" e "chiudi [code]" |
|
|
|
|
|
#16 | ||
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
sto andando avanti nel progetto. Ho creato cosi' il codice per disegnare l'arco. Non so se e' giusto, soprattutto nella parte del startAngle e del arcAngle, ma nel momento in cui lo compilo mi vien fuori questo errore: C:\java\project>javac Arc.java Error occurred during initialization of VM java.lang.OutOfMemoryError: unable to create new native thread Non capisco che significa. Aggiungo che ho cambiato la superclass da Shape in MyShape come il professore mi chiedeva. Quote:
|
||
|
|
|
|
|
#17 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Ti sei dimenticato qualche parentesi graffa.
prova con questa: Codice:
import java.awt.*;
import javax.swing.*;
public abstract class Arc extends MyShape {
public Arc () {super();}
public Arc (int val1, int val2, int val3, int val4, int val5, int val6) {super(val1,val2,val3,val4,val5,val6);}
public Arc (int val1, int val2, int val3, int val4, int val5, int val6, boolean fill) {
super(val1,val2,val3,val4,val5,val6,fill);}
public Arc (int val[]) {super(val); }
public Arc (int val[], boolean fill) {super(val, fill); }
public int getUpperLeftX() { return Math.min(getX1(), getX2()); }
public int getUpperLeftY() { return Math.min(getY1(), getY2()); }
public int getWidth() {return Math.abs(getX1() - getX2());}
public int getHeight() {return Math.abs(getY1() - getY2());}
public int getStartAngle() {return Math.min(getX3, getY3);}
public int getArcAngle() {return Math.min(getX4, getY4);}
public void draw (Graphics g) {
if(getFilled())
g.drawArc(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight(),getStartAngle(),getArcAngle());
else
g.fillRect(getUpperLeftX(),getUpperLeftY(),getWidth(),getHeight(),getStartAngle(),getArcAngle());
}
}
|
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
ora esce un altro errore:
C:\java\project>javac Arc.java Error occurred during initialization of VM Could not reserve enough space for object heap sai cosa significa? |
|
|
|
|
|
#19 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 274
|
Che la JVM non riesce ad allocare la quantità richiesta di Memoria Heap.
Ma è strano che te lo dia in fase di compilazione. |
|
|
|
|
|
#20 | |
|
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
Grazie tante. Una curiosita', la parte di codice di Arc per acquisire lo startAngle e arcAngle e' corretta, secondo te? |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:51.












azz








