So i have an image being drawn inside a void method in my main class, and every tutorial i have looked at has told me to just write 'this' in the last field of the drawImage() method.
Unfortunately, obviously since this is inside a void method, 'this' does not work.
How do I deal with this?
Is there a way to create a local ImageObserver variable?
I dont need to use it really.
Code snippets:
URL iurl = new URL("cyn.png");
Image bi = Toolkit.getDefaultToolkit().getImage(iurl);
graphics.drawImage(bi,d.width/2+10,110,128,128,iob);
i dont know what else i need to show
i imported the Image class and graphics class
How do I deal with this? Is there a way to create a local ImageObserver variable? I dont need to use it really.
If you don't need to use the image observer, you can safely pass null as argument. (It's basically only used when loading images asynchronously.)
From The Java™ Tutorials: Drawing an Image:
The observer parameter notifies the application of updates to an image that is loaded asynchronously. The observer parameter is not frequently used directly and is not needed for the BufferedImage class, so it usually is null.
Related
I know that when paint is happening an instance of the Graphics class should be created. That Graphics object (Actually Graphics2D object) is going through paint() method and all the details that should be printed or updated on the screen are stored in that object.
As I know of this process is handled by RepaintManager when user called repaint() method or when repaint is needed to the UI. So, a Graphics instance has to be created to store the information regarding the painting.
As this process is handled by RepaintManager, I thought that the Graphics instance is also initialized inside RepaintManager. But I found source code of RepaintManager recently. And couldn't find a place that a new Graphics instance is created.
So, is it really created inside RepaintMananger or anywhere else..?
Thanks..
It's created in Component.getGraphics() see the API for more information
I've got a class in which there is a method that draws rectangles on a JFrame. Furthermore I've got a few methods with different types of sorting. In those sorting methods I am calling the drawing method when a specified button is clicked. I would like my code to be cleaner, so I wanted to divide the class into one that is responsible for drawing things and the other one that does the sorting. I don't know how I can call the drawing method from outside the class. I wanted to use static, but I would have to make all the variables inside the method static. I also thought about making an inner class, but I will still have that one big class and it doesn't really help. What can I do?
You should only be drawing from within the paintComponents method of the container. So it doesn't make sense for some external code to initiate a call to draw stuff. If you want this external class to change what is drawn, it should pass a reference to an object implementing some understood interface that can be called by the paintComponents method.
I am working on a project for a CS class, so I cannot post any code, but I will try my best to describe what setup I have. The program is supposed to have 3 separate JPanels, each with an image and buttons to rotate and reset the image. A Driver class was provided that sets up the JFrame, creates a single Project object to pass around to other classes (this Project class contains the methods for rotating and combining the images), and has the main method.
What I've done is create a class that extends JPanel to setup a JPanel with the image, file name, and rotate/reset buttons. I have the constructor for this class taking in the number of the panel (to keep up with writing the image number in the panel), the image file to display, and the Project object that was created in the Driver class (to be able to access the methods for manipulating the images). I immediately call super(); and then set up the file name and image (as a JLabel) and buttons as (JButtons).
This is where my problem comes in though. I'm trying to set up the Rotate button, so I created an anonymous inner class action listener and actionPerformed method. What I planned on doing was using the Project object reference to call the rotate method on the image object, having it return a BufferedImage into a modifiedImage BufferedImage. Then remove the JLabel with the original image, add the modifiedImage as a JLabel, revalidate, and repaint. However, I cannot use the this reference or the reference to the Project object within the inner actionPerformed class.
How do I gain access to these references within the actionperformed inner class? or is my setup completely awful? Please bear with me- it's my first time working with any kind of GUI.
Make your reference to Project object final. It will solve the problem and does not make any harm as you are not going to assign it again.
I am attempting to create a custom Media Controller by duplicating the source and providing my own layout. The final component is a substitute for PolicyManager.makeNewWindow(mContext);
The PolicyManager class is an internal class and I can not seem to find a way to get a new Window. The documentation suggests new Window(mContext) but eclipse complains that it 'Cannot instantiate the type Window'. Any ideas?
Unless you use the NDK to access private implementations, you cannot actually create a Window. I have done exactly what you are trying with an embedded system, but it is not safe to do for a general purpose app (the underlying implementations have changed and will change again).
Regardless of whether you are using a SurfaceView or TextureView for the MediaPlayer, you should be able to a achieve a similar effect with a regular old transparent custom View. The separate Window shouldn't be necessary. It won't be as easy as copying Android's MediaController code, but it shouldn't be too complicated.
Also, if all you want is a different layout and not a drastic change in functionality, have a look at these two links (the second refers to the first, but it adds commentary & an example project).
I am working on a sketchpad android application. I am using a path object to draw the users input, but am having trouble retaining it. Right now whenever you navigate away from the page everything that the user draws is erased. What is the best way to retain the path object so that I can redraw the users previous input when states are changed.
Thank you.
Simplest way would be to declare the Path object using static keyword and as class variable.