Hello,

I have some problems with my code I need help with.

1) The Copy Last button is supposed to copy the last figure and display it once more. (Look for /*** THE COPY LAST BUTTON ***/ in the Draw Figures code)
2) The Remove Last button is supposed to remove the last figure displayed. (Look for /***THE REMOVE LAST BUTTON ***/ in the Draw Figures code)
3) My color buttons does not work. Their purpose is to change the color of the next figure displayed. (Look for /*** COLOR BUTTONS ***/ in the Draw Figures code)

Some of the code is in Norwegian. If there are any misunderstanding I will help out.

Have to complete this code in the next 6 hours - So any help would be preferred.


Draw Figures code:
Java Code:
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class DrawFigures extends JFrame {
	private JButton jbtCircle;
	private JButton jbtRectangle;
	private JButton jbtTriangle;
	private JButton jbtFilledCircle;
	private JButton jbtFilledRectangle;
	private JButton jbtFilledTriangle;
	private JButton jbtClone;
	private JButton jbtRemove;
	private JButton jbtRed;														//Adding the red button
	private JButton jbtBlue;													//Adding the blue button
	private JButton jbtGreen;													//Adding the green button
	private JPanel buttonPanel;
	private JPanel buttonPanel2;
	private DrawPanel drawPanel;
	static final String colorNames[] = {"Red", "Blue", "Green"};
	static final Color colors[] = {Color.RED, Color.BLUE, Color.GREEN};
	private ArrayList<Figure> theFigures = new ArrayList<Figure>(); 			//Holdes the figures that are made
	private Color currentColorBlue = Color.BLUE; 								//Holdes the chosen color
	private Color currentColorRed = Color.red;
	private Color currentColorGreen = Color.green;
	


	public DrawFigures() {
		buttonPanel = new JPanel(); 											// panelet som holder trykk-knappene i det nederste panelet
		buttonPanel2 = new JPanel();											// inn her: lage panelet som holder fargevalg knappene
		drawPanel = new DrawPanel();											// i dette panelet skal figurene tegnes opp
		jbtCircle = new JButton("Sirkel");
		jbtRectangle = new JButton("Rektangel");
		jbtTriangle = new JButton("Triangel");
		jbtFilledCircle = new JButton("Fylt Sirkel");
		jbtFilledRectangle = new JButton("Fylt Rektangel");
		jbtFilledTriangle = new JButton("Fylt Triangel");
		jbtClone = new JButton("Kopier siste");
		jbtRemove = new JButton("Fjern en");
		jbtRed = new JButton("Red");											//Red button
		jbtBlue = new JButton("Blue");											//Blue button
		jbtGreen = new JButton("Green");										//Green button
		buttonPanel.add(jbtCircle);
		buttonPanel.add(jbtRectangle);
		buttonPanel.add(jbtTriangle);
		buttonPanel.add(jbtFilledCircle);
		buttonPanel.add(jbtFilledRectangle);
		buttonPanel.add(jbtFilledTriangle);
		buttonPanel.add(jbtClone);
		buttonPanel.add(jbtRemove);
		buttonPanel2.add(jbtRed);												//Red button to buttonPanel2
		buttonPanel2.add(jbtBlue);												//Blue button to buttonPanel2
		buttonPanel2.add(jbtGreen);												//Green button to buttonPanel2
		
		jbtRed.setBackground(Color.RED);										//Setting the button background to red
		jbtBlue.setBackground(Color.BLUE);										//Setting the button background to blue
		jbtGreen.setBackground(Color.GREEN);									//Setting the button background to green

		add(drawPanel, BorderLayout.CENTER);									// legg tegnepanelet i CENTER av frame
		add(buttonPanel, BorderLayout.SOUTH);									// legg trykk-knappene i SOUTH i frame
		add(buttonPanel2, BorderLayout.NORTH);									// legger farge-knappene i NORTH frame

																				// Koble en felles hendelseslytter til alle knappene
		ButtonsListener bl = new ButtonsListener();
		jbtCircle.addActionListener(bl);										//Adding circle button to the ActionListener(bl)
		jbtRectangle.addActionListener(bl);										//Adding rectangle button to the ActionListener(bl)
		jbtTriangle.addActionListener(bl);										//Adding triangle button to the ActionListener(bl)
		jbtFilledCircle.addActionListener(bl);									//Adding filledcircle button to the ActionListener(bl)
		jbtFilledRectangle.addActionListener(bl);								//Adding filledrectangle button to the ActionListener(bl)
		jbtFilledTriangle.addActionListener(bl);								//Adding filledtriangle button to the ActionListener(bl)
		jbtClone.addActionListener(bl);											//Adding clone button to the ActionListener(bl)
		jbtRemove.addActionListener(bl);										//Adding remove button to the ActionListener(bl)
		
		jbtRed.addActionListener(bl);											//Adding red button to the ActionListener(bl)
		jbtBlue.addActionListener(bl);											//Adding blue button to the ActionListener(bl)
		jbtGreen.addActionListener(bl);											//Adding green button to the ActionListener(bl)
	
																				// Knytt lytter til resterende knapper

	}

	/*** Main method ***/
	public static void main(String[] args) {
		JFrame frame = new DrawFigures();
		frame.setTitle("Program som lager figurer");
		frame.setLocationRelativeTo(null); 										// Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	/*** Klasse som inneholder en metode for å lytte på alle trykk-knappene i det nederste panelet ***/
	class ButtonsListener implements ActionListener {

		/*** Metode for å lytte på trykk-knapp hendelser ***/
		@Override
		public void actionPerformed(ActionEvent e) {
			String kommando = e.getActionCommand(); 							// Får tak i teksten på trykk-knappen
			int width = drawPanel.getWidth();
			int heigth = drawPanel.getHeight();
			

			// I if(...) konstruksjonen nedenfor:
			// Opprett en figur med tilfeldig farge, fyll, størrelse og plassering
			// Størrelse ikke større enn 1/8 av bredden på vinduet
			// Initiell X og Y posisjon for figuren slik at hele figuren tegnes
			// opp innenfor vinduet
			
			if (kommando.equals("Sirkel")) {
				theFigures.add(new Circle(false, currentColorBlue, 20, 20, 40)); 		// husk verdier skal være tilfeldige!

			} else if (kommando.equals("Rektangel")) {
				theFigures.add(new Rektangel(false, currentColorRed, 60, 20, 30, 40));
				
			} else if (kommando.equals("Triangel")) {
				Polygon p = new Polygon();
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				theFigures.add(new Triangel(false, currentColorGreen, p.xpoints[0], p.ypoints[0], p));
				
			} else if (kommando.equals("Fylt Sirkel")) {
				theFigures.add(new Circle(true, currentColorBlue, 20, 40, 40));
			} else if (kommando.equals("Fylt Rektangel")) {
				theFigures.add(new Rektangel(true, currentColorRed, 60, 20, 30, 40));
			} else if (kommando.equals("Fylt Triangel")) {
				Polygon p = new Polygon();
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				p.addPoint((int)(width * Math.random() * 0.7), (int) (heigth * Math.random() * 0.7));
				theFigures.add(new Triangel(true, currentColorGreen, p.xpoints[0], p.ypoints[0], p));
			
			/*** THE COPY LAST BUTTON ***/
			} else if (kommando.equals("Kopier siste")) {
			
				
			/***THE REMOVE LAST BUTTON ***/
			} else if (kommando.equals("Fjern en")) {
				
			}
			// videre: en else pr knapp

			// til slutt:
			drawPanel.repaint(); // denne påkaller metoden paintComponent i DrawPanel klassen
		}
	}

	/*** Klasse som inneholder en metode for å lytte på alle trykk-knappene i det øverste panelet (som du må lage) ***/
	class ColorButtonsListener implements ActionListener {

		/*** Metode for å lytte på trykk-knapp hendelser 
		 * @return ***/
		@Override
		public void actionPerformed(ActionEvent e) {
			String kommando = e.getActionCommand(); // Får tak i teksten på trykk-knappen
            
			// kompletter if-setningen nedenfor slik at den dekker alle fargevalg-trykk-knapper
			
			/*** COLOR BUTTONS ***/
			if (kommando.equals("Red"))
				currentColorRed = Color.RED;
			else if (kommando.equals("Blue"))
				currentColorBlue = Color.BLUE;
			else
				currentColorGreen = Color.GREEN;
			
		}
	}

	class DrawPanel extends JPanel {

		@Override
		// denne metoden skal du ikke kalle direkte (repaint vil indirekte kalle denne..)
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			for (Figure f : theFigures)
				f.drawMySelf(g);
		}
	}
}

