如何在Java中实现非破坏性绘画?

发布于 2025-01-26 00:39:13 字数 2364 浏览 3 评论 0原文

我想在面板上绘制许多具有不同频率的正弦体,但是由于绘画具有破坏性,所以我无法实现这一目标。我只能看到最后一个正弦曲线绘制。

在代码中,generatesInus()方法仅称为3次,但可能被称为多次,我不想为paintComponent()方法中的每个正弦曲线调用g.filloval()方法。

有没有一种方法可以实现非破坏性绘画并看到所有具有不同频率的鼻窦药?

请参阅下面的代码:

package testdrawsinus;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestDrawSinus extends JPanel 
{
   private static double[] x;
   private static double[] y;
   private static boolean buttonClicked = false;
   private static JPanel panel = new TestDrawSinus();
   
   @Override
   public void paintComponent(Graphics g) 
   {
      super.paintComponent(g);
      g.setColor(Color.GREEN);
      if (buttonClicked)
      {          
          for (int i=0; i<x.length; i++)
          {                   
             g.fillOval((int)x[i] + panel.getWidth()/2, -1*((int)y[i]) + panel.getHeight()/2, 10, 10);                          
          }
          buttonClicked = false;
      }
   }

   private static void generateSinus(int freq)
   {
      x = new double[200];
      y = new double[200];
      for (int i=0; i<=199; i++)
      {
         x[i]= (double)i;
         y[i] = 100*Math.sin(2*Math.PI*freq*i/200);
              
      }
   }

   public static void main(String[] args) 
   {
      JFrame frame = new JFrame();  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(null);
      frame.setSize(800, 600);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      panel.setLayout(null);
      panel.setBounds(20,20, 700,400);
      panel.setBackground(Color.BLACK);
      frame.add(panel);

      JButton button1 = new JButton();
      button1.setText("plot");
      button1.setBounds(300, 500, 150, 50);
      frame.add(button1);

      button1.addActionListener(new ActionListener() 
      {
         @Override
         public void actionPerformed(ActionEvent e) 
         {                   
           buttonClicked = true; 
           generateSinus(1);
           panel.repaint();
           generateSinus(2);
           panel.repaint();
           generateSinus(3);
           panel.repaint();
         }
               
      });              
   }
}

感谢您的帮助。

I want to plot many sinusoids with different frequencies on a panel, but because painting is destructive, I cannot achieve this. I can only see the last sinusoid plotted.

In the code, the generateSinus() method is called only three times, but it might be called many times and I don't want to call g.fillOval() method for each sinusoid in the paintComponent() method.

Is there a way to achieve non-destructive painting and see all sinusioids with different frequencies?

Please see the code below:

package testdrawsinus;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestDrawSinus extends JPanel 
{
   private static double[] x;
   private static double[] y;
   private static boolean buttonClicked = false;
   private static JPanel panel = new TestDrawSinus();
   
   @Override
   public void paintComponent(Graphics g) 
   {
      super.paintComponent(g);
      g.setColor(Color.GREEN);
      if (buttonClicked)
      {          
          for (int i=0; i<x.length; i++)
          {                   
             g.fillOval((int)x[i] + panel.getWidth()/2, -1*((int)y[i]) + panel.getHeight()/2, 10, 10);                          
          }
          buttonClicked = false;
      }
   }

   private static void generateSinus(int freq)
   {
      x = new double[200];
      y = new double[200];
      for (int i=0; i<=199; i++)
      {
         x[i]= (double)i;
         y[i] = 100*Math.sin(2*Math.PI*freq*i/200);
              
      }
   }

   public static void main(String[] args) 
   {
      JFrame frame = new JFrame();  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(null);
      frame.setSize(800, 600);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      panel.setLayout(null);
      panel.setBounds(20,20, 700,400);
      panel.setBackground(Color.BLACK);
      frame.add(panel);

      JButton button1 = new JButton();
      button1.setText("plot");
      button1.setBounds(300, 500, 150, 50);
      frame.add(button1);

      button1.addActionListener(new ActionListener() 
      {
         @Override
         public void actionPerformed(ActionEvent e) 
         {                   
           buttonClicked = true; 
           generateSinus(1);
           panel.repaint();
           generateSinus(2);
           panel.repaint();
           generateSinus(3);
           panel.repaint();
         }
               
      });              
   }
}

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2025-02-02 00:39:13

您可能有多种方法可以做到这一点,一种方法是将每个系列分开为自己的型号,然后将每个模型的面板涂料都涂上,例如...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    private Color[] masterColors = new Color[]{
        Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
        Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK,
        Color.RED, Color.WHITE, Color.YELLOW
    };

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestDrawSinus sinusPane = new TestDrawSinus();

                JFrame frame = new JFrame();
                frame.add(sinusPane);

                JButton button1 = new JButton();
                button1.setText("plot");
                frame.add(button1, BorderLayout.SOUTH);

                button1.addActionListener(new ActionListener() {
                    private List<Color> colors = new ArrayList<>();
                    private int freq = 0;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                    }

                    protected Color nextColor() {
                        if (colors.isEmpty()) {
                            colors.addAll(Arrays.asList(masterColors));
                            Collections.shuffle(colors);
                        }
                        return colors.remove(0);
                    }

                });

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private Random rnd = new Random();

    private SinusPlot generateSinus(int freq, Color color) {
        double[] x = new double[200];
        double[] y = new double[200];
        for (int i = 0; i < 200; i++) {
            x[i] = (double) i;
            y[i] = 100 * Math.sin(2d * Math.PI * freq * i / 200d);
        }
        return new SinusPlot(x, y, color);
    }

    public class SinusPlot {
        private double[] x;
        private double[] y;
        private Color color;

        public SinusPlot(double[] x, double[] y, Color color) {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public int getSize() {
            return x.length;
        }

        public double getXAt(int index) {
            return x[index];
        }

        public double getYAt(int index) {
            return y[index];
        }

        public Color getColor() {
            return color;
        }

    }

    public class TestDrawSinus extends JPanel {
        private List<SinusPlot> plots = new ArrayList<>(8);

        public void addSinusPlot(SinusPlot plot) {
            plots.add(plot);
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (SinusPlot plot : plots) {
                System.out.println(plot.getColor());
                g2d.setColor(plot.getColor());
                for (int i = 0; i < plot.getSize(); i++) {
                    Ellipse2D dot = new Ellipse2D.Double(((getWidth() - plot.getSize()) / 2) + plot.getXAt(i), plot.getYAt(i) + getHeight() / 2, 10, 10);
                    g2d.fill(dot);
                }
            }
            g2d.dispose();
        }
    }
}

net/fkieo.png“ rel =” nofollow noreferrer“> 布局不会帮助您,花点时间了解布局管理系统的工作原理,它将节省大量时间和精力。请参阅在容器中放置组件以获取更多详细信息。

static不是您的朋友(尤其是在这种情况下),请努力了解如何没有它(以及何时使用它)

There are a number of ways you might do this, one way would be to seperate each series into its own model, then have the panel paint each model, for example...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    private Color[] masterColors = new Color[]{
        Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
        Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK,
        Color.RED, Color.WHITE, Color.YELLOW
    };

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestDrawSinus sinusPane = new TestDrawSinus();

                JFrame frame = new JFrame();
                frame.add(sinusPane);

                JButton button1 = new JButton();
                button1.setText("plot");
                frame.add(button1, BorderLayout.SOUTH);

                button1.addActionListener(new ActionListener() {
                    private List<Color> colors = new ArrayList<>();
                    private int freq = 0;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                        sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
                    }

                    protected Color nextColor() {
                        if (colors.isEmpty()) {
                            colors.addAll(Arrays.asList(masterColors));
                            Collections.shuffle(colors);
                        }
                        return colors.remove(0);
                    }

                });

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private Random rnd = new Random();

    private SinusPlot generateSinus(int freq, Color color) {
        double[] x = new double[200];
        double[] y = new double[200];
        for (int i = 0; i < 200; i++) {
            x[i] = (double) i;
            y[i] = 100 * Math.sin(2d * Math.PI * freq * i / 200d);
        }
        return new SinusPlot(x, y, color);
    }

    public class SinusPlot {
        private double[] x;
        private double[] y;
        private Color color;

        public SinusPlot(double[] x, double[] y, Color color) {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public int getSize() {
            return x.length;
        }

        public double getXAt(int index) {
            return x[index];
        }

        public double getYAt(int index) {
            return y[index];
        }

        public Color getColor() {
            return color;
        }

    }

    public class TestDrawSinus extends JPanel {
        private List<SinusPlot> plots = new ArrayList<>(8);

        public void addSinusPlot(SinusPlot plot) {
            plots.add(plot);
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (SinusPlot plot : plots) {
                System.out.println(plot.getColor());
                g2d.setColor(plot.getColor());
                for (int i = 0; i < plot.getSize(); i++) {
                    Ellipse2D dot = new Ellipse2D.Double(((getWidth() - plot.getSize()) / 2) + plot.getXAt(i), plot.getYAt(i) + getHeight() / 2, 10, 10);
                    g2d.fill(dot);
                }
            }
            g2d.dispose();
        }
    }
}

null layouts aren't going to help you, take the time to learn understand how the layout management system works, it will save you lot of time and effort. See Laying Out Components Within a Container for more details.

static is not your friend (especially in this context), make the effort to understand how to live without it (and when to use it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文