Rysowanie kwadratu lub kola przyciski Applet

0

Witam wszystkich

Chcialbym napisac program ktory daje uzytkownikowi do wyboru co chce narysowac. Najpierw coordynaty a pozniej 2 przyciski kwadrat lub kolo ale nie do konca mi to wychodzi. Moglby ktos pomoc.

Pozdrawiam.

/*demonstrating drawRect */

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;

public class RactangleOval extends Applet implements ActionListener
{
	int x=0;
	int y=0;
	int wdth;
	int height;
	Label label1, label2,label3,label4, resultlabel;

	//declare textfields
	TextField number1, number2,number3,number4, resultnumber;
	Button drawButton = new Button("Draw Oval");
	Button drawButton1 = new Button("Draw Rectangle");



	public void init()
		{
			//create labels
			label1 = new Label("Enter possition X on screen:");
			label2 = new Label("Enter possition Y on screen:");
			label3 = new Label("Enter width co-ordinate:");
			label4 = new Label("Enter hight co-ordinate:");





			//create textfields
			number1 = new TextField(10);
			number2 = new TextField(10);
			number3 = new TextField(10);
			number4 = new TextField(10);

			Button drawButton = new Button("Draw Oval");
			Button drawButton1 = new Button("Draw Rectangle");
			//add labels and textfields to applet
			add(label1);
			add(number1);

			add(label2);
			add(number2);

			add(label3);
			add(number3);

			add(label4);
			add(number4);
			add(drawButton);
			drawButton.addActionListener(this);
       		//add(resultnumber);
       		add(drawButton1);
       		drawButton1.addActionListener(this);
		}





public void actionPerformed(ActionEvent e)
	{
	x = Integer.parseInt(number1.getText());
	y = Integer.parseInt(number2.getText());
	wdth = Integer.parseInt(number3.getText());
	height = Integer.parseInt(number4.getText());
	repaint();
	}


	public void paint(Graphics g)
	{
	//g.clearOval(getX(), getY(), getWidth(), getHeight());
	g.drawOval(x,y,wdth,height);

	g.clearRect(getX(), getY(), getWidth(), getHeight());
	g.drawRect(x,y,wdth,height);
	}

}
0

Dodaj do okna apletu panel (lub Canvas) i na nim rysuj.

0
bogdans napisał(a):

Dodaj do okna apletu panel (lub Canvas) i na nim rysuj.

Jeszcze paneli nie bralem, czytalem na ich temat ale nie potrafie jeszcze tego zastosowac

1
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
 
public class RectangleOval extends Applet implements ActionListener
{
    int x = 0;
    int y = 0;
    int width;
    int height; 
    //declare textfields
    TextField number1, number2,number3,number4;
    Button drawButton = new Button("Draw Oval");
    Button drawButton1 = new Button("Draw Rectangle");
    int kindOfDraw = 0;
    Panel drawPanel = new Draw();
 
    public void init()
    {
            //create labels
        setLayout(new BorderLayout(2,2));    
        Label label1 = new Label("Enter possition X on screen:");
        Label label2 = new Label("Enter possition Y on screen:");
        Label label3 = new Label("Enter width co-ordinate:");
        Label label4 = new Label("Enter hight co-ordinate:");
        
        Panel controlPanel = new Panel();
        controlPanel.setLayout(new GridLayout(5,2));
 
 
 
 
 
            //create textfields
        number1 = new TextField(10);
        number2 = new TextField(10);
        number3 = new TextField(10);
        number4 = new TextField(10);

            //add labels and textfields to applet
        controlPanel.add(label1);
        controlPanel.add(number1);
 
        controlPanel.add(label2);
        controlPanel.add(number2);
 
        controlPanel.add(label3);
        controlPanel.add(number3);
 
        controlPanel.add(label4);
        controlPanel.add(number4);
        controlPanel.add(drawButton);
        drawButton.addActionListener(this);
               
        controlPanel.add(drawButton1);
        drawButton1.addActionListener(this);
        add(controlPanel,BorderLayout.NORTH);
       
        drawPanel.setSize(400,400);
        add(drawPanel,BorderLayout.CENTER);
        
    }
    public void actionPerformed(ActionEvent e)
    {
        Object src = e.getSource();
        if(src == drawButton)
        {
            kindOfDraw = 1;
        }
        if(src == drawButton1)
        {
            kindOfDraw = 2;
        }        
        x = Integer.parseInt(number1.getText());
        y = Integer.parseInt(number2.getText());
        width = Integer.parseInt(number3.getText());
        height = Integer.parseInt(number4.getText());
        drawPanel.repaint();
    }    
    class Draw extends Panel
    {
 
        public void paint(Graphics g)
        {
            super.paint(g);
            if(kindOfDraw == 1)
            {
                g.drawOval(x,y,width,height);
            }
            if(kindOfDraw == 2)
            {        
                g.drawRect(x,y,width,height);
            }
        }
    }
 
}

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.