Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>Move the timer out of paintcomponent.</li> <li>You have declared an abstract class that has a draw method that needs a <code>Graphics2D</code> object to be able to draw, it has no access to it. It also makes little sense to declare a class just to hold two values (screensize and location) if that class also does stuff like drawing.</li> <li><p>To fix the issues with 2, you can either let your <code>gladiator</code> extend <code>JComponent</code>, override its <code>paintcomponent</code> and place your <code>draw()</code> code in there and add gladiator to the panel as a component.</p></li> <li><p>You can alternatively do active rendering, which means that you get the <code>Graphic2D</code> object of your canvas (the panel in this case) and take control of the rendering yourself instead of relying on swing.</p></li> </ol> <p>Since you are working with swing I will give you a working example of what you probably intend to do. If you have more specific questions please do leave a comment.</p> <pre><code>public class Test extends JPanel { public static abstract class graphic extends JComponent { public Dimension dim = new Dimension(500, 500); private int[] loc = new int[] { 250, 250 }; @Override @Transient public Dimension getPreferredSize() { return new Dimension(500, 500); } public int[] getLoc() { return loc; } public Dimension getDim() { return dim; } } public static class gladiator extends graphic { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.green); g2d.fillArc(getLoc()[0], getLoc()[1], 100, 100, 45, 90); g2d.setColor(Color.black); g2d.fillArc((getLoc()[0] + 50 - 10), (getLoc()[1] + 50 - 10), 20, 20, 0, 360); } } public static void main(String[] args) { JFrame frame = new JFrame(); Test canvas = new Test(); gladiator gladiator = new gladiator(); canvas.add(gladiator); frame.getContentPane().add(canvas); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } } </code></pre> <p>This renders</p> <p><img src="https://i.stack.imgur.com/bVCie.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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