Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - Calling methods to draw on JFrame

Can somebody explain to me why this isn't working? The error seems to be inside the Gen class but I think it may something to do with BoxMan. The error says cannot find symbol- variable g. I also tried putting in ints and doubles but it gives me: Required (Java.awt.Graphics) Found(int) / (double). So how can fix this? I've looked everywhere and can't find the answer. Help a beginner!

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.lang.Object.*;

       public class JFrame_Test
{
        public static void main (String [] args)
    {
         Gen Gen= new Gen (1500,1000,"A Name"); // this gives parameters for a Jframe later.
    }
}


{
     Gen (int size1, int size2, String title)
     {
     JFrame aFrame = new JFrame (title);
     aFrame.setSize(size1,size2);
     aFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     aFrame.setVisible(true);
     //aFrame.getContentPane().add(new Canvas());
     //Was trying to get it to work with a canvas
     BoxMan.paint (g); // the error pops up here.
    }
}

public class BoxMan

{
    public Graphics2D g2;
  public void paint(Graphics a ) 
  {
     g2 = (Graphics2D) g; // i even tried declaring "g" here.
     g2.drawRect (10, 10, 200, 200); 
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather then repeating what Jantomedes has already said (which is all excellent), I'm going to expand on it...

Painting in AWT and Swing is done through the paint sub system. This system makes decision about what and when to paint and calls the appropriate methods to update the components on the screen.

See Painting in AWT and Swing for more details

Graphics is an abstract concept in Java and is used to standardise the concept of painting to a variety of possible outputs, including the screen, image and printers. Apart from images, you can't create your own Graphics context, you need it to be supplied by the system

Check out Perfoming Custom Painting in Swing for details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...