Results 1 to 7 of 7
- 12-27-2009, 03:26 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Need help with JFrame background image
Hi. I have a program where I have designed a simple house on the 700x700px screen and want to put a background image. I have the code but the code creates a NEW window so when I run the program I have the house AND the other window with the background image. I was just wondering, does anyone know how to change the code so it applies the background image to the window I am working with without creating a new frame?
Here is the code that creates the background image in a new frame:
PS: And also, in my program I am using JFrame not JPanel. Is there anyway I can still make it work? Because when I change my program to JPanel, other stuff doesnt work at all.Java Code:public class R extends JPanel{ public void paint(Graphics g) { super.paint(g); int c[]={200,200,215,230,245,260,275,290,305,320,335,350,365,380,395,410,425,440,455,470,485,500,500}; //house int d[]={300,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,300}; g.drawPolygon(c,d,23); g.setColor(Color.yellow); g.fillPolygon(c,d,23); g.setColor(Color.pink); Graphics2D g2 = (Graphics2D) g; RoundRectangle2D r = new RoundRectangle2D.Float(0, 0, 700, 700, 25, 25); RoundGradientPaint rgp = new RoundGradientPaint(50, 50, Color.white, new Point2D.Double(350, 350), Color.black); g2.setPaint(rgp); g2.fill(r); } class RoundGradientPaint implements Paint { protected Point2D point; protected Point2D mRadius; protected Color mPointColor, mBackgroundColor; public RoundGradientPaint(double x, double y, Color pointColor, Point2D radius, Color backgroundColor) { if (radius.distance(0, 0) <= 0) throw new IllegalArgumentException("Radius must be greater than 0."); point = new Point2D.Double(x, y); mPointColor = pointColor; mRadius = radius; mBackgroundColor = backgroundColor; } public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) { Point2D transformedPoint = xform.transform(point, null); Point2D transformedRadius = xform.deltaTransform(mRadius, null); return new RoundGradientContext(transformedPoint, mPointColor, transformedRadius, mBackgroundColor); } public int getTransparency() {
Thanks in advance.
-
Please see (and reply to) your animation thread. There you'll see how to put a JPanel into a JFrame.
Next, to make your image panel the background panel, just give it a layout and add your components to it. If you want added components such as JPanels to show the image through them, be sure to set their opaque property to false via setOpaque(false);
Finally, when drawing on a JPanel, be sure to override paintComponent, not paint.Last edited by Fubarable; 12-27-2009 at 03:40 AM.
- 12-27-2009, 03:52 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Hi. Thanks a lot. I will have a look at it and see how it goes.
Thank you for your help. I really apreciate it.
-
I couldn't resist the temptation to create a resizable SmileyPanel, so here it is :)
So now if I want to add components and panels to my SmileyPanel, I can easily do that like so:Java Code:import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") class SmileyPanel extends JPanel { private static final double EH_RATIO = 1.0/5; private static final double EW_RATIO = 1.0/7; private static final double EX_RATIO = 1.0/4; private static final double EY_RATIO = 1.0/5; private static final double SM_RATIO = 1.0/5; private static final int SMILE_DEGREES = 75; private static final double STROKE_RATIO = 1.0/40; protected void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.yellow); g2.fillOval(0, 0, w, h); int eyeH = (int)(h * EH_RATIO); int eyeW = (int)(w * EW_RATIO); int eyeX = (int)(w * EX_RATIO); int eyeY = (int)(h * EY_RATIO); g2.setColor(Color.black); g2.fillOval(w/2 - eyeX, eyeY, eyeW, eyeH); g2.fillOval(w/2 + eyeX - eyeW, eyeY, eyeW, eyeH); int smileW = w; int smileH = h; int smileX = 0; int smileY = 0 - (int)(h * SM_RATIO); Stroke oldStroke = g2.getStroke(); float stroke = (float)(h * STROKE_RATIO); g2.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2.drawArc(smileX, smileY, smileW, smileH, 270 - SMILE_DEGREES/2, SMILE_DEGREES); g2.setStroke(oldStroke); } private static void createAndShowUI() { JPanel sPanel = new SmileyPanel(); sPanel.setPreferredSize(new Dimension(600, 600)); JFrame frame = new JFrame("SmileyPanel"); frame.getContentPane().add(sPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Java Code:import java.awt.*; import javax.swing.*; public class SmileyPanelTest { private static final Dimension SIZE = new Dimension(600, 600); private static final String TITLE = "My Title"; private static final float TITLE_POINTS = 64; private JPanel mainPanel = new SmileyPanel(); public SmileyPanelTest() { JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER); titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, TITLE_POINTS)); titleLabel.setForeground(Color.red.darker()); JPanel btnPanel = new JPanel(new GridLayout(1, 0, 25, 0)); btnPanel.setOpaque(false); btnPanel.add(new JButton("Button 1")); btnPanel.add(new JButton("Button 2")); btnPanel.add(new JButton("Button 3")); mainPanel.setPreferredSize(SIZE); mainPanel.setOpaque(true); mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); mainPanel.setLayout(new BorderLayout(10, 10)); mainPanel.add(titleLabel, BorderLayout.NORTH); mainPanel.add(btnPanel, BorderLayout.SOUTH); } public JPanel getMainPanel() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("Background Panel"); frame.getContentPane().add(new SmileyPanelTest().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }Last edited by Fubarable; 12-27-2009 at 05:08 AM.
- 12-27-2009, 11:23 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Hi. Thanks a lot for your help. I still have cannot get it to work. When I change my program to JPanel stuff doesnt work. Sorry if I am getting a bit annoying but I have been trying to do this for a week now and it still isnt working. :confused: I know my code is a bit too long to be put here but Im still going to put it so you can have a look at it and see what do I need to do to put it into a JPanel. After running my program it creates 2 windows: one is my program and the other one is the frame with a bgimage. Have a look at my code and see what can and cant be done with it.
Java Code:import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.Graphics; import java.applet.Applet; import java.io.File; import java.math.*; import java.util.*; import java.io.FileInputStream; import java.applet.AudioClip; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.JApplet; import javax.sound.sampled.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class java_assignment extends JFrame implements MouseListener { BufferedImage image; public java_assignment(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paint(g); // Draw image centered. int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } String message=""; Graphics g; Rectangle2D whole_flag = new Rectangle2D.Float(650,655,50,30); Rectangle2D left_window = new Rectangle2D.Float(220,350,280,402); Rectangle2D right_window = new Rectangle2D.Float(420,350,480,402); Rectangle2D door = new Rectangle2D.Float(300,380,400,490); public java_assignment() { setVisible(true); setSize(700,700); addMouseListener(this); show();setResizable(false);setTitle("House of Horror"); } public void paint(Graphics g) { super.paint(g); g.drawString(message,30,50); int bb=0; //background fading effect int green = 1; /* for(int cc=0; cc<=100; cc++) { g.setColor(new Color(green,green,225)); int xx2=0,widdd=700,heiggg=1; g.fillRect(xx2,bb,widdd,heiggg); bb=bb+10; green=green+1; //background fading effect g.setColor(Color.white); } */ int c[]={200,200,215,230,245,260,275,290,305,320,335,350,365,380,395,410,425,440,455,470,485,500,500}; //house int d[]={300,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,300}; g.drawPolygon(c,d,23); g.setColor(Color.yellow); g.fillPolygon(c,d,23); g.setColor(Color.black); //house int a[]={450,495,530,170,195,240,225,250,450,465,445,400}; //roof int b[]={228,240,300,300,253,237,200,150,150,180,214,239}; g.drawPolygon(a,b,12); g.setColor(Color.red); g.fillPolygon(a,b,12); g.setColor(Color.black); //roof g.drawLine(260,220,280,232); //left roof crack g.drawLine(285,210,285,232); g.drawLine(260,230,265,232); g.drawLine(272,215,280,225); g.drawLine(255,245,285,242); g.drawLine(263,240,280,237); g.drawLine(270,255,280,249); g.drawLine(287,238,294,240); g.drawLine(277,209,278,212); //left roof crack g.drawLine(390,210,388,245); //right roof crack g.drawLine(388,250,409,249); g.drawLine(388,248,398,242); g.drawLine(403,225,394,235); g.drawLine(396,223,399,218); g.drawLine(391,255,401,260); g.drawLine(385,252,370,265); g.drawLine(385,247,375,235); g.drawLine(378,226,382,233); g.drawLine(372,249,379,250); //right roof crack g.drawLine(170,300,215,300); //left 40 large roof g.drawLine(210,300,210,280); //left 20up large roof g.drawLine(530,300,485,300); //right 40 large roof g.drawLine(490,300,490,280); //right 20up large roof g.drawLine(215,300,245,300); //---------------------------------------0.9 g.drawLine(245,300,245,280); int w = 245, h = 280,w35 = w+35; // roof for(int i = 0; w<=455; i++) { g.drawLine(w,300,w35,300); w=w+35; g.drawLine(w,300,w,280); // roof } int l[]={280,280,310,310}; //chimney int m[]={150,110,100,150}; g.drawPolygon(l,m,4); g.setColor(new Color(150,0,0)); g.fillPolygon(l,m,4); g.setColor(Color.black); //chimney int n[]={270,320,320,270}; //chimney top int o[]={112,97,92,107}; g.drawPolygon(n,o,4); g.setColor(new Color(150,150,150)); g.fillPolygon(n,o,4); g.setColor(Color.black); //chimney top g.drawLine(455,300,485,300); //---------------------------------------0.9 g.drawLine(280,150,280,110); //chimney left up line g.drawLine(310,150,310,100); //chimney right up line g.drawLine(280,120,310,120); //chimney bricks g.drawLine(280,135,310,135); //chimney brick g.drawLine(290,120,290,107); //chimney brick2 g.drawLine(300,135,300,120); //chimney brick2 g.drawLine(290,135,290,150); //chimney brick2 int v[]={305,320,335,350,365,380,395,400,400,300,300}; //door int z[]={490,510,490,510,490,510,490,497,380,380,497}; g.drawPolygon(v,z,11); g.setColor(Color.orange); g.fillPolygon(v,z,11); g.setColor(Color.black); //door g.drawLine(300,380,400,380); //door top line g.drawLine(300,497,300,380); //left door line int aa[]={320,320,340,340,320}; //top left window int ab[]={390,440,440,390,390}; g.drawPolygon(aa,ab,5); g.setColor(Color.yellow); g.fillPolygon(aa,ab,5); g.setColor(Color.black); g.drawLine(320,390,340,390); g.drawLine(320,390,320,440); //top left window int ac[]={360,360,380,380,360}; //top right window int ad[]={390,440,440,390,390}; g.drawPolygon(ac,ad,5); g.setColor(Color.yellow); g.fillPolygon(ac,ad,5); g.setColor(Color.black); g.drawLine(360,390,380,390); g.drawLine(360,390,360,440); //top right window int ae[]={320,320,340,340}; //bottom left window int af[]={509,470,470,498}; g.drawPolygon(ae,af,4); g.setColor(Color.yellow); g.fillPolygon(ae,af,4); g.setColor(Color.black); g.drawLine(320,470,340,470); g.drawLine(320,470,320,508); //bottom left window int ag[]={360,360,380,380}; //bottom right window int ah[]={497,470,470,509}; g.drawPolygon(ag,ah,4); g.setColor(Color.yellow); g.fillPolygon(ag,ah,4); g.setColor(Color.black); g.drawLine(360,470,380,470); g.drawLine(360,470,360,498); //bottom right window g.drawOval(385,450,9,9); //door handle g.drawOval(386,451,7,7); //door handle2 g.drawOval(387,452,5,5); //door handle3 int r[]={210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,280,220,210,290}; //left window flower int s[]={400,410,400,410,400,410,400,410,400,410,400,410,400,410,400,410,400,435,435,400,400}; g.drawPolygon(r,s,19); g.setColor(Color.green); g.fillPolygon(r,s,19); g.setColor(Color.black); //left window flower int ai[]={280,280,220,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290}; //left window int aj[]={402,350,350,402,410,400,410,400,410,400,410,400,410,400,410,400,410,400}; g.drawPolygon(ai,aj,17); g.setColor(Color.yellow); g.fillPolygon(ai,aj,17); g.setColor(Color.black); g.drawLine(220,390,280,390); g.drawLine(220,350,280,350); g.drawLine(220,351,280,351); g.drawLine(220,350,220,402); g.drawLine(221,350,221,402); g.drawLine(280,350,280,402); g.drawLine(281,350,281,402); //left window int t[]={410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,480,420}; //right window flower int u[]={400,410,400,410,400,410,400,410,400,410,400,410,400,410,400,410,400,435,435}; g.drawPolygon(t,u,19); g.setColor(Color.green); g.fillPolygon(t,u,19); g.setColor(Color.black); //right window flower int ak[]={480,480,420,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490}; //right window int al[]={402,350,350,402,410,400,410,400,410,400,410,400,410,400,410,400,410,400}; g.drawPolygon(ak,al,17); g.setColor(Color.yellow); g.fillPolygon(ak,al,17); g.setColor(Color.black); g.drawLine(420,390,480,390); g.drawLine(420,350,480,350); g.drawLine(420,351,480,351); g.drawLine(420,350,420,402); g.drawLine(421,350,421,402); g.drawLine(480,350,480,402); g.drawLine(481,350,481,402); //right window int p[]={200,150,200,215,230,245,260,275,290,305,320,335,350,365,380,395,410,425,440,455,470,485,500,550,500}; //fence int q[]={600,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,510,490,600}; g.drawPolygon(p,q,25); g.setColor(new Color(100,0,0)); g.fillPolygon(p,q,25); g.setColor(Color.black); //fence int fe=200; //fence vertical lines for(int i=1; fe<=500;i++) { g.drawLine(fe,510,fe,600); fe=fe+30; } //fence vertical lines // g.fillArc(640,30,50,50,50,360); for (int dots=1; dots<65; dots++) //randomly generates dots position { int xxx=(int)(Math.random()*1000%680); int yyy=(int)(Math.random()*1000%700); g.setColor(Color.white); g.fillArc(xxx,yyy+10,13,13,0,-360); } //randomly generates dots position g.setColor(Color.black); int x[]={500,520,525,545,530,535,500,530,505,560,535,555,600,610,590,585,400,550,525,550,480,440,460,570,630,620,640,610,660,620,600,660,590,640,590,650,580,585,570,590,530,525,515,505,450,435,415,425,370,360,365,310,329,270,270,220,200,210,50,120,50,90,20,80,50,100,95,120,130,250,150,150,165,175,160,180,265,160,120,165,198,180,160,190,165,190,160,150,140,150,200,200,500,550}; int y[]={510,480,490,465,450,410,440,370,370,315,285,285,295,250,200,195,240,150,145,140,80,120,40,80,200,260,340,360,350,370,470,440,540,535,555,630,590,610,590,645,610,650,620,680,610,650,610,670,610,625,605,670,610,610,645,610,625,610,675,590,590,570,490,540,320,450,250,200,150,80,160,190,180,210,200,220,235,240,285,300,340,330,360,380,370,450,460,450,440,490,600,600,600,490}; //g.setColor(Color.black); //polygon g.fillPolygon(x,y,94); Graphics2D g2 = (Graphics2D)g; //Draws bulgarian flag g.setColor(Color.white); Rectangle2D white_flag = new Rectangle2D.Float(650,660,50,10); g2.fill(white_flag); g.setColor(Color.green); Rectangle2D green_flag = new Rectangle2D.Float(650,670,50,10); g2.fill(green_flag); g.setColor(Color.red); Rectangle2D red_flag = new Rectangle2D.Float(650,680,50,10); g2.fill(red_flag); g.setColor(Color.black); //Draws bulgarian flag } public void mouseClicked(MouseEvent e) //INTERACTIVITY { int xxxx=e.getX(),yyyy=e.getY(); if(xxxx>=whole_flag.getX() && yyyy>=whole_flag.getY()){message = "You clicked on the bulgarian flag";} else if(((xxxx>=left_window.getX() && xxxx<=left_window.getX()+60) && (yyyy>=left_window.getY() && yyyy<=left_window.getY()+52))){message = "You clicked on the left window";} else if(((xxxx>=right_window.getX() && xxxx<=right_window.getX()+60) && (yyyy>=right_window.getY() && yyyy<=right_window.getY()+52))){message = "You clicked on the right window";} else if(((xxxx>=door.getX() && xxxx<=door.getX()+100) && (yyyy>=door.getY() && yyyy<=door.getY()+110))){message = "You clicked on the door";} else {message = "You clicked somewhere else";} repaint(); } public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} //INTERACTIVITY public static void main(String[] args)throws IOException { String path = "C:/Users/FiKo/Desktop/java2.jpg"; BufferedImage image = ImageIO.read(new File(path)); R contentPane = new R(image); // You'll want to be sure this component is opaque // since it is required for contentPanes. Some // LAFs may use non-opaque components. contentPane.setOpaque(true); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; // Add components. JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(contentPane); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); SwingUtilities.invokeLater(new Runnable() { public void run() { java_assignment b = new java_assignment(); b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b.startApp(); } }); } private void startApp() {playStartupSound();} private void endApp() {System.exit(0);} private void playStartupSound() { Runnable soundPlayer = new Runnable() { public void run() { try { File tadaSound = new File("C:/users/fiko/desktop/silent.wav"); //Uses Windows sound AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new FileInputStream(tadaSound)); AudioFormat audioFormat = audioInputStream.getFormat(); DataLine.Info dataLineInfo = new DataLine.Info( Clip.class, audioFormat); Clip clip = (Clip) AudioSystem.getLine(dataLineInfo); clip.open(audioInputStream); clip.start(); } catch (Exception e) {e.printStackTrace();} } }; Thread soundPlayingThread = new Thread(soundPlayer); soundPlayingThread.start(); } public void run() { } }Last edited by ProGenius; 12-27-2009 at 11:25 AM.
-
This tells us nothing about what you're doing wrong. How are you changing things? How is it not working? Where's the code attempt?
Myself, I really don't want to see your JFrame code -- I want to see your attempt at solving this problem and the problems this is causing. So on that note, ...Sorry if I am getting a bit annoying but I have been trying to do this for a week now and it still isnt working. :confused: I know my code is a bit too long to be put here but Im still going to put it so you can have a look at it and see what do I need to do to put it into a JPanel. After running my program it creates 2 windows: one is my program and the other one is the frame with a bgimage. Have a look at my code and see what can and cant be done with it.
1) Start small. You're having a problem creating a JPanel, adding things to this JPanel and putting it in a JFrame, so create a small program that tries to do just this and doesn't have all this other junk in it. This way you don't have to deal with side issues not related to this very basic problem.
2) Show us your attempt with this small program and let us know any error messages you may be having and what lines are causing these errors.
Much luck.
-
Also, I tried to compile your code just to see what the app looked like, but no go due to some class called "R" which we don't have the code for:
So what is "R" and why is it in this code?Java Code:public static void main(String[] args) throws IOException { String path = "C:/Users/FiKo/Desktop/java2.jpg"; BufferedImage image = ImageIO.read(new File(path)); R contentPane = new R(image); // ***** what is "R"?? *****
Similar Threads
-
JFrame window doesnt change background color
By Addez in forum New To JavaReplies: 7Last Post: 11-07-2009, 09:38 PM -
Background in JFrame ( GUI).
By Twister03 in forum AWT / SwingReplies: 2Last Post: 03-12-2009, 03:24 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
setting background color of JFrame form with NetBeans 6.1
By onefootswill in forum New To JavaReplies: 4Last Post: 08-12-2008, 07:02 AM -
GUI... setting my background to an image, im using a JFrame
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-24-2008, 05:29 AM


LinkBack URL
About LinkBacks


Bookmarks