Displaying and manipulating an BufferedImage

I have to program a game in Java for a university course. Therefore I need to display a quite big image (the game field).
I tried to display the image with an own Panel Class ImagePanel:
public class ImagePanel extends JPanel {
    private BufferedImage background;
    /** Creates a new instance of ImagePanel */
    public ImagePanel(BufferedImage image) {
        this.setBackground(image);
    public void paintComponent(Graphics graphic) {
        super.paintComponent(graphic);
        graphic.drawImage(getBackground(), 0,0,getBackground().getWidth(),getBackground().getHeight(), this);
    public Dimension getPrefferedSize() {
        return new Dimension(getBackground().getWidth(),getBackground().getHeight());
    public BufferedImage getBackground() {
        return background;
    public void setBackground(BufferedImage background) {
        this.background = background;
}I display a BufferedImage using the ImagePanel on a JScrollPane. The image is displayed, but the scrollbars doesn't appear (the Horizontal and VerticalScrollBarPolicy is set to AS_NEEDED) :
        BufferedImage map = ImageIO(UrlToFile);
        Graphics2D graphic = map.createGraphics();
        graphic.drawRenderedImage(map, null);
        imagePanel = new ImagePanel(map);
        jScrollPane1.add(imagePanel);
        jScrollPane1.getViewport().setView(imagePanel);   I also want to resize the image from time to time and put another image on the image. Can anyone tell me how to display the image, scroll and resize it? I also have to put other (small) images on the map, is graphic.DrawRenderedImage(buffImage,null) the best way to do this?

First read How to Use Scroll Panes:
http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html
You don't add your ImagePanel to the scroll pane, you set the client using the scroll pane constructor or setViewportView.
Also you need to set the preferred size of the scroll pane to be smaller than the preferred size of your ImagePanel if you want to see the scroll bars.
If you want to resize your image you can set a new preferred size for your ImagePanel, and then call ImagePanel.revalidate.
You just need to change the ImagePanel a bit so it will scale the image to its preferred size.
public class ImagePanel extends JPanel {
    private BufferedImage background;
    /** Creates a new instance of ImagePanel */
    public ImagePanel(BufferedImage image) {
        this.setBackground(image);
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
    public void paintComponent(Graphics graphic) {
        super.paintComponent(graphic);
        graphic.drawImage(getBackground(), 0,0,getPreferredSize().getWidth(),getPreferredSize().getHeight(), this);
    public BufferedImage getBackground() {
        return background;
    public void setBackground(BufferedImage background) {
        this.background = background;
}If you want to draw on top of your image you can use one of the drawImage functions in [url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html]Graphics2D.

Similar Messages

  • Messed up color display and resolution because of mini-DVI to video adapter

    Once I connected the mini-DVI to video adapter to the tv and got weird colors on my screen for a while but then it worked out, then after a while I disconnected the cable and turned my computer off. After that, everytime I turned my macbook on, the color is fine for two seconds and then it turns into a weird offset color, really bright colors, kinda sketchy and just a few colors, and a weird resolution and display obviously, but then I just connect my Mini-DVI to video adapter and disconnect it again (quickly) and the color returns to normal. How can I make the normal colors stay and not have to connect the mini-DVI to video adapater everytime I want to see the normal color display and resolution??
    please help!

    Open the Universal Access pane of System Preferences, click on Seeing, turn off the White on Black, VoiceOver, and Zoom options, and drag the Contrast slider all the way to the left. If the issue persists, create a new folder on the desktop, drag half of the items from the Library/Preferences/ folder of the item in the Finder's sidebar with the house icon into it, and narrow down the files until you've isolated the one responsible.
    (30807)

  • Dual display and VRAM usage.

    Hi, back in the days I wanted a 12" iBook but couldn't afford one, last autumn thought I decided to get a Macbook Pro, the update came and...
    .. it had 128MB vram.
    I want to use dual screen and maybe even occasional gaming, I couldn't decide if this where sufficient for my needs or not so I waited for the june update.
    The mid-07 MBP arrived and, it had 128MB vram AGAIN!
    Why oh why Apple? Oh, don't answer that, it's because you're in bed with Intel and want to force feed us useless highest end CPUs. But all I care for is the VRAM, why do I have to pay a ****-of-a-lot for a 1/11th faster CPU?
    Anyway, I tried to decide if I should "bit the sour Apple" and get the mid modell or just get the low one because that one is much more price worthy. A few days ago I ordered the low end one.
    But then I've seen on barefeats that he says that a 30" ACD display would use 128MB vram of the 256MB vram modell, are vram always splitted between displays in OS X?
    You see, if the extra 20" 1680x1050 display only use 1680x1050x(3 or 4) bytes that is ok, maybe even if it's double buffered (is it?), because then I will probably have like 100MB vram left for all my windows and for desktop usage that is probably ok. But if the vram is shared between displays and my 20" which I will use most only get 64MB vram that's quite low.
    I know I can turn of the sleeping capabilities of the MBP and shut the lid and only use one screen but that's a quite lame solution.
    I hate how Apple does this, and it seems to worry a lot of people. Some get the more expensive modell and hate that they had to do it, some doesn't, and some don't buy a machine at all .. I would probably have stayed in the last crowd.
    256MB vram can't have cost them many tens of dollars, why not have that in all modells? Why is the middle one even needed? Just make 2.4GHz CPU and option in that case.
    Anyway, does anyone know how much vram my screen will have available? Will 128MB work for me with all the regular desktop apps I will have running? Or should I really go with the 256MB one? The tricky part is that my MBP might be shipped today or similair.
    I've called Apple support three times but noone of them knows how this works so they aren't for much help.
    (Dell vostro 1500 sells here in Sweden for less than half the price of the mid MBP and comes way more balanced, you can even get a 1680x1050 screen. Sure it has 2.0GHz CPUs or whatever but I don't care about that. Where is the old "omg MHz myth"-Apple? Please understand CPU speed isn't everything. Instead of leading the MHz myth fight it ;D, start calling the CPUs T7300 and so on aswell ;D)

    Hi Johan, actually the VRAM used by OS X is likely to be quite a bit more than just your screen resolution multiplied by 3 or 4, since each open window is composited offscreen for the shadow effects and for things like zooming (Ctrl-Mousewheel) and the "Genie effect".
    I'm pretty sure the total VRAM isn't divided in half for each of the two displays though, as it will be allocated from the video driver on an as-needed basis. In practice, with both displays active and a few windows open, you should still have maybe 80-90% of your VRAM available for applications.
    Games and other VRAM-heavy applications are a special case as these will typically use as much VRAM as you can give them, and swap out to system RAM via the PCI-X bus when they need more (and unlike swapping to disk, swapping into VRAM from system RAM is still pretty fast).

  • Horizontal shifting bars on MBP 15 after disconnecting 24" LED cinema display and waking up from clamshell mode.

    I use my MBP 15 (late 2011) with 24" Apple LED Cinema display in a clamshell mode. After disconnecting external display, and opening display, it shows horizontal shifted bars, so it becomes completely unusable. Have to hard reset laptop. Any hints on how to resolve this? Thanks.

    Ok, more information for anyone that might care. The wake-up seems to be the familiar dark wake in order to go into hibernate mode. Except now, it's a death wake. Looking at the logs, I see that it happens exactly 70 minutes after sleeping, even though hibernatemode = 0 (which shouldn't hibernate at all!)
    So my power settings look like this:
    Currently in use:
    standbydelay         4200
    standby              1
    halfdim              1
    hibernatefile        /var/vm/sleepimage
    darkwakes            0
    gpuswitch            2
    disksleep            10
    sleep                10
    autopoweroffdelay    14400
    hibernatemode        0
    autopoweroff         1
    ttyskeepawake        1
    displaysleep         2
    acwake               0
    lidwake              1
    standbymode is 70 minutes, what a coincidence. I'm going to try setting it to 0 and see what that does.

  • How do I use my iPad as the display and input device for my Mac Mini?

    I plan to have the following stereo configuration:
    Integrated amplifier with USB input that supports up to 24/192 audio, the Peachtree Nova 125
    Mac Mini with USB connection to amp, wifi to the Internet, housed in the equipment cabinet with the integrated amp, no other music source.  No display or keyboard connected to the Mac Mini.
    I would like to use my iPad as the display and to control the Mac Mini to run Pandora, iTunes, Spotify, podcasts, etc.
    Anyone have success with this general configuration?

    You need a set of apps (one for iPad and another for computer) like Team viewer or Go to my PC

  • Can a Mac Pro (2.66 ghz duel core) be upgradable to support Thunderbolt displays and other devices.

    Can a Mac Pro (2.66 ghz duel core (4cores)) be upgradable to support Thunderbolt displays and other devices. Does this need to be done through the Graphics card expansion or a additional card to be inserted?

    No. There is no way to upgrade any Mac Pro older then the cylindrical 2013 Mac Pro to ThunderBolt.
    The requirements of Thunderbolt require a totally redesigned motherboard and chip set rather any plugin cards. These motherboard were never developed for older Mac Pros.

  • My iPad mini retina display wifi 16 GB is showing blank white display and is like frozen. I can't shut it down or restart. It was working perfectly till 13/4/2015 night and next day morning it showing this problem.Please help........

    My iPad mini retina display wifi 16 GB is showing blank white display and is like frozen. I can't shut it down or restart. It was working perfectly till 13/4/2015 night and next day morning it showing this problem.There is no physical damage to my iPad and is in perfect good condition. I updated the latest OS 8.3 version 3 days before this issue happened. I took it to the apple authorised service centre but to my shock they told the display and unit has to be replaced which will cost me Rs.15000/-. It's just been one year and 7 days only since i bought my iPad,i.e, just 7 days out of warranty period only.I don't find any fault with the hardware neither no physical damage is there. Need help to sort out this issue.

    My iPad mini retina display wifi 16 GB is showing blank white display and is like frozen. I can't shut it down or restart. It was working perfectly till 13/4/2015 night and next day morning it showing this problem.There is no physical damage to my iPad and is in perfect good condition. I updated the latest OS 8.3 version 3 days before this issue happened. I took it to the apple authorised service centre but to my shock they told the display and unit has to be replaced which will cost me Rs.15000/-. It's just been one year and 7 days only since i bought my iPad,i.e, just 7 days out of warranty period only.I don't find any fault with the hardware neither no physical damage is there. Need help to sort out this issue.

  • How to connect a 23" Cinema display and mac mini to an AV Receiver?

    I am trying to get my 23" Cinema display and Mac Mini to a Yamaha RX-2700 AV Receiver. I am on first name terms with the guy in Maplins because I tried to get an Xbox 360 working with this display before and gave up in the end.  The AV receiver has upscaling capability so I thought I'd try again.  I tried connecting a HDMI - DVI adapter to the HDMI Out on the receiver and a HMDI to DVI cable from the Mac Mini to the HDMI in with a optical cable for the sound.  The sound works fine but I get no display - the LED on the monitor flashes three times with a blank screen so it can't read the signal.  Another option suggested was to use a VGA Converter connected to a VGA - HDMI cable but I would like to find out if that would work before going down that route?
    I am looking for some advice about whether this type of configuation is possible with the 23" ACD and if so what do I need to buy to make it work?  I love the quality of my ACD to avoid having to replace it with a TV to get my multi media life in order.
    Many thanks

    The fifth and sixth item on this page are DisplayPort male to Mini DisplayPort female "Extension cables". Under US$6 plus shipping from a reputable seller:
    http://www.monoprice.com/products/subdepartment.asp?c_id=102&cp_id=10246#1024602

  • How to connect a cinema display and a VGA monitor to mac mini

    I bought a Macmini and want to connect to a cnema display and to a monitor that uses VGA connection. I also bought a MiniDisplayPort to VGA adapter but the MacMini only has ONE Minidisplay port.  I thought this would be more straitforward....

    I'm unsure of the issue, the Mini has both mDP and HDMI outputs, right? If so, the mDP for the Cinema Display an a HDMI to VGA adapter for the 2nd monitor.

  • I have a early 2011 Macbook pro 13 inch, I'm running Lion 10.7.4, and i want to upgrade to 10.8 Mountain Lion. Is it safe for my computer or are people experiencing problems with displays and Battery life?

    I have a early 2011 Macbook pro 13 inch, I'm running Lion 10.7.4, and i want to upgrade to 10.8 Mountain Lion. Is it safe for my computer or are people experiencing problems with displays and Battery life?
    Do Not post stuff about how Airplay doesnt work, because i dont have Apple TV and dont need to hear about it.

    If you have an external drive equal to or larger than your internal HD, clone your internal to it with either Carbon Copy Cloner or Super Duper. Test the clone to make sure it's bootable. Then upgrade either one. Try it out first.

  • I want to connect my IMAC to TV with HDMI cable. I did it before going to Dispaly and Arrangments and then mirror. Now when I go to display I dont have to option of arrangements. Olny {Display and Color} pops up. Any ideas what happend to arrengements?

    I want to connect my IMAC to TV with HDMI cable. I did it before going to Dispaly and Arrangments and then mirror. Now when I go to display I dont have to option of arrangements. Olny {Display and Color} pops up. Any ideas what happend to arrengements?

    For the "Arangements" option to appear, the external display must be turned on with the correct input selected and connected to the computer.  Then close system prefs, reopen, go to "Display" and "Arrangement" will be along the top. 

  • Is there a way to connect 1 external thunderbolt display and 1 external HDMI display to a mid 2012 MBP (non-retina display)?

    I've had a 24" Asus HDMI montitor for a while, and just got a 27" Apple thunderbolt display. I was wondering if there was a way to connect both of these to my mid-2012 Macbook Pro (non-retina). I know you can daisy-chain 2 thunderbolt displays, so I tried plugging my Asus into the thunderbolt display (using an HDMI to mini-display port adapter) and it didn't work. Suggestions??

    Mini DisplayPort displays and adapters don't work directly with a Thunderbolt display, but will if you connect a Thunderbolt disk drive (with two Thunderbolt ports) between the Thunderbolt display and the Mini DisplayPort device.

  • Two External Displays and Internal Display Over Different Connections

    Hi there.
    I'm going to be purchasing a rMBP in the very, very near future. However, I am unclear about how external displays work with this. My current display setup at work is two basic 1080p monitors that can be run over DVI, VGA or HDMI. I'd like to keep using these when I start using the rMBP but also gain the advantage of a third screen by using the internal display at the same time (so much space for activities!).
    One of the thunderbolt ports will need to be used for an Ethernet connection (there is no wireless here), so this leaves the setup needing to be one monitor run via Thunderbolt -> DVI and the other monitor run via HDMI.
    The internet in general isn't being entirely helpful with this. I'm looking for a clear answer of whether the rMBP will run the internal display and the two external displays using this setup under OS X. Some seem to believe that it won't work using different connection types, for example.
    Thanks for any help.

    Hello dljfield,
    Thanks for using Apple Support Communities.
    If you'd like to use multiple external displays with a MacBook Pro with Retina display, then please take a look at the information outlined below. A 15" MacBook Pro mid-2014 can support up to two external displays using different outputs.
    MacBook Pro (Retina, 15-inch, Mid 2014) - Technical Specifications
    OS X Yosemite: Connect multiple displays to your Mac
    Take care,
    Alex H.

  • Want Mac mini and G4 to share Display and USB keyboard and mouse

    I bought a KVM switch at a local electronics store. I got the only one that claimed Mac compatibility (PPA Int'l). I can't plug the switch into the G4 Cube. No such ports. With the adapter that accompanied my brand new Mac Mini, I can plug the KVM monitor port into the mini's monitor port, BUT that port is currently being used by the Apple DVI-to-ADC adapter I bought so I can connect my Cinema Display to the Mac mini.
    The KVM switch wants 3 ports on each computer: a VGA (monitor) port, the keyboard port, and the mouse port; but the keyboard/mouse connectors are not USB.
    As things stand--without the switch--I can shut down whatever computer I'm working on (mini or Cube), unplug the display from one, connect it to the other, power up and everything's fine. I suppose the only advantage with the KVM switch is being able to power off, switch, then power on without having to move the display plug.
    My QUESTION is, does anybody manufacture a KVM switch I can use between two Macintosh computers to share use of my USB keyboard powered from a USB port on the display, and my USB mouse powered from a USB port on the keyboard?
    Or is the answer NOT to go for the convenience of a KVM switch?
    Does it matter that the mini OS is Leopard, and the Cube OS is Tiger?
    I am heartily impressed with the mini. Setup was over in 5 minutes, and I must say it's fast. Appreciate any help from the experts. --Spot

    I had the same problem, and finally came up with two other alternative solutions.
    First, if you have a recent LCD monitor, it is fairly simple and cheap. The Mini has a digital connection for the monitor. My G4 400 MHz has both digital and analog connections. I have a Samsung 19 inch widescreen monitor that has both a digital and an analog input. I connected the G4 to the monitor using the analog input with my old cable, and connected the Mini using the digital input. The Samsung has a switch that will use either the digital or analog input - very easy.
    Now the keyboard and mouse. I got a 6 foot USB cable with the A plug on one end and B plug on the other, plugged the A plug end into the G4, and ran the cable with the B plug up near the Mini. It helps to keep it close. You already have the keyboard/mouse plugged into the Mini. You simply unplug the keyboard cable from the Mini, and plug it into the B plug from the G4. Switch the monitor from digital to analog, and you have the G4 (remember to start it up). Once you have run the cables, it takes about 10 seconds to make the switch.
    Second, and I use both ways, is to get an inexpensive wired router. I bought a Belkin 4 port router (about $30, I think), got a couple Cat 5 cables, hooked both the Mini and G4 up to the router, along with my internet connection, and have both computers available on one monitor with no physical switching involved. You will need to go to System Preferences/Sharing and turn on Personal File Sharing (there may be another change, but I can't remember it right now).
    When all is hooked up, you go to GO in the Finder Menu Bar (make sure the G4 is booted before doing this) click on either NETWORK or CONNECT TO SERVER, find your G4 and connect. It may be confusing the first few times, but it works great because you have access to both computers at once, along with your internet connection - the best of both worlds!
    I did this in January of this year, so please forgive me if I have forgotten a step somewhere. Ask me and I'll be glad to try to walk you through it. The first solution is the simplest to do, and the second in the most versatile. It's your choice.
    Hope this helps. Just don't be intimidated by it - if I can figure it out, we can get you connected. Good luck!

  • What is the  difference between list display and grid display in alv report

    Hai genious
    i am a new of the abap, i dont know the alv report, i have a small doubt, can u please tell me  what is the main difference between list display and grid display in alv report
    thanks&regards
    chinnu

    Hi ,
    1. For all practical purposes, they are the same.
    2. Some differences:
    a) from abap coding point of view,
    alv list is done with Function modules,
    alv gris can also be done with FM,
    but can also be done using OO concepts.
    b) Alv grid (using oo concept) requires
    designing the screen layout .
    Hence, in one screen, we can show more
    then one alv grid
    (we cannot show more than
    one alv list on one screen)
    c) ALV grid uses ActiveX controls
    present on the Presentation Server.
    Hence, it consumes More Memory
    on the presentation server.
    d) ALV LIST is Display Only.
    Whereas
    ALV Grid Can Be made EDITABLE for entry purpose.
    e) In alv grid, these options are possible,
    but not in alv list.
    without horizontal lines
    without vertical lines
    without cell merging during sorts
    display total lines above the entries
    I hope it helps.
    Regards,
    Mamta
    Edited by: Mamta Verma on Nov 27, 2008 6:41 AM

Maybe you are looking for

  • T410s Optimus game performanc​e / heat

    Hello, I have a 2 year old T410s.  I'm  trying to play the Diablo 3 beta.  However, after a couple minutes of in-game action the game slows down to a crawl. I have Thinkpad Fan Control running at fan state 80 (6000+ RPMs) and even so the temperature

  • Boot OS X Volume appears on TimeMachine after ML install

    Hi everybody, I just upgraded to Mac OS X Mountain Lion 10.8.2 from Lion 10.7.5 (MBP 15" mid 2010). So far, everything went really well. Today I ejected my TimeMachine Volume (External WD Elements 1.5 TB, 3,5", FileVault 2 encrypted under Lion 10.7.4

  • A period in a class name

    Is it possible to use a "." in a class name? For example: public class A.Class {} The filename would then be A.Class.java Is there away to make this work? javac seems to not like it.

  • Pages for iMac

    I have just got an imac and pages is missing from the apps... Is there another word processor on the imac? I have already purchased pages for my iphone and ipad so does this mean i have to purchase it again for the imac? i hope not.

  • Generation gap - firewire/USB

    Ok, so I just got my 8gig Nano - Woohoo! Also just found out that I can't sync it using my 3gen dock via Firewire - Boo! Here're the questions: The dock connectors on the new Nano & 3rd gen iPod are the same, correct? Can I just swap the cables and p