CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Java, graphics..

    Hey!

    I have an application that almost works, but I can't draw 2D graphics on the panel when I push the button "RITA". Why ?


    Code:
    package grafik;
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    
    public class NyRam extends JFrame implements ActionListener
    {
        private JPanel panel1 = new JPanel ();
        private JPanel panel2 = new JPanel ();
        private JPanel panel3 = new JPanel ();
        private JPanel panelen = new JPanel ();
        private JLabel etikett1 = new JLabel ("Punkt 1");
        private JLabel etikett2 = new JLabel ("Punkt 2");
        private JLabel etikett3 = new JLabel ("Punkt 3");
        private JTextField textFalt1 = new JTextField (5);
        private JTextField textFalt2 = new JTextField (5);
        private JTextField textFalt3 = new JTextField (5);
        private JTextField textFalt4 = new JTextField (5);
        private JTextField textFalt5 = new JTextField (5);
        private JTextField textFalt6 = new JTextField (5);
        private JButton knapp = new JButton ("RITA");
        private Point2D.Double punktP1;
        private Point2D.Double punktP2;
        private Point2D.Double punktP3;
        private NyPanel nyPan = new NyPanel ();
    
        public NyRam (String titel)
        {
            super (titel);
            this.setResizable (false);
            this.setLocationRelativeTo (null);
            this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            this.setSize (800, 600);
            this.setVisible (true);
    
            etikett1.setForeground (Color.WHITE);
            etikett2.setForeground (Color.WHITE);
            etikett3.setForeground (Color.WHITE);
            
            textFalt1.setBackground (Color.BLACK);
            textFalt1.setForeground (Color.WHITE);
            textFalt1.setText ("5.0");
            textFalt2.setBackground (Color.BLACK);
            textFalt2.setForeground (Color.WHITE);
            textFalt2.setText ("15.0");
            textFalt3.setBackground (Color.BLACK);
            textFalt3.setForeground (Color.WHITE);
            textFalt3.setText ("50.0");
            textFalt4.setBackground (Color.BLACK);
            textFalt4.setForeground (Color.WHITE);
            textFalt4.setText ("100.0");
            textFalt5.setBackground (Color.BLACK);
            textFalt5.setForeground (Color.WHITE);
            textFalt5.setText ("150.0");
            textFalt6.setBackground (Color.BLACK);
            textFalt6.setForeground (Color.WHITE);
            textFalt6.setText ("200.0");
    
            panel1.setLayout (new FlowLayout(FlowLayout.CENTER, 6, 2));
            panel1.add (etikett1);
            panel1.add (textFalt1);
            panel1.add (textFalt2);
            panel1.setBackground (Color.BLACK);
    
            panel2.setLayout (new FlowLayout (FlowLayout.CENTER, 6, 2));
            panel2.add (etikett2);
            panel2.add (textFalt3);
            panel2.add (textFalt4);
            panel2.setBackground (Color.BLACK);
    
            panel3.setLayout (new FlowLayout (FlowLayout.CENTER, 6, 2));
            panel3.add (etikett3);
            panel3.add (textFalt5);
            panel3.add (textFalt6);
            panel3.setBackground (Color.BLACK);
    
            knapp.addActionListener (this);
            knapp.setBackground (Color.BLACK);
            knapp.setForeground (Color.WHITE);
            
            panelen.setLayout (new GridLayout (1, 1));
            panelen.add (panel1);
            panelen.add (panel2);
            panelen.add (panel3);
            panelen.add (knapp);
            
            this.setLayout (new BorderLayout ());
            this.add (panelen, "South");
            this.add (nyPan, "North");
        }
    
        public void actionPerformed (ActionEvent e)
        {
            try
            {
                String p1x = textFalt1.getText ();
                double punkt1x = Double.parseDouble (p1x);
    
                String p1y = textFalt2.getText ();
                double punkt1y = Double.parseDouble (p1y);
    
                String p2x = textFalt3.getText ();
                double punkt2x = Double.parseDouble (p2x);
    
                String p2y = textFalt4.getText ();
                double punkt2y = Double.parseDouble (p2y);
    
                String p3x = textFalt5.getText ();
                double punkt3x = Double.parseDouble (p3x);
    
                String p3y = textFalt6.getText ();
                double punkt3y = Double.parseDouble (p3y);
    
                punktP1 = new Point2D.Double (punkt1x, punkt1y);
                punktP2 = new Point2D.Double (punkt2x, punkt2y);
                punktP3 = new Point2D.Double (punkt3x, punkt3y);
    
                nyPan = new NyPanel (punktP1, punktP2, punktP3);
                this.add (nyPan, "North");
                nyPan.repaint ();
    
            } catch (Exception g)
            {
                JOptionPane.showMessageDialog (null, "Du måste ange heltal eller flyttal i alla fälten!\n" +
                        "För varje punkt så skall x samt y anges, (x, y).\n\n Felmeddelandet: " + g, "Meddelande", JOptionPane.ERROR_MESSAGE );
            }
        }
    }


    Code:
    package grafik;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    public class NyPanel extends JPanel
    {
        private Point2D.Double p1 = new Point2D.Double ();
        private Point2D.Double p2 = new Point2D.Double ();
        private Point2D.Double p3 = new Point2D.Double ();
    
        public NyPanel (Point2D.Double p1, Point2D.Double p2, Point2D.Double p3)
        {
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
        }
    
        public NyPanel ()
        {
            
        }
    
        @Override
        public void paintComponent (Graphics gr)
        {
            super.paintComponent (gr);
            this.setBackground(Color.BLACK);
            this.setSize(800, 546);
            Graphics2D g = (Graphics2D) gr;
            g.setColor (Color.WHITE);
            
            QuadCurve2D kurvan = new QuadCurve2D.Double (this.p1.x, this.p1.y, this.p2.x, this.p2.y, this.p3.x, this.p3.y);
            Line2D.Double linjen = new Line2D.Double (this.p1, this.p3);
            Rectangle2D rektangeln = new Rectangle2D.Double (this.p1.x, this.p1.y, this.p3.x, p3.y);
    
            g.draw (kurvan);
            g.draw (linjen);
            g.draw (rektangeln);
        }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java, graphics..

    Rather than adding a new panel to the frame every time you press the button why not just reuse the existing panel.

    For example change the 3 parameter constructor in NyPanel to a method ie setValues(..) and then just call this method from the action listener.

    The problem with dynamically changing the components on a displayed screen is you have to force the container and layout manager to re-validate themselves. Sometimes this is the approach you have to take but in your case there is no reason I can see why you can't reuse the NyPanel you have already added.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Re: Java, graphics..

    I solved this problem now. On this way:

    [CODE]this.getContentPane().remove(nyPan);

    nyPan = new NyPanel (punktP1, punktP2, punktP3);
    this.add (nyPan, BorderLayout.CENTER);
    this.getContentPane().validate();

    nyPan.repaint ();[CODE]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured