0 Members and 1 Guest are viewing this topic.
import java.awt.Component;public class Game{ public static void main(String[] args) { MainFrame mainFrame = new MainFrame(); MainGraphics mainGraphics = new MainGraphics(); mainFrame.getContentPane().add(mainGraphics); }}
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Cannot instantiate the type MainGraphics The method add(Component) in the type Container is not applicable for the arguments (MainGraphics)
Component mainGraphics = new MainGraphics();
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from MainGraphics to Component Cannot instantiate the type MainGraphics
import java.awt.Graphics2D;public abstract class MainGraphics extends Graphics2D { public MainGraphics() { this.create(); this.drawLine(5, 5, 10, 10); }}
public class Test extends JPanel{ public Test(){ JFrame frame = new JFrame("test"); frame.getContentPane().add(this); frame.setSize(300, 300); frame.setVisible(true); } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawLine(5, 5, 10, 10); } public static void main(String[] args){ new Test(); }}
public class Runner{ public static void main(String[] args){ new Game().run(); }}
public class Game extends JPanel{ public Game(){ //set up JFrame } public void run(){ //show JFrame } public void paintComponent(Graphics g){ //paint things here }}
public class Runner{ public static void main(String[] args) { new Game().run(); }}
import java.awt.Graphics2D;import javax.swing.JFrame;import javax.swing.JPanel;public class Game extends JPanel{ JFrame mainFrame = new JFrame("Game"); public Game() { mainFrame.setSize(800, 600); } public void run() { mainFrame.setVisible(true); } public void paintComponent(Graphics2D g) { super.paintComponent(g); g.drawLine(5, 5, 10, 10); }}
mainFrame.getContentPane().add(this);
the exact some code is working on my computer. are you sure you saved/recompiled before you ran the code? also, what's the project you're working on?
import java.awt.Graphics2D;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Game extends JPanel{ JFrame mainFrame = new JFrame("Game"); public Game() { mainFrame.setSize(800, 600); JButton myButton = new JButton("hello"); mainFrame.getContentPane().add(this); this.add(myButton); } public void run() { mainFrame.setVisible(true); } public void paintComponent(Graphics2D g) { super.paintComponent(g); g.fillArc(5, 5, 100, 100, 45, 45); g.drawLine(5, 5, 10, 10); }}
Again, you cannot add Graphics to the panel. JPanels have a Graphics instance variable already. i just noticed your method header for paintComponent is incorrect. it's parameter should be of type Graphics, not Graphics2D.