How to draw something in a JFrame Canvas object using BufferStrategy ?
Closed due to the fact that the essence of the question is incomprehensible by the participants of Kromster , Pavel Mayorov , Alexey Shimansky , Grundy , cheops 23 Jun '16 at 6:33 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- “Please provide the source code” - and you have the documents, on what basis do we have to show you something? - Kromster
|
1 answer
Examples of mass, the main thing is to search:
package testframe; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import javax.swing.JFrame; public class Testframe extends Canvas implements Runnable { //FIELDS private static final long serialVersionUID = 1L; public static int WIDTH = 1024; public static int HEIGHT = WIDTH / 16 * 9; private JFrame frame; private boolean running; private Thread t1; public Testframe(){ Dimension size = new Dimension(WIDTH, HEIGHT); setPreferredSize(size); frame = new JFrame(); } public synchronized void start (){ running = true; t1 = new Thread (this); t1.start(); // calls run() } //Main runnable public void run(){ while (running){ update(); draw(); } } public void update(){} public void draw(){ BufferStrategy bs = getBufferStrategy(); if (bs== null){ createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.BLACK); g.fillOval(0, 0, 20, 20); g.dispose(); bs.show(); } public static void main(String[] args){ Testframe myPanel = new Testframe(); myPanel.frame.setResizable(false); myPanel.frame.setTitle("My frame"); myPanel.frame.add(myPanel); myPanel.frame.pack(); myPanel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myPanel.frame.setLocationRelativeTo(null); myPanel.frame.setVisible(true); myPanel.start(); } } Example with enSO
|