confused with java graphics paint

前端 未结 3 1670
春和景丽
春和景丽 2021-01-25 22:15

i am very bigginer to java .i trying to draw a line in a jpanel (using netbean ide)..i read some articles .but problem is it draw thais line without calling it .i want to draw l

相关标签:
3条回答
  • 2021-01-25 23:06

    please add proper methods to be invoked from button listener. Also add panel where you want to draw line. This seems to be a probable duplicate of :-

    Drawing a line on a JFrame

    0 讨论(0)
  • 2021-01-25 23:11

    Here is an example to help you:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Line2D;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author David
     */
    public class Test {
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Frame");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            final LinePanel linePanel = new LinePanel();
    
            JPanel buttonPanel = new JPanel();
            JButton buttonDrawRandomLine = new JButton("Draw Random Line");
    
            final Random rand = new Random();
            buttonDrawRandomLine.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Line2D line = new Line2D.Double(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
                    linePanel.addLine(line);
                }
            });
    
            buttonPanel.add(buttonDrawRandomLine);
    
            frame.add(buttonPanel, BorderLayout.SOUTH);
    
            frame.add(linePanel, BorderLayout.CENTER);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    class LinePanel extends JPanel {
    
        ArrayList<Line2D> lines = new ArrayList<>();
    
        public void addLine(Line2D l) {
            lines.add(l);
            repaint();
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
            for (Line2D l : lines) {
                g2d.draw(l);
            }
    
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
    

    1) I would not suggest learning Swing via Netbeans Drag and Drop, first do it by hand, than use the Drag and drop to make your life easier once you understand how things work.

    2) Do not extend a JFrame (for OOP best practice) we dont extend classes unless we are adding functionality to them which they do not already support

    3) Dont override paint of JFrame unless absolutely necessary rather use JComponent like JPanel and override paintComponent

    0 讨论(0)
  • 2021-01-25 23:15

    Problem: You have no event code in your button event handler.

    1. Try creating a state variable called boolean showLine before your constructor.
    2. Initialize it to false in constructor.
    3. Modify state variable on button click.

      private void jButton1ActionPerformed(
                      java.awt.event.ActionEvent evt) {                                         
      Graphics gg = null;
      // TODO add your handling code here:
      showLine = (true) ? false : true;
      

      }

    4. Add condition checking to paint method.

      @Override public void paint(Graphics g) { Graphics2D ga = (Graphics2D)g;

      if (showLine) {
          ga.setPaint(Color.red);
          ga.drawLine(200,100,200,300);
      }
      

      }

    0 讨论(0)
提交回复
热议问题