الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في برنامج رسم اشكال

بدأه m1m6 في 6 مايو 2013 · 2 رد · 604 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم 

عندي مشكلة في مشروع

وهو رسم اشكال يحددها المستخدم ومن ثم يقوم بالرسم عن طريق الماوس 

 

لكن المشكلة بعد اخذ الاحداثيات لا يظهر الشكل على الـ  panel

المشكلة في الجرافيكس 

 

كلاس Shapes 

package painter;import java.awt.Color;import javax.swing.JPanel;import java.awt.*;import java.awt.event.*;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() {                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();            repaint();        System.out.println(x1+" "+y1);        }        @Override        public void mouseReleased(MouseEvent e) {            x2=e.getX();            y2=e.getX();            lines[LC]=new MyLine(x1,y1,x2,y2);             LC++;                   System.out.println(x2+" "+y2);                   repaint();                    }        @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.draw(g);                                                      }            }                        if (ShapeType==1)             {                    for(int i=0;i<LC;i++)                  {                      lines.draw(g);                  }            }                       if (ShapeType==1)             {                    for(int i=0;i<LC;i++)                  {                      lines.draw(g);                  }            }                   }   }
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);       
    }
    
    
    
}
#2

الكود غير كامل نرجوا وضع الكود كاملا كى نتمكن من تحديد و حل المشكلة

 

وشكرا

Software Developer
Mahmoudkelany.com


 

#3
Main class // 

package painter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;

public class Painter {
    
    static JFrame window;
    static JButton btn1;
    static JButton btn2;
    static JComboBox Cb;
    static Color c;
    static Shapes panel;
    
    public static void main(String[] args) {
        // create a window and set size .
        window =new JFrame("Printer Applicaton");
        window.setSize(700,450);
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        window.setLayout(null);
        panel = new Shapes();
       
        //set Button1
        btn1=new JButton("Choose A Color");
        btn1.setBounds(10,30, 200, 25);
        window.add(btn1);
        
        //set Button 2
        btn2=new JButton("Draw");
        btn2.setBounds(250,30, 100, 25);
        window.add(btn2);
        
        //Set ComboBox
        Cb=new JComboBox();
        Cb.setBounds(400, 30, 100, 25);
        Cb.addItem("Line");
        Cb.addItem("Rectangle");
        Cb.addItem("Oval");
        window.add(Cb);
// Button1 implementaion      
        btn1.addActionListener(
                new ActionListener(){           
                 @Override
                public void actionPerformed(ActionEvent e) {
                     c=JColorChooser.showDialog(null, null, c);
                        if(c==null)
                            c=c.GRAY;
                        else 
                            ;
                        
                            }
                    }
                );
   
        Cb.addItemListener(
                new ItemListener(){
                    @Override
                    public void itemStateChanged(ItemEvent e){
                      if(e.getStateChange()==ItemEvent.SELECTED)
                        panel.setShapeType(Cb.getSelectedIndex());
                    }
                  }
                );
        window.add(panel);
        
        window.setVisible(true);
    
    }
    }

 

 

 

 

 

Shapes Class

 

package painter;import java.awt.Color;import javax.swing.JPanel;import java.awt.*;import java.awt.event.*;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() {                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();            repaint();        System.out.println(x1+" "+y1);        }        @Override        public void mouseReleased(MouseEvent e) {            x2=e.getX();            y2=e.getX();            lines[LC]=new MyLine(x1,y1,x2,y2);             LC++;                   System.out.println(x2+" "+y2);                   repaint();                    }        @Override        public void mouseEntered(MouseEvent e) {        }        @Override        public void mouseExited(MouseEvent e) {        }        @Override        public void mouseDragged(MouseEvent e){                x2=e.getX();            y2=e.getX();                   System.out.println(x2+" dragged "+y2);                   repaint();        }}    @Override        public void paintComponent(Graphics g){            super.paintComponent(g);            g.setColor(Color.red);            if (ShapeType==0)            {                 for(int i=0;i<LC;i++)                {                                         lines.draw(g);                                                      }            }                        if (ShapeType==1)             {                    for(int i=0;i<LC;i++)                  {                      lines.draw(g);                  }            }                       if (ShapeType==1)             {                    for(int i=0;i<LC;i++)                  {                      lines.draw(g);                  }            }                   }   }

 

 

 

 

MyShapes Class

 

package painter;
import java.awt.Graphics;
import java.awt.Color;
public abstract class MyShape {
    Color c;
    public abstract void draw(Graphics g);
    
    int x1=0;
    int y1=0;
    int x2=0;
    int y2=0;

    public Color getC() {
        return c;
    }

    public void setC(Color c) {
        this.c = c;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public int getX2() {
        return x2;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public int getY2() {
        return y2;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }


    
    
    
    
}

 

 

 

 

 

MyOval Class

 

package painter;

import java.awt.Color;
import java.awt.Graphics;
public class MyOval extends MyShape{

    MyOval(int x1,int y1 ,int x2,int y2 ){
     this.x1=x1;
     this.x2=x2;
     this.y1=y1;
     this.y2=y2;
     
    }

    @Override
         public void draw(Graphics g) {
        
        
        g.drawOval(this.getX1(), this.getY1(), this.getX2(), this.getY2());       
    }
    
}

 

 

 

 

 

MyRectangle class

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package painter;

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

/**
 *
 * @author mahmmoud
 */
public class MyRectangle extends MyShape{
    MyRectangle(int x1,int y1 ,int x2,int y2 ){
     this.x1=x1;
     this.x2=x2;
     this.y1=y1;
     this.y2=y2;
     
    }

    @Override
         public void draw(Graphics g) {
        
        
        g.drawOval(this.getX1(), this.getY1(), this.getX2(), this.getY2());       
    }
    
}

 

 

 

MyLine 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);       
    }
    
    
    
}

مواضيع مشابهة