-2

In Java, how do I create Graphics within a class and then get the Graphics for use in an Applet?

One attempt to create this class for the graphic is as follows.

import java.lang.*;
import java.util.*;
import java.awt.*;

public class Cords{


    public static Graphics cords;
    public static int w,h,n;
    private static int xC,yC;

    public static void Paint(Graphics g)
    {
        for(xC=0;xC<=w;xC+=n){
            g.drawLine(xC,0,xC,h);
            g.drawString(""+xC,xC,11);
        }
        for(yC=0;yC<=h;yC+=n){
            g.drawLine(0,yC,w,yC);
            g.drawString(""+yC,1,yC));
        }
        cords=g.create();
    }
    public static Graphics cords(int w, int h,int n){
        return cords;
    }

Which I then attempted to use in an applet...

import java.awt.*;

import java.applet.Applet;
import javax.swing.Timer;

public class CordsTest extends Applet

    private int x,y,w,h,n;
    private Cords a;

    public void init()
    { 
        //w=getWidth();
        //h=getHeight();
        //a.cords(w,h,50);
    }

    public void paint(Graphics g){
        w=getWidth();
        h=getHeight();
        g.setColor(Color.black);
        paint(a.cords(w,h,50));

    }
}

As I am relatively new to asking questions on Stack Overflow, if there are any errors in question formatting, please have patience and if possible, let me know via comment so I can avoid these in the future. Thank you!

Matthew0898
  • 263
  • 2
  • 13
  • 1
    First of all, have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Dec 29 '14 at 01:01
  • @MadProgrammer thank you for the links. The second one describes what I am attempting to do quite well. I do have a couple of questions though. First what is the significance of the line `super.paintComponent(g);` Also, if it is true that `paint()` invokes `paintComponent()`,then why does their example work but mine did not? Does it have to do with the `super.paintComponent(g) line? (http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html) – Matthew0898 Dec 29 '14 at 01:28
  • 1
    Painting is made up of a series chained method calls, on of the jobs of `paintComponent` is to prepare the `Graphics` context before painting should be done. The bigger problem (from looking at your code) seems more to do with how you are passing a reference of the `Graphics` context. You should never maintain a reference to a `Graphics` that you did create yourself. – MadProgrammer Dec 29 '14 at 01:37
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 29 '14 at 23:00
  • @AndrewThompson Sorry for the delay in responding. I have read the attached links, and I do have a few questions about them. I'd like to clarify that the questions stated below are asked solely for clarification, and that I am not by any means advocating the usage of AWT over Swing. First off, quoting you, "Swing both builds on, and relies heavily on, classes in the AWT." This said, are there any capabilities of AWT that, to your knowledge, are not capabilities of Swing? Furthermore, if Swing is dependent upon AWT, does this mean that AWT is capable of nearly everything Swing is? Thanks! – Matthew0898 Feb 14 '15 at 23:03
  • 1
    *"This said, are there any capabilities of AWT that, to your knowledge, are not capabilities of Swing?"* No, none. But there are things in Swing that you can't use in AWT. *"Furthermore, if Swing is dependent upon AWT, does this mean that AWT is capable of nearly everything Swing is?"* No! If you had read the answer I linked above, that should have been obvious.. – Andrew Thompson Feb 14 '15 at 23:38
  • @AndrewThompson Okay, one more question, Is Swing built solely upon AWT? If the answer to this is "no", then I agree that the latter question should have likely been obvious. – Matthew0898 Feb 14 '15 at 23:44
  • *"If the answer to this is "no", then I agree that the latter question should have likely been obvious."* It would have been pretty obvious if you'd followed the link and read it! (Which is what you are supposed to do when someone offers a link around here. If you're not prepared to do that, maybe you're in the wrong place..) – Andrew Thompson Feb 14 '15 at 23:48
  • First off, I did read the link. I quoted you, which shows I read it. Near the end of your post you advised all CS teachers to teach JFrame Based applications. I read on the Javadocs that "Warning: Swing is not thread safe. For more information see Swing's Threading Policy."--[JFrame](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html). I also read that Jframe is "An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture."--[Swing Package Summery](http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html) Continued below... – Matthew0898 Feb 14 '15 at 23:57
  • @AndrewThompson Does Swing's [Threading Policy](http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html#threading) impact the development of Swing applications or applets? As stated in the above policy, "Where the impact lies, however, is in constructing and showing a Swing application. Calls to an application's main method, or methods in Applet, are not invoked on the event dispatching thread." – Matthew0898 Feb 15 '15 at 00:22
  • 1
    The advice offered there in regards to the Event Dispatch Thread (which, BTW - is an 'AWT thread') applies exactly the same to both Swing & AWT applets (`JApplet` & `Applet` - use `invokeAndWait`) as well as frames (`JFrame` & `Frame` - use `invokeLater`). – Andrew Thompson Feb 15 '15 at 01:19

1 Answers1

1

paint is called automatically by the painting system when it wants your component to be repainted.

In order to do any painting, you should pass a reference of Graphics to an instance of you painting class, for example.

Using something like...

public class Cords{


    public void paint(Graphics g, int w, int h, int n)
    {
        for(int xC = 0; xC <=w; xC += n){
            g.drawLine(xC,11,xC,h);
            g.drawString(""+xC,xC-(n/5),11);
        }
        for(int yC = 0; yC <= h; yC += n){
            g.drawLine(25,yC,w,yC);
            g.drawString(""+yC,1,yC+((n/5)/2));
        }
    }
}

In your applet, you need to create an instance of Cords and then pass it a reference of Graphics

public class CordsTest extends Applet implements ActionListener{

    private Cords cords;

    public void paint(Graphics g){
        w=getWidth();
        h=getHeight();
        g.setColor(Color.black);
        if (cords == null) {
            cords = new Cords();
        }
        cords.paint(g, w, h, 10);
    }

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details.

Honestly, unless you REALLY have to, avoid applets, start with a nice simple JFrame and JPanel

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366