2

i still have to learn a lot about programming (java), i have mostly used Graphics till now. I tried making a menubar, and it worked. But i have problems using Graphics and JMenubar together. First i coulde draw Graphics at all, but after looking on some site's (including this one), i managed to get Graphics working. Unfortunately, not Double Buffering.

This is how i have been Double Buffering, I do not know if this is a bad way to Double Buffer:

import java.awt.*;
import javax.swing.*;


public class DoubleBuffering extends JFrame
{
private Image dbImage;
private Graphics dbg;
int i = 1;

public DoubleBuffering()
{
    setTitle("Double Buffering");
    setVisible(true);
    setResizable(false);
    setSize(400, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void paint(Graphics g)
{
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}

public void paintComponent(Graphics g)
{
    g.setColor(Color.black);
    g.drawString("Text", 75, 71);

    i++;
    g.drawRect(50 + i, 100, 50, 50);

    repaint();
}

public static void main(String[] args)
{
    DoubleBuffering DB = new DoubleBuffering();
}

}

And this is my code, with only the menubar and graphics. Main class:

import javax.swing.*;


public class MenubarTest extends JFrame
{
JMenuBar menubar = new JMenuBar();
private drawing drawing;


public MenubarTest()
{
    setTitle("JTextField test");
    setVisible(true);
    setSize(400, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.drawing = new drawing();
    this.add(drawing);

    setJMenuBar(menubar);
    menubar.setSize(getWidth(), 10);

    JMenu menuA = new JMenu("Menu A");
    menubar.add(menuA);
    JMenuItem ItemA1 = new JMenuItem("Item A1");
    menuA.add(ItemA1);


    JMenu menuB = new JMenu("Menu B");
    menubar.add(menuB);
    JMenuItem itemB1 = new JMenuItem("Item B1");
    menuB.add(itemB1);
}

public static void main(String[] args)
{
    MenubarTest mt = new MenubarTest();
}
}

drawing class:

import java.awt.*;
import javax.swing.*;


public class drawing extends JPanel
{
int i;

public void paintComponent(Graphics g)
{
    g.setColor(Color.black);
    g.drawString("Text", 75, 71);

    i++;
    g.drawRect(50 + i, 100, 50, 50);
}
}

I hope you can help me, i have been searching for a while and i found some ways to Double Buffer, but they didn't work. Some of them were BufferedImage and BufferStrategy.

Thank you for your time.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • `public void paintComponent(Graphics g) { g.setColor(Color.black);` should be `public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black);`. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 11 '12 at 22:58
  • `JFrame` has no `paintComponent(Graphics)` method. Confirm it with `@Override` notation. – Andrew Thompson Sep 11 '12 at 23:00
  • Thank you, it worked. paintComponent() is repeating and the rectangle moves to the right. Thank you. –  Sep 12 '12 at 16:31

0 Answers0