Circle code:
Java Code:
import java.awt.Color;
import java.awt.Graphics;


public class Circle extends Figure {
	int radius;
	
	Circle(boolean f, Color c, int x, int y, int r) {
		super(f,c,x,y);
		radius = r;
	}
    @Override
	public void drawMySelf(Graphics g) {
		g.setColor(color);
		if (filled)
			g.fillOval(xPos, yPos, 2*radius, 2*radius);
		else
			g.drawOval(xPos, yPos, 2*radius, 2*radius);
	}
    
    // set / get metoder om nødvendig
}
Figure code:
Java Code:
import java.awt.Color;
import java.util.Random;
import java.awt.Graphics;


public abstract class Figure {
	protected boolean filled = false;
	protected Color color = Color.RED;
	protected int xPos, yPos;
		
	Figure(boolean f, Color c, int x, int y) {
		filled = f;
		color = c;
		xPos = x;
		yPos = y;
	}
	
	// metode som skal implementeres i barneklassene
	public void drawMySelf(Graphics g){};
	
	// set / get metoder om nødvendig

}
Rektangel code:
Java Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Rektangel extends Figure {
	int side1;
	int side2;
	
	Random rand = new Random();
	
	float r = rand.nextFloat();
	float g = rand.nextFloat();
	float b = rand.nextFloat();
	
	Color randomColor = new Color(r, g, b);
	
	Rektangel(boolean f, Color c, int x, int y, int s1, int s2) {
		super(f, c, x, y);
		side1 = s1;
		side2 = s2;
	}
	
	@Override
	public void drawMySelf(Graphics g) {
		g.setColor(color);
		if (filled)
			g.fillRect(xPos, yPos, side1, side2);
		else
			g.drawRect(xPos, yPos, side1, side2);
	}

}
Triangel code:
Java Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.Random;

public class Triangel extends Figure {
	Polygon tPoints;
	
	Triangel(boolean f, Color c, int x, int y, Polygon p) {
		super(f, c, x, y);
		tPoints = p;
	}
	
	@Override
	public void drawMySelf(Graphics g) {
		super.drawMySelf(g);
		if (filled) {
			g.fillPolygon(tPoints);
		} else {
			g.drawPolygon(tPoints);
		}
	}
}
Thanks!