programmering java bygga figurer

Trädvy Permalänk
Medlem
Registrerad
Maj 2019

programmering java bygga figurer

Jag hade en uppgift på jobbet att skapa olika figurer!

Vad är det för fel på denne kod? varför funkar den inte?

package namn; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; /** * * @author */ public class ShapePanel extends javax.swing.JPanel { private ArrayList<Shape> shapes = new ArrayList<Shape>(); private int nr; /** * Creates new form ShapePanel */ public ShapePanel() { initComponents(); nr = 0; JFrame frame = new JFrame("Kvadrater och trianglar"); frame.add(this); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnGroupSort.add(radioShape); btnGroupSort.add(radioColour); btnGroupAdd.add(radioSquare); btnGroupAdd.add(radioTriangle); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < nr; i++) { Shape shape = shapes.get(i); shape.drawShape(i*125+20, 200+i, g); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnGroupSort = new javax.swing.ButtonGroup(); btnGroupAdd = new javax.swing.ButtonGroup(); pnlSearch = new javax.swing.JPanel(); lblSearchColour = new javax.swing.JLabel(); txtSearchColour = new javax.swing.JTextField(); btnSearch = new javax.swing.JButton(); pnlAdd = new javax.swing.JPanel(); btnAdd = new javax.swing.JButton(); radioSquare = new javax.swing.JRadioButton(); radioTriangle = new javax.swing.JRadioButton(); pnlSort = new javax.swing.JPanel(); radioShape = new javax.swing.JRadioButton(); radioColour = new javax.swing.JRadioButton(); btnSort = new javax.swing.JButton(); pnlShape = new javax.swing.JPanel(); txtShapes = new javax.swing.JTextArea(); pnlSearch.setBackground(new java.awt.Color(255, 255, 255)); pnlSearch.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sök", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24))); // NOI18N lblSearchColour.setText("Färg"); btnSearch.setText("Sök"); btnSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSearchActionPerformed(evt); } }); javax.swing.GroupLayout pnlSearchLayout = new javax.swing.GroupLayout(pnlSearch); pnlSearch.setLayout(pnlSearchLayout); pnlSearchLayout.setHorizontalGroup( pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlSearchLayout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(lblSearchColour) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtSearchColour, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pnlSearchLayout.setVerticalGroup( pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlSearchLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblSearchColour) .addComponent(txtSearchColour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(26, 26, 26) .addComponent(btnSearch)) ); pnlAdd.setBackground(new java.awt.Color(255, 255, 255)); pnlAdd.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Lägg till", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24))); // NOI18N btnAdd.setText("Lägg till"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); radioSquare.setSelected(true); radioSquare.setText("Kvadrat"); radioTriangle.setText("Triangel"); javax.swing.GroupLayout pnlAddLayout = new javax.swing.GroupLayout(pnlAdd); pnlAdd.setLayout(pnlAddLayout); pnlAddLayout.setHorizontalGroup( pnlAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlAddLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(pnlAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(pnlAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(radioSquare) .addComponent(radioTriangle)))) ); pnlAddLayout.setVerticalGroup( pnlAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlAddLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(radioSquare) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radioTriangle, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAdd)) ); pnlSort.setBackground(new java.awt.Color(255, 255, 255)); pnlSort.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sortera", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24))); // NOI18N radioShape.setSelected(true); radioShape.setText("Form"); radioColour.setText("Färg"); btnSort.setText("Sortera"); btnSort.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSortActionPerformed(evt); } }); javax.swing.GroupLayout pnlSortLayout = new javax.swing.GroupLayout(pnlSort); pnlSort.setLayout(pnlSortLayout); pnlSortLayout.setHorizontalGroup( pnlSortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlSortLayout.createSequentialGroup() .addContainerGap() .addGroup(pnlSortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlSortLayout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(btnSort) .addContainerGap()) .addGroup(pnlSortLayout.createSequentialGroup() .addGroup(pnlSortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(radioShape) .addComponent(radioColour)) .addGap(0, 0, Short.MAX_VALUE)))) ); pnlSortLayout.setVerticalGroup( pnlSortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlSortLayout.createSequentialGroup() .addGap(19, 19, 19) .addComponent(radioShape) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radioColour) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnSort)) ); pnlShape.setBackground(new java.awt.Color(153, 204, 255)); pnlShape.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Kvadrater och trianglar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24))); // NOI18N txtShapes.setColumns(20); txtShapes.setLineWrap(true); txtShapes.setRows(5); javax.swing.GroupLayout pnlShapeLayout = new javax.swing.GroupLayout(pnlShape); pnlShape.setLayout(pnlShapeLayout); pnlShapeLayout.setHorizontalGroup( pnlShapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtShapes, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE) ); pnlShapeLayout.setVerticalGroup( pnlShapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlShapeLayout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(txtShapes, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(pnlShape, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pnlAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pnlSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pnlSort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(pnlSort, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pnlShape, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pnlAdd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pnlSearch, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(238, 238, 238)) ); }// </editor-fold> private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) { int colour = -1; Shape shape; if (txtSearchColour.getText().equals("R")) { colour = 0; } else if (txtSearchColour.getText().equals("G")) { colour = 1; } else if (txtSearchColour.getText().equals("B")) { colour = 2; } txtShapes.setText(""); if (colour >= 0) { for (int i = 0; i < nr; i++) { shape = shapes.get(i); if (shape.getColourNr() == colour) { txtShapes.setText(txtShapes.getText() + shape.toString()); } } } repaint(); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { Shape shape; if (nr < 5) { if (radioSquare.isSelected()) { shape = new Shape("kvadrat", (int) (Math.random() * 3)); // 3 different random colour R, G, B } else { shape = new Shape("triangel", (int) (Math.random() * 3)); } shapes.add(shape); txtShapes.setText(txtShapes.getText() + shape.toString()); repaint(); nr++; } repaint(); } private void btnSortActionPerformed(java.awt.event.ActionEvent evt) { Shape shape; ArrayList<Shape> tempshapes = new ArrayList<Shape>(); txtShapes.setText(""); if (radioShape.isSelected()) { for (int i = 0; i < nr; i++) { shape = shapes.get(i); if (shape.getShape().equals("kvadrat")) { txtShapes.setText(txtShapes.getText() + shape.toString()); tempshapes.add(shape); } } for (int i = 0; i < nr; i++) { shape = shapes.get(i); if (shape.getShape().equals("triangel")) { txtShapes.setText(txtShapes.getText() + shape.toString()); tempshapes.add(shape); } } shapes = tempshapes; } else if (radioColour.isSelected()) { for (int i = 0; i < nr; i++) { shape = shapes.get(i); for (int j = i; j < nr; j++) { if (shapes.get(j).getColourNr() < shape.getColourNr()) { // Byt plats shapes.set(i, shapes.get(j)); shapes.set(j, shape); shape = shapes.get(0); } } } for (int i = 0; i < nr; i++) { shape = shapes.get(i); txtShapes.setText(txtShapes.getText() + shape.toString()); } } repaint(); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.ButtonGroup btnGroupAdd; private javax.swing.ButtonGroup btnGroupSort; private javax.swing.JButton btnSearch; private javax.swing.JButton btnSort; private javax.swing.JLabel lblSearchColour; private javax.swing.JPanel pnlAdd; private javax.swing.JPanel pnlSearch; private javax.swing.JPanel pnlShape; private javax.swing.JPanel pnlSort; private javax.swing.JRadioButton radioColour; private javax.swing.JRadioButton radioShape; private javax.swing.JRadioButton radioSquare; private javax.swing.JRadioButton radioTriangle; private javax.swing.JTextField txtSearchColour; private javax.swing.JTextArea txtShapes; // End of variables declaration }

