Results 1 to 10 of 10
- 12-04-2007, 10:51 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Problem going outside paintComponent
I got two classes which can draw a scene/canvas on which I can paint stuff like circles, triangles etc.
If I draw a shape inside the paintComponent and then fill it, it works fine.
If I try to do the same thing from outside of the paintCompoment, using the same methods, it gives a nullPointerException.
The two classes:
Scene.java
Java Code:// Created by Thez // Created on 04 December 2007, 18:48 package crossfire; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.Shape; import javax.swing.JPanel; public class Scene extends javax.swing.JPanel { private Graphics2D theScene; //this overrides the original method to allow for modification public void paintComponent(Graphics renderScene){ super.paintComponent(renderScene); theScene = (Graphics2D)renderScene; //Shape test = createRectangle(20,20,200,100); //fillShape(theShape,new Color(200,120,0)); System.out.println("test"); } //makes a fully filled drawing of the shape given public void fillShape(Shape theShape, Color theColor){ theScene.setColor(theColor); theScene.fill(theShape); } //makes an outline drawing of the shape given public void drawShape(Shape theShape, Color theColor){ theScene.setColor(theColor); theScene.draw(theShape); } //this creates a rectangle public Shape createRectangle(int offsetX, int offsetY, int shapeWidth, int shapeHeight){ Shape newShape = null; //set the size and position int[] xPoints = {offsetX, offsetX + shapeWidth, offsetX + shapeWidth, offsetX}; int[] yPoints = {offsetY, offsetY, offsetY + shapeHeight, offsetY + shapeHeight}; //a rectangle always has 4 points/corners Polygon theRectangle = new Polygon(xPoints,yPoints,4); //make a shape out of it newShape = theRectangle; return newShape; } }
Java Code://Created on 04-12-2007 18:26 //Created by Thez package crossfire; import java.awt.Color; import java.awt.Shape; import javax.swing.JFrame; public class Main { private Window theWindow; private JFrame theFrame; private Scene theSceneGenerator; //load the window, note the dimensions are still needed if made fullscreen, in case the user minimizes public Main(){ theWindow = new Window(false,true,800,600,"Test"); loadSceneIntoFrame(); } //startup public static void main(String[] args) { new Main(); } //this loads the scene class and puts it into the frame public void loadSceneIntoFrame(){ //first we get the JFrame theFrame = theWindow.getFrame(); theSceneGenerator = new Scene(); theFrame.getContentPane().add(theSceneGenerator); Shape test = theSceneGenerator.createRectangle(20,20,200,100); theSceneGenerator.fillShape(test,new Color(200,120,0)); } }
All I need is to able to draw and remove shapes from the scene/canvas outside the paintComponent.
Any ideas?
- 12-05-2007, 12:47 AM #2
Using a non–live (eg, member variable) graphics context reference will generally result in a NullPointerException. The way to draw in java is to set up your graphic component to be able to draw the current state of its enclosing class at any time and communicate with it from your event code to change its state and tell it to repaint itself to reflect the changes. All drawing should be placed inside or called from the relevant painting method, here, paintComponent. More like this:
Java Code:import java.awt.*; import javax.swing.*; public class MainScene { private JFrame theFrame; private Scene theSceneGenerator; public MainScene(){ theFrame = new JFrame(); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theFrame.setSize(400,400); theFrame.setLocation(200,200); theFrame.setVisible(true); loadSceneIntoFrame(); } private void loadSceneIntoFrame(){ theSceneGenerator = new Scene(); theFrame.getContentPane().add(theSceneGenerator); // Tell the frame to make a new layout/inventory // of its children when making changes after // realization. theFrame.validate(); // Sometimes you need this next line, sometimes not; // you have to try for the minimum in each case. // theFrame.repaint(); Shape test = createRectangle(20,20,200,100); theSceneGenerator.setFill(true); theSceneGenerator.setFillColor(new Color(200,120,0)); theSceneGenerator.setShape(test); } private Shape createRectangle(int offsetX, int offsetY, int shapeWidth, int shapeHeight){ //set the size and position int[] xPoints = {offsetX, offsetX + shapeWidth, offsetX + shapeWidth, offsetX}; int[] yPoints = {offsetY, offsetY, offsetY + shapeHeight, offsetY + shapeHeight}; //a rectangle always has 4 points/corners Polygon polygon = new Polygon(xPoints,yPoints,4); Rectangle rect = new Rectangle(offsetX, offsetY, shapeWidth, shapeHeight); // Both Polygon and Rectangle implement the Shape // interface so you can return either one okay. return rect; //polygon; } public static void main(String[] args) { new MainScene(); } } class Scene extends JPanel { private boolean fill = false; Color fillColor; Shape theShape; protected void paintComponent(Graphics renderScene){ super.paintComponent(renderScene); Graphics2D g2 = (Graphics2D)renderScene; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if(fill) { g2.setPaint(fillColor); g2.fill(theShape); } g2.setPaint(Color.blue); g2.draw(theShape); System.out.println("test"); } public void setFill(boolean fill){ this.fill = fill; repaint(); } public void setFillColor(Color color) { fillColor = color; repaint(); } public void setShape(Shape theShape){ this.theShape = theShape; repaint(); } }
- 12-05-2007, 01:05 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Thanks, I'll sift through it all, but one thing, how can I create multiple shapes in the same scene?
When I create another shape and fill/draw it, the first one is gone.
- 12-05-2007, 01:30 AM #4
Send in an array of shapes or a list:
Java Code:Shape[] shapes = new Shape[1]; shapes[0] = rect; List<Shape> list = new ArrayList<Shape>(); list.add(rect);
- 12-05-2007, 12:15 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Ah, thanks, I got it working it now.
One thing, why is the inner class (Scene) protected and not private or public?
- 12-05-2007, 07:14 PM #6
why is the inner class (Scene) protected and not private or public?
I didn't see that Scene was an inner class in your original post.
- 12-07-2007, 01:36 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Another problem now, using images by classpath.
I use the Toolkit.getImage() and it will work fine if I use a full path (e.g ""C:\Documents\Thez\test.jpg") but if I try to use a classpath it gives an 'Uncaught error fetching image: NullPointerException'.
My project has two packages, 'core' and 'images'.
I tried with a simple JLabel from NetBeans and it uses 'getClass().getResource("/images/test.jpg")' and the label shows the image just fine.
If I try Toolkit.getImage(getClass().getResource("/images/test.jpg")); it gives the error.
Any idea what I'm doing wrong?
- 12-07-2007, 06:38 PM #8
You'll have to experiment with the path until you get it figured out. If the class file is in folder "core" and the image files are in folder "images" and both folders are on the same level in their parent directory/folder "Thez" you could try
../images/test.jpg
in a File constructor. The "../" part moves you up to the parent folder.
The getResource method requires that the resource be in/on the classpath which means that
C:\Documents\Thez
should be part of your classpath environment variable. You can check this environment variable with
set classpath
at the prompt.
If you do have the images in/on the class path then you can experiment with
Java Code:String path_to_try = ... URL url = getClass().getReaource(path_to_try); System.out.println("url = " + url.getPath());
- 12-08-2007, 03:12 PM #9
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Okay I got that working as well now, but I got another weird problem.
If I draw an animated gif over a shape (say a red square), it seems to flicker a bit, parts of the red square pop in and out of it while it animates.
It does animate properly though, but bits of the shape behind it flicker through it randomly.
Maybe there is some kind of z-index setting like in CSS that is needed?
- 12-08-2007, 05:59 PM #10
Similar Threads
-
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 11:36 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:43 AM
Bookmarks