hello

i am working on a simple panting project
that allow the user to select a shap from ComboBox

then draw it on the panel

but problem is when is choose any type of shapes and drawing it on panel
no shape appear on the panel ?!

here is my panel class

package painter;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Shapes extends JPanel{
      private MyLine [] lines=new MyLine[100];
      private MyRectangle []  recs;
      private MyOval [] ovals;
      int ShapeType=0;

      int LC=0;// conter for number of lines
      int RC=0;// conter for number of rectable
      int OC=0;// conter for number of ovals

      int x1;
      int y1;
      int x2;
      int y2;

      Shapes() {


      Color c=Color.BLUE;
      this.setBackground(Color.WHITE);
      this.setBounds(0,100, 300, 300);
      MouseHandler handler=new MouseHandler();
      this.addMouseListener(handler);
      this.addMouseMotionListener(handler);

      }

    public int getShapeType() {
        return ShapeType;
    }

    public void setShapeType(int ShapeType) {
        this.ShapeType = ShapeType;
    }



    private class MouseHandler extends MouseAdapter implements MouseMotionListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            x1=e.getX();
            y1=e.getX();
        System.out.println(x1+" "+x2);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            x2=e.getX();
            y2=e.getX();
            lines[LC]=new MyLine(x1,y1,x2,y2); 
            LC++;
                   System.out.println(x1+" "+x2);


        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
        @Override
        public void mouseDragged(MouseEvent e){

        }

}
    @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.red);
            if (ShapeType==0)
            {  
               for(int i=0;i<LC;i++)
                { 
                    lines[i].draw(g);
                }
            }

            if (ShapeType==1)
             {   
                 for(int i=0;i<LC;i++)
                  { 
                     lines[i].draw(g);
                  }
            }

           if (ShapeType==1)
             {   
                 for(int i=0;i<LC;i++)
                  { 
                     lines[i].draw(g);
                  }
            }
        }
   }

Line Class ......

package painter;
import java.awt.Graphics;
import java.awt.Color;

public class MyLine extends MyShape{
    MyLine(int x1,int y1 ,int x2,int y2){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
        this.c = c;

    }

    @Override

    public void draw(Graphics g) {
        g.setColor(Color.RED);
     g.drawLine(500, 200, 300, 400);       
    }



}

When you add lines etc in your mouse handler there's no way for Java to know that this will affect the way the panel is painted, so the paintComponent won't get called. After updating the Lines etc, call the repaint(); method for your drawing panel, so Java will schedule an update of the panel and call your paintComponent.

where i must use repaint() method ...
i does not take any course in graphics, thats my problem :(

it's working now
but still a problem when i drawing lines
an then choose to draw Recatngle
all lines gonna to be deleted from the panel
how i can draw many shapes on the same panel ?!
at the same time

Edited 3 Years Ago by mjbor1

In your paintComponent you need to paint all the lines and all the rectangles etc. That's one loop thru the lines array, folowed by one thru the recs array (etc). The code you posted just paints lines, and there's a variable ShapeType that makes no sense

u say i need one loop to drawing lines and other to drawing rectangles ?!

ahaaaaaa
it's done now
that's problem because of if statments :)

This question has already been answered. Start a new discussion instead.