Results 1 to 9 of 9
Thread: Ghost reflection in GUI
- 02-04-2010, 08:46 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Ghost reflection in GUI
I'm trying to make a simple GUI with three JPanels. In the top one, there are some labels and buttons. In the middle one, a image is drawn, and in the button one, the are some labels and JCheckBoxes.
For some reason, there's a reflection in the middle JPanel, of the last button I've touched, and of some of the labels from the top JPanel. I've attached a screen dumb of it.
Can anyone explain this, and tell me how I avoid it?
I've also experienced the same kind of reflection in a text field, in another GUI I've made. This was though a reflection from the image I draw.
- 02-05-2010, 01:13 AM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
Are you mixing AWT and Swing?
For instance does the code use Label or Button instead of JLabel or JButton?
Otherwise, you haven't given us much to go on.
-
I agree with zweibieren: you have a bug in the program you've written, and we have no way of knowing what it is based on the information that you've given us.
- 02-05-2010, 12:24 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Code
I don't think I've mixed Swing and AWT. Here's the code. I've removed a lot of the buttons, and their functionality to simplify it, but the reflection still appears...
The Mainwindow class creates all the panels, buttons, ect. And then in the middle panel the Picture class is added, which contains a paint function, that paints a picture...
Mainwindow:
Picture:Java Code:package windows; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; public class Mainwindow extends JFrame{ //top panel public JPanel total; //contains top, middle and buttom panel public JPanel top; //contains guide_pages and status_end panels public JPanel middle; // here is the picture drawn public JPanel buttom; public JPanel guide_pages; //contains JLabel and buttons panel public JPanel status_end; public JPanel buttons; public JLabel guide; public JLabel status1; public JLabel status2; public String status1Text; public String status2Text; public JButton end; public JButton start; public int yTop; private int x=1200; private int y=780; public int yMiddle; public int yButtom; public Picture picture; JScrollPane scrollpane; public Mainwindow() { //Panels yMiddle=200; yButtom=400; yTop=100; top = new JPanel(); top.setPreferredSize(new Dimension(1200,yTop)); top.setLayout(new GridBagLayout()); top.setBackground(Color.WHITE); middle = new JPanel(); middle.setLayout(new BoxLayout(middle, BoxLayout.LINE_AXIS)); middle.setPreferredSize(new Dimension(x,yMiddle)); middle.setBackground(Color.WHITE); buttom = new JPanel(); buttom.setPreferredSize(new Dimension(x,yButtom)); buttom.setBackground(Color.WHITE); // First panel i top panel - Contains Guide text and page buttons guide_pages = new JPanel(); guide_pages.setPreferredSize(new Dimension(700,yTop)); guide_pages.setLayout(new BoxLayout(guide_pages, BoxLayout.Y_AXIS)); guide_pages.setAlignmentX(LEFT_ALIGNMENT); guide_pages.setBackground(Color.WHITE); //Far right panel in top panel - Contains status text and end button status_end = new JPanel(); status_end.setPreferredSize(new Dimension(150,yTop)); status_end.setLayout(new BoxLayout(status_end, BoxLayout.Y_AXIS)); status_end.setBackground(Color.WHITE); //Contains the buttons buttons = new JPanel(); buttons.setBackground(Color.WHITE); //guide_pages Panel content //Label guide = new JLabel("Du kan bevæge dig rundt mellem opgaverne ved at trykke på numrene"); status1 = new JLabel("Status 1"); status2 = new JLabel("Status 2"); //Creating the Buttons start = new JButton("x"); end = new JButton("Afslut prøven"); //adding buttons to buttons panel. buttons.add(start); //Adding Label and panel to guide_pages. guide_pages.add(guide); guide_pages.add(buttons); // The status_end labels and button. status_end.add(status1); status_end.add(status2); status_end.add(end); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridx = 0; c.gridy = 0; top.add(guide_pages,c); c.gridx = 1; c.gridy = 0; top.add(status_end,c); picture = new Picture(this); picture.name="front"; middle.add(picture); total = new JPanel(); total.setLayout(new FlowLayout()); total.setPreferredSize(new Dimension(x,y)); total.add(top); total.add(middle); total.add(buttom); scrollpane = new JScrollPane(total); getContentPane().add(scrollpane, BorderLayout.CENTER); top.updateUI(); // Gør så programmet lukker når man trykker på X i hjørnet af vinduet addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try{ System.exit(0); } catch(NullPointerException g){ System.exit(1); } } }); //sætter vindue specifikationer this.setTitle("Geo Test"); this.setSize(x, y); this.setVisible(true); } /** * @param args */ public static void main(String[] args) { Mainwindow window = new Mainwindow(); } }
Java Code:package windows; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; /** * * @author Johannes Russell Laustsen * @version 260608 */ public class Picture extends JPanel { public BufferedImage img=null; public String name=""; private Mainwindow window; public Picture(Mainwindow window){ this.window=window; } /** * Konstrukteren opretter hashmappen som det skal være når spillet starter. */ /** * Paint metoden er den metode der kaldes, når programmet opdatere sin GUI. */ public void paint(Graphics g) { Image img = createImage(); int x = this.img.getWidth() + 20; int y = this.img.getHeight()+10; window.middle.setPreferredSize(new Dimension(x,y)); g.drawImage(img, 10,10,this); window.middle.updateUI(); } /** * * @param name navnet på det billed der skal loades * @return returnere det billed den har loaded. */ public Image createImage(){ try{ img = ImageIO.read(new File("c:\\"+this.name+".jpg")); } catch(IOException e){ System.out.println("Couldn't find picture"); } return img; } }
-
I don't have your image file and can't replicate your "ghost", but I do see some problems in your Picture class here:
1) You should not be uploading and constructing an image in the paint/paintComponent method. This method needs to be lean and fast. Create your image in your constructor.Java Code:public void paint(Graphics g) { Image img = createImage(); int x = this.img.getWidth() + 20; int y = this.img.getHeight()+10; window.middle.setPreferredSize(new Dimension(x,y)); g.drawImage(img, 10,10,this); window.middle.updateUI(); }
2) You have a BufferedImage variable in this class that you never use. Your createImage method should probably return a BufferedImage so you can use this method in your constructor to create an image for the img variable.
3) You are calling program logic within the paint/paintComponent method (the setPreferredSize method).
4) You shouldn't be calling updateUI in a Swing program. This is for AWT.
5) This is a JComponent (a JPanel to be precise), and so its paint method should not be overridden. Instead override paintComponent.
- 02-05-2010, 11:29 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Solved
Great. Thanks for the corrections...
It turned out it was the updateUI() call, that coursed some problems, but thanks for the other stuff too, I'll try to fix it all...
Again, thank you very much for your help...
-
- 02-06-2010, 04:25 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
In addition you should always have a super.paintComponent() at the start of the method. Otherwise the entire background will not be repainting and you may see painting artifacts.5) This is a JComponent (a JPanel to be precise), and so its paint method should not be overridden. Instead override paintComponent.
-
Similar Threads
-
how to use reflection
By sunilpatel28 in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 10:51 PM -
n00b : Ghost image while using mouseDragged event to move a filled jPanel
By ankitmcgill in forum New To JavaReplies: 5Last Post: 11-02-2008, 06:41 AM -
Problem in Reflection API
By rnd in forum New To JavaReplies: 5Last Post: 08-19-2008, 12:44 PM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks