Question
How do you draw an object from another class in java? I am trying to draw objects from the sun class in my DrawFrame class
How do you draw an object from another class in java?
I am trying to draw objects from the sun class in my DrawFrame class but I am getting errors. The line @Override in my DrawFrame class says "method does not override or implement a method from a supertype" and this line super.paintComponent(g); says it cannot find the symbol. How do I correct this so that the sun draws in the window?
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.Color; Sun Class code public class Sun extends ShapeComponent { private double x; private double y; private double width; private double height; public Sun(double anX, double aY, double w, double h) { x = anX; y = aY; width = w; height = h; } public void draw(Graphics2D g2) { g2.setColor (Color.orange); g2.fillArc (100,20,80,80,0,360); g2.drawLine (95,55,75,55); g2.drawLine (140,105,140,125); g2.drawLine (140,15,140,0); g2.drawLine (185,60,205,60); g2.drawLine (105,35,85,25); g2.drawLine (120,20,105,5); g2.drawLine (100,80,80,90); g2.drawLine (115,100,100,120); g2.drawLine (175,5,160,20); g2.drawLine (205,25,175,35); g2.drawLine (205,90,175,80); g2.drawLine (175,120,160,100); } public void translate(int dx, int dy) { x += dx; y += dy; } }
Code of my DrawFrame class
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.Rectangle; import java.awt.geom.Line2D; import java.awt.Polygon; import java.awt.Color; import java.awt.BasicStroke; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel;
public class DrawFrame extends JFrame {
public Sun sun;
public DrawFrame() { this.setTitle("Greeting Card"); this.setSize(480, 600); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initializeShapes();
}
public void initializeShapes() { sun = new Sun(5,6,7,5); }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; sun.draw(g2); }
public static void main(String[] args) { new DrawFrame(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started