0

I am trying to make a GUI using Java graphics, but for some reason it won't work. Here is the code:

public class ScreenCap extends Canvas {

/**
 * @param args the command line arguments
 */
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public static void main(String[] args) {
    new ScreenCap();
}

public ScreenCap() {
    Window window = new Window(this);
    window.setVisible(true);
    this.addMouseListener(new MouseHandler());
    drawComponents();
}

private void drawComponents() {
    System.out.println("in draw");
    createBufferStrategy(3);
    BufferStrategy bs = getBufferStrategy();

    Graphics g = bs.getDrawGraphics();

    g.setColor(Colors.BG);
    g.fillRect(0, 0, getWidth(), getHeight());
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
muuk
  • 932
  • 1
  • 7
  • 15
  • What doesn't work? Is this the complete code? It doesn't compile – Paul Samsotha Dec 25 '13 at 16:56
  • There should be related question asked many times before. Someone will just need to find correct one and close this question. – Sage Dec 25 '13 at 16:57
  • 1
    Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Dec 25 '13 at 17:18
  • 1
    Why did you include a "Swing" tag? You are using a Canvas which is an AWT component. You are using a BufferStrategy, which is not need with Swing because Swing is double buffered by default. I suggest you do use Swing instead of AWT. Read the section from the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for information on how do to painting in Swing along with working examples. – camickr Dec 25 '13 at 17:20

1 Answers1

2

I would consider using Swing instead of AWT. AWT is pretty out-dated. If using swing, you would do something like the below code

  • Subclass JPanel
  • Override paintComponent(Graphics g)
  • Cast to Graphics2D (optional) for more versatilitiy
  • Draw in the paintComponent method
  • Add an instance of your panel class to a container.

Read more on Graphics here. Loaded with tutorials. More on Swing here.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SwingDemo extends JPanel {
    private static final int DIM_WIDTH = 500;
    private static final int DIM_HEIGHT = 500;


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        g2.setColor(Color.BLUE);
        g2.fillRect(100, 100, 200, 200);
    }

    public static void createAndShowGui(){
        JFrame frame = new JFrame();
        frame.add(new SwingDemo());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize(){
        return new Dimension(DIM_WIDTH, DIM_HEIGHT);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                createAndShowGui();
            }
        });
    }
}

enter image description here

Nothing spectacular :)

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720