1

The Var g2D at the bottom says that the Var can't be resolved as an error but it is defined in class MyPanel. I have tried extending class Con with class MyPanel and JFrame but that didn't work so I tried using the code that defines Var g2D under class MyPanel in class Con and I might have been getting that to work but IDK why it isn't and just need a solution.

P.S. Sorry for the bad formatting it is like my 3rd day coding Java. There are also ** and ** around Var g2D so that it is easier to pick out.

import javax.swing.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.Image;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ThisShit<MyPanel>{
    public void main(String[] args) {
        new MyFrame();

    }
    public class MyFrame extends JFrame{
        MyPanel panel;
        MyFrame(){
            
            panel = new MyPanel();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            this.add(panel);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
        
    }
    public class MyPanel extends JPanel{

        Image image;
    
        MyPanel(){
    
            image = new ImageIcon("Bob.jpg").getImage();
            this.setPreferredSize(new  Dimension(500,500));
        }
        public void paint(Graphics g)   {
    
            Graphics2D g2D = (Graphics2D) g;
            g2D.drawImage(image, 1, 1, null);
            g2D.setPaint(Color.green);
            //g2D.setStroke(new BasicStroke(7));;
            //g2D.drawLine(0, 0, 500, 500);
    
            g2D.setPaint(Color.pink);
            //g2D.drawRect(0, 0, 100, 200);
            g2D.fillRect(0, 0, 5, 5);
    
            //g2D.setPaint(Color.orange);
            //g2D.drawOval(0, 0, 100, 100);
            //g2D.fillOval(0, 0, 100, 100);
    
            g2D.setPaint(Color.red);
            //g2D.drawArc(0, 0, 100, 100, 0, 180);
            //g2D.fillArc(0, 0,  100, 100, 0, 180);
            //g2D.setPaint(Color.white);
            //g2D.fillArc(0, 0,  100, 100, 180, 180);
    
            int[] xPoints = {150,250,350};
            int[] yPoints = {300,150,300};
            g2D.setPaint(Color.yellow);
            //g2D.drawPolygon(xPoints, yPoints, 3);
            g2D.fillPolygon(xPoints, yPoints, 3);
    
            //unlike everything so far a string stating point is at the bottem lefthand courner
            //It goes in a weird order of type of font then u got to say it is a font then you can make it bold and say the font size
            //g2D.setPaint(Color.magenta);
            //g2D.setFont(new Font("Ink Free",Font.BOLD,45));
            //g2D.drawString("Ur Reading This????", 50, 50);
    
        }
    }
    public class Con extends JPanel{
        public static void main(String[] args){
            char[] ch;
        int letter = 0, space = 0, num = 0, other = 0, length;
            String simput;
            Scanner imput = new Scanner(System.in);
            simput = imput.nextLine();
            System.out.println("Enter your message: ");
        
        System.out.println(simput);
        ch = simput.toCharArray();
        length = simput.length();
 
        for (int i=0;i<length;i++){
            if(Character.isLetter(ch[i])){
                letter++;
            }
            else if(Character.isDigit(ch[i])){
                num++;
            }
            else if(Character.isSpaceChar(ch[i])){
                space++;
            }
            else {
                other++;
            }
        }
        int all = letter + num +space + other;
        System.out.println("Amount of Char: " + all);
        int num1 = 0;
        int num2 = 1;
        for (int w=0;w<all;w++){
            String first = simput.substring(num1,num2);
            if (first.equals("a")){
                **g2D**.fillRect(0,0,5,5);
        }
    }
}
    }
}```
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
  • It isn't defined in `MyPanel`. It's just a local variable in the method `paint` and not visible to the outside. Even if it were defined in `MyPanel`, you'd still have no access inside `Con`. Please have a look at [What is scope in Java](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – QBrute Nov 12 '21 at 18:03

0 Answers0