Results 1 to 3 of 3
  1. #1
    BobTedmen is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Need help with project (new to programming and java)

    for a programming project i haft to be able to draw two rectangles and show only the two most recent ones (i also haft to detect if they over lap and show it but i haven't made an attempt at this yet so if you want to go into it i would be great full , however it isn't necessary for you to do so). so far my code works off and on and with drawing one rectangle but not two i also haven't started with drawing the most recent, only two then a repaint. as of now my code is really bloated if there is a more efficient way if doing this (ie. arrays for holding the coordinates ( i havent had time to fiddle with them)) i would be great full if you showed me. for this project i used net beans 6.8. thanks for your help, i am new to this form so if i posted this in the wrong place please let me know

    ps. i know i have horrible spelling

    Java Code:
    /*
     * add info
     */
    
    /*
     * OverLap.java
     *
     * Created on Apr 11, 2010, 5:39:20 PM
     */
    
    package overlapdector;
    
    import java.awt.Graphics;
    
    
    
    
    public class OverLap extends javax.swing.JFrame {
    
        public static int x,y;
    
    
        rectangle areff= new rectangle();
        rectangle test;
    
        
        public OverLap() {
            initComponents();
            initApp();
        }
    
         public void paintComponent(rectangle i) {
    
        Graphics gDisplay =jPDisplay.getGraphics();
    
    
        
        gDisplay.drawRect(i.getFirstXC(), i.getFirstYC(), i.getSecondXC()-i.getFirstXC()
                         ,i.getSecondYC()-i.getFirstYC());
       
    
        
        gDisplay.drawRect(i.getFirstXC2(), i.getFirstYC2(), i.getSecondXC2()-i.getFirstXC2()
                         ,i.getSecondYC2()-i.getFirstYC2());
        
    
        if(i.getNumRectangle()==2){
            repaint();
            test=new rectangle(0,0,0,0,0,0,0,0,0);
    
        }
    
      }
    
    
        
    
        
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jPDisplay = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPDisplay.setBorder(javax.swing.BorderFactory.createEtchedBorder());
            jPDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jPDisplayMouseClicked(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout jPDisplayLayout = new org.jdesktop.layout.GroupLayout(jPDisplay);
            jPDisplay.setLayout(jPDisplayLayout);
            jPDisplayLayout.setHorizontalGroup(
                jPDisplayLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 247, Short.MAX_VALUE)
            );
            jPDisplayLayout.setVerticalGroup(
                jPDisplayLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 216, Short.MAX_VALUE)
            );
    
            jButton1.setText("Reset");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Exit");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(37, 37, 37)
                    .add(jPDisplay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jButton1)
                        .add(jButton2))
                    .addContainerGap(36, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jPDisplay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(jButton1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jButton2)))
                    .add(60, 60, 60))
            );
    
            pack();
        }// </editor-fold>
    
        private void jPDisplayMouseClicked(java.awt.event.MouseEvent evt) {                                       
             x=evt.getX();
             y=evt.getY();
             rectangle.firstRectangle();
             rectangle.secondRectangle();
             paintComponent(areff);
        }                                      
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            initApp();
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.exit(0);
        }                                        
    
        private void initApp(){
            repaint();
            test=new rectangle(0,0,0,0,0,0,0,0,0);
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new OverLap().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JPanel jPDisplay;
        // End of variables declaration
    
    }
    Java Code:
    /*
     * rectangle class
     */
    
    package overlapdector;
    
    
    
    public class rectangle {
    
    
        public rectangle(int x1,int y1,int x2,int y2,
                int x12,int y12,int x22,int y22, int num){
            setFirstXC(x1);
            setFirstYC(y1);
    
            setSecondXC(x2);
            setSecondYC(y2);
    
            setFirstXC2(x12);
            setFirstYC2(y12);
    
            setSecondXC2(x22);
            setSecondYC2(y22);
    
            setNumRectangle(num);
        }//end full pramiter constructor
        public rectangle(int num){
            setNumRectangle(num);
        }
        
        public rectangle(){
            this(DEFAULT,DEFAULT,DEFAULT,DEFAULT,
                    DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT);
        }//end nopramiter constructor
    
        private static final int DEFAULT=0;
    
        private static int firstXC ;
        private static int firstYC ;
        private static int secondXC ;
        private static int secondYC ;
    
        private static int firstXC2 ;
        private static int firstYC2 ;
        private static int secondXC2 ;
        private static int secondYC2 ;
    
        private static boolean isClicked = false;
        private static boolean isClicked2 = false;
        private static int numRectangle=0;
    
        //gets and sets
    
        public int getNumRectangle(){
            return numRectangle;
        }
        public void setNumRectangle(int myNumRectangle){
            numRectangle=myNumRectangle;
        }
        public int getFirstXC(){
            return firstXC;
        }
        public void setFirstXC(int myfirstXC){
            firstXC=myfirstXC;
        }
         public int getFirstYC(){
            return firstYC;
        }
        public void setFirstYC(int myfirstYC){
            firstYC=myfirstYC;
        }
        public int getSecondXC(){
            return secondXC;
        }
        public void setSecondXC(int mysecondXC){
            secondXC=mysecondXC;
        }
         public int getSecondYC(){
            return secondYC;
        }
        public void setSecondYC(int mysecondYC){
            secondYC=mysecondYC;
        }
    
        public int getFirstXC2(){
            return firstXC2;
        }
        public void setFirstXC2(int myfirstXC2){
            firstXC2=myfirstXC2;
        }
         public int getFirstYC2(){
            return firstYC2;
        }
        public void setFirstYC2(int myfirstYC2){
            firstYC2=myfirstYC2;
        }
        public int getSecondXC2(){
            return secondXC2;
        }
        public void setSecondXC2(int mysecondXC2){
            secondXC2=mysecondXC2;
        }
         public int getSecondYC2(){
            return secondYC2;
        }
        public void setSecondYC2(int mysecondYC2){
            secondYC2=mysecondYC2;
        }
    
        //end gets and sets
    
    
    
                    public static void firstRectangle() {
    		 if(numRectangle==0){
                        if(!isClicked) {
    				firstXC = OverLap.x;
    				firstYC = OverLap.y;
    				isClicked = true;
    
    			}
    			else {
    				secondXC = OverLap.x;
    				secondYC = OverLap.y;
    
    			}
    			numRectangle =1;
                     }
    		}
                    public static void secondRectangle(){
    
                        if(numRectangle==1){
                         if(!isClicked2) {
    				firstXC2 = OverLap.x;
    				firstYC2 = OverLap.y;
    				isClicked2 = true;
    
    			}
    			else {
    				secondXC2 = OverLap.x;
    				secondYC2 = OverLap.y;
    
    			}
    			numRectangle =2;
                    }
                    }
    
    
    }
    Last edited by Eranga; 04-12-2010 at 05:55 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when you posting next time. Unformated codes are really hard to read. If you don't know how to added code tags, check on my signature. You can find a link to relevant page.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You have problems here. Please see the // !! comments:
    Java Code:
       public void paintComponent(rectangle i) {
    
          // !! do not use getGraphics to get a workable Graphics object.  The object obtained won't persist.  
          // !! so this entire method needs to go and be replaced by the paintComponent method below
          Graphics gDisplay = jPDisplay.getGraphics();
    
          gDisplay.drawRect(i.getFirstXC(), i.getFirstYC(), i.getSecondXC()
                   - i.getFirstXC(), i.getSecondYC() - i.getFirstYC());
    
          gDisplay.drawRect(i.getFirstXC2(), i.getFirstYC2(), i.getSecondXC2()
                   - i.getFirstXC2(), i.getSecondYC2() - i.getFirstYC2());
    
          if (i.getNumRectangle() == 2) {
             repaint();
             test = new rectangle(0, 0, 0, 0, 0, 0, 0, 0, 0);
    
          }
    
       }
    
       // !! do your drawing in the display panel's paint component method
       private void initComponents() {
    
          jPDisplay = new javax.swing.JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                
                // do all your drawing here!
             }
          };
          jButton1 = new javax.swing.JButton();
          jButton2 = new javax.swing.JButton();

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2009, 11:57 PM
  2. Replies: 1
    Last Post: 05-31-2009, 07:48 PM
  3. Replies: 1
    Last Post: 05-31-2009, 03:17 PM
  4. Replies: 16
    Last Post: 05-28-2009, 04:41 PM
  5. Java programming help
    By mukul9999 in forum New To Java
    Replies: 2
    Last Post: 04-30-2009, 02:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •