Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>+1 mKorbel and HFOE answers.</p> <p>Using HFOEs advice:</p> <p>Herre is an example I made:</p> <p>It uses a <code>MyOval</code> class (a class modeled to hold sufficient data to draw an oval in a ceartain color), it also uses a single <code>JPanel</code> called <code>DrawPanel</code> which contains an <code>ArrayList</code> for <code>MyOval</code> class and a method to add to the <code>ArrayList</code>.</p> <p>The <code>JPanel</code> is added o the JFrame the frame is setvisible than 2 ovals are added to the <code>DrawPanel</code> class using <code>addOval(...)</code> which will than add a new oval and to the arraylist and call repaint which in-turn will call <code>paintComponent</code> of <code>JPanel</code>.</p> <p><img src="https://i.stack.imgur.com/PbuS5.png" alt="enter image description here"></p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; class Test { public Test() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DrawPanel dp = new DrawPanel(500, 400); frame.add(dp); frame.pack(); frame.setVisible(true); dp.addOval(0, 0, dp.getWidth(), dp.getHeight(), Color.GREEN); dp.addOval(0, 0, dp.getWidth(), dp.getHeight(), Color.BLUE); //dp.addOval(5, 5, dp.getWidth(), dp.getHeight(),Color.RED);//uncomment here to see 2nd circle better } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } } class DrawPanel extends JPanel { private ArrayList&lt;MyOval&gt; ovals = new ArrayList&lt;&gt;(); private int width, height; public DrawPanel(int w, int h) { width = w; height = h; } @Override protected void paintComponent(Graphics grphcs) { super.paintComponent(grphcs); Graphics2D g2d = (Graphics2D) grphcs; g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); for (MyOval oval : ovals) { g2d.setColor(oval.getColor()); g2d.drawOval(oval.getX(), oval.getY(), oval.getWith(), oval.getHeight()); } } @Override public Dimension getPreferredSize() { return new Dimension(width, height); } void addOval(int x, int y, int w, int h, Color color) { ovals.add(new MyOval(x, y, w, h, color)); repaint(); } } class MyOval { int x, y, with, height; Color color; public MyOval(int x, int y, int with, int height, Color color) { this.x = x; this.y = y; this.with = with; this.height = height; this.color = color; } public Color getColor() { return color; } public int getHeight() { return height; } public int getWith() { return with; } public int getX() { return x; } public int getY() { return y; } } </code></pre> <p>NB yes we cant see both ovals but thats because they are at the same co-ordinates simply do:</p> <pre><code>dp.addOval(0, 0, dp.getWidth(), dp.getHeight(), Color.GREEN); //dp.addOval(0, 0, dp.getWidth(), dp.getHeight(), Color.BLUE); dp.addOval(5, 5, dp.getWidth(), dp.getHeight(), Color.RED);//uncomment here to see 2nd circle better </code></pre> <p>to see the other like so:</p> <p><img src="https://i.stack.imgur.com/FPVT0.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload