Note that there are some explanatory texts on larger screens.

plurals
  1. POTransitioning to an abstract class
    text
    copied!<p>I am desperately trying to implement some things which I don't think I fully understand. I am attempting to set it up so the commented out actions can be taken (I will need to change syntax but I want to make sure I am on the right track first). </p> <p>Am I going about this the right way? Where will my drawing actions go if not in the draw method? I get lots of errors when I move it there. Thanks</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Graphics2D; import java.awt.Graphics; public class Test extends JPanel{ abstract class graphic { public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); private int[] location = new int[] {screenSize.width/2,screenSize.height/2}; } public class gladiator extends graphic { void draw() { //g2d.setColor(Color.green); //g2d.fillArc(location[0], location[1], 100, 100, 45, 90); //g2d.setColor(Color.black); //g2d.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); new Timer(200, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // setLocation((location[0]+1),location[1]); repaint(); System.out.println("repainting"); } }).start(); } public void setLocation(int x, int y){ //this.location[0] = x; //this.location[1] = y; } public static void main(String[] args){ JFrame jf=new JFrame(); jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); jf.add(new Test()); jf.pack(); jf.setVisible(true); } } </code></pre> <p>My original code:</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test extends JPanel{ private int[] location = new int[2]; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.fillArc(location[0], location[1], 100, 100, 45, 90); g.setColor(Color.black); g.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360); new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setLocation((location[0]+50),50); repaint(); System.out.println("repainting"); } }).start(); } public void setLocation(int x, int y){ this.location[0] = x; this.location[1] = y; } public static void main(String[] args){ JFrame jf=new JFrame(); jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.setPreferredSize(new Dimension(300,500)); jf.setLocation(100,100); jf.add(new Test()); jf.pack(); jf.setVisible(true); } } </code></pre> <p>Edit: Should have included this bit, first commenter was right.</p> <p>The error is cannot find symbol, referring to g2d or g, whichever. I take it to mean that drawing can only happen inside of paint components and that I will have to find a way to include all the instructions for drawing there. I want to make sure I'm just doing something fundamentally wrong, though as this is my first brush with both abstract classes and 2d drawing in java. Also, I know the location[0] etc will not work as is. Lets ignore that.</p> <p>The bottom code is what I am trying to accomplish (at least at first), but I am trying to use something similar to the top code to create multiple instances of it which can operate independently.</p>
 

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