La till Code-taggar /Mod
Trädvy Permalänk
Hedersmedlem
Plats
Linköping
Registrerad
Okt 2006

Den är inte skriven rätt, det är felet.

Skämt åsido. Du får såklart berätta vad det är som inte funkar. Speciellt när du klistrar in så lång kod.

Trädvy Permalänk
Medlem
Registrerad
Maj 2019

@Shimonu:

den klagar på en variable.... här är den andra classen:

import java.awt.Color;
import java.awt.Graphics;

/**
*
* @author
*/
public class namn {

private int sideLength;
private String shape;
private int colourNr;

public namn(String shape, int colourNr) {
sideLength = 50;
this.shape = shape;
this.colourNr = colourNr;
}

@Override
public String toString() {
if (colourNr == 0) {
return "En röd " + shape + ".\n";
} else if (colourNr == 1) {
return "En grön " + shape + ".\n";
} else {
return "En blå " + shape + ".\n";
}
}

public String getShape() {
return shape;
}

public int getColourNr() {
return colourNr;
}

public void drawShape(int x, int y, Graphics g) {
if (colourNr == 0) {
g.setColor(Color.RED);
} else if (colourNr == 1) {
g.setColor(Color.GREEN);
} else if (colourNr == 2) {
g.setColor(Color.BLUE);
}
if (shape.equals("kvadrat")) {
g.fillRect(x, y, sideLength, sideLength);
} else {
int[] xPoints = new int[3];
xPoints[0] = x;
xPoints[1] = x + sideLength;
xPoints[2] = x;
int[] yPoints = new int[3];
yPoints[0] = y;
yPoints[1] = y;
yPoints[2] = y + sideLength;
g.fillPolygon(xPoints, yPoints, 3);
}
g.drawString(shape, x + sideLength + 10, y + sideLength / 2);
}

}

Trädvy Permalänk
Medlem
Registrerad
Jan 2012

Vilken variabel klagar den på och vad säger den det är för fel?

Sen när du klistrar in kod bör du använda [code] taggen

import java.awt.Color; import java.awt.Graphics; /** * * @author */ public class namn { private int sideLength; private String shape; private int colourNr; public namn(String shape, int colourNr) { sideLength = 50; this.shape = shape; this.colourNr = colourNr; } @Override public String toString() { if (colourNr == 0) { return "En röd " + shape + ".\n"; } else if (colourNr == 1) { return "En grön " + shape + ".\n"; } else { return "En blå " + shape + ".\n"; } } public String getShape() { return shape; } public int getColourNr() { return colourNr; } public void drawShape(int x, int y, Graphics g) { if (colourNr == 0) { g.setColor(Color.RED); } else if (colourNr == 1) { g.setColor(Color.GREEN); } else if (colourNr == 2) { g.setColor(Color.BLUE); } if (shape.equals("kvadrat")) { g.fillRect(x, y, sideLength, sideLength); } else { int[] xPoints = new int[3]; xPoints[0] = x; xPoints[1] = x + sideLength; xPoints[2] = x; int[] yPoints = new int[3]; yPoints[0] = y; yPoints[1] = y; yPoints[2] = y + sideLength; g.fillPolygon(xPoints, yPoints, 3); } g.drawString(shape, x + sideLength + 10, y + sideLength / 2); } }