Results 1 to 3 of 3
  1. #1
    mcstelios is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Problem with repaint() flickering

    hello!I am trying to make a program to java and i have the common problem with flickering. I am very begginer to swing and paint and repaint methods! I have try many things to eliminate but all the same. while the oval that i paint is moving the japplet is flickering. i need your help to solve this problem. here is my code:

    import java.awt.Color;



    public class all extends JApplet implements Runnable {

    double x=0;
    double y=0;
    int m=0;
    int n=0;
    int f=30;
    int μ=0;
    Thread kinisi;
    JPanel panel;
    JFrame frame;
    private boolean running = false;
    private JTextField textField1;
    private JTextField textField2;
    Image backGround;
    JPanel panel_3;
    Image bf = createImage(m, n);
    private Graphics doubleg;
    private Image i;

    public void init() {
    this.setSize(800, 700);

    }

    public all() {
    getContentPane().setLayout(null);


    JButton btnNewButton = new JButton("Start");
    btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    String b=textField2.getText();
    String z =textField1.getText();
    if (textField1.getText().equals("") || textField2.getText().equals("")){
    JOptionPane.showMessageDialog(new JFrame(),"Δωστε τιμή για το φ και το μ!","ERROR",JOptionPane.ERROR_MESSAGE);

    }
    else{
    f = Integer.parseInt(b);
    μ = Integer.parseInt(z);
    Start();
    }
    }
    });
    btnNewButton.setBounds(469, 168, 89, 23);
    getContentPane().add(btnNewButton);

    JButton btnStop = new JButton("Pause");
    btnStop.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    pause();
    }
    });
    btnStop.setBounds(588, 168, 89, 23);
    getContentPane().add(btnStop);

    JButton btnReset = new JButton("Reset");
    btnReset.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    Reset();
    }
    });
    btnReset.setBounds(701, 168, 89, 23);
    getContentPane().add(btnReset);

    JLabel label = new JLabel("\u03BC");
    label.setHorizontalAlignment(SwingConstants.CENTER );
    label.setBounds(549, 63, 46, 14);
    getContentPane().add(label);

    textField1 = new JTextField();
    textField1.setBounds(529, 101, 86, 20);
    getContentPane().add(textField1);
    textField1.setColumns(10);

    JLabel label_1 = new JLabel("\u03C6");
    label_1.setHorizontalAlignment(SwingConstants.CENT ER);
    label_1.setBounds(681, 63, 46, 14);
    getContentPane().add(label_1);

    textField2 = new JTextField();
    textField2.setBounds(667, 101, 86, 20);
    getContentPane().add(textField2);
    textField2.setColumns(10);

    JButton btnNewButton_1 = new JButton("");
    btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    JOptionPane.showMessageDialog(new JFrame(),"Οδηγίες προγράμματος","Οδηγίες",JOptionPane.INFORMATION_ME SSAGE);

    }
    });
    btnNewButton_1.setIcon(new ImageIcon(all.class.getResource("/Images/info.png")));
    btnNewButton_1.setBounds(732, 5, 39, 35);
    getContentPane().add(btnNewButton_1);

    JLabel label_2 = new JLabel("");
    label_2.setIcon(new ImageIcon(all.class.getResource("/Images/earth.jpg")));
    label_2.setBounds(-20, 0, 820, 361);
    getContentPane().add(label_2);

    JPanel panel_1 = new JPanel();
    panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel_1.setBounds(10, 372, 369, 290);
    getContentPane().add(panel_1);

    JPanel panel_2 = new JPanel();
    panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel_2.setBounds(408, 372, 369, 290);
    getContentPane().add(panel_2);
    }

    public void paint(Graphics g){
    super.paint(g);
    g.drawLine(0,f,350,200);
    g.drawLine(0,200,350,200);
    g.drawOval(m,n,40,40);
    Color brown=new Color(137,66,0);
    g.setColor(brown);
    g.fillOval(m, n, 40, 40);
    //Graphics2D g2d = (Graphics2D) g;
    g.drawLine(460,400,460,650);
    g.drawLine(20, 620, 350, 620);
    g.drawLine(50,400,50,650);
    g.drawLine(430, 620, 760, 620);

    }

    public void Start() {
    kinisi = new Thread(this);
    kinisi.start();
    running = true;
    }

    public void run() {
    while (running) {
    if (x < 340){
    double l = 200-f;
    double k = l/350;
    double y=k*x+f-30;
    x= x+1;
    m = (int) x;
    n = (int) y;
    repaint();
    try {
    Thread.sleep(μ);
    } catch (InterruptedException ie) {}
    }
    }
    }
    public void update(Graphics g) {
    if(i==null){
    i=createImage(800,700);
    doubleg = i.getGraphics();
    }
    doubleg.setColor(getBackground());
    doubleg.fillRect(0,0,800,700);
    doubleg.setColor(getForeground());
    paint1(doubleg);
    g.drawImage(i,0,0,this);
    }


    public void paint1(Graphics g){
    g.drawLine(0, f ,350, 200);
    g.drawOval(m, n, 40, 40);
    Color brown=new Color(137,66,0);
    g.setColor(brown);
    g.fillOval(m, n, 40, 40);
    }

    public void pause() {
    if (kinisi != null) {
    running = false;
    }
    }

    public boolean Reset() {
    if (kinisi != null) {
    running = false;
    kinisi.interrupt();
    kinisi = null;
    x=0;
    y=0;
    f=30;
    m=0;
    n=0;
    repaint();
    textField1.setText("");
    textField2.setText("");
    }
    Graphics g = getGraphics();
    g.drawOval(m,n,40,40);
    Color brown=new Color(137,66,0);
    g.setColor(brown);
    g.fillOval(m, n, 40, 40);
    return false;
    }

    public static void main(String[] args) {
    JApplet applet = new JApplet();
    applet.getContentPane().add(new all());
    applet.setSize(800, 700);
    applet.setVisible(true);
    }
    }

    Thank you very much and sorry for my english are not very good!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Problem with repaint() flickering

    Crossposted: java - How to stop repaint() flickering - Stack Overflow

    The OP ignored the 5 suggestions in the other postings. I'm sure anything we suggest here will also be ignored.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Problem with repaint() flickering

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Flickering problem in checkbox
    By dinu in forum Advanced Java
    Replies: 7
    Last Post: 04-25-2013, 05:32 PM
  2. problem with repaint bar
    By vitaly87 in forum New To Java
    Replies: 2
    Last Post: 07-12-2011, 03:37 PM
  3. repaint() problem ?
    By santa in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 12:47 PM
  4. flickering problem
    By dimril in forum Java 2D
    Replies: 5
    Last Post: 06-11-2010, 09:52 AM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 04:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •