package game;
import java.awt.*;
import javax.swing.*;
public class Panel extends JPanel {
private int txtx, txty;
private int rectx, recty;
private int ovalx, ovaly;
private boolean start, running;
public Panel() {
txtx = 50;
txty = 50;
rectx = 50;
recty = 80;
ovalx = 50;
ovaly = 210;
start = true;
setBackground(Color.WHITE);
setForeground(Color.BLACK);
setFont(new Font("Ariel", Font.PLAIN, 20));
run();
}
public void run(){
if(start){
running = true;
gameloop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawString("diz iz mah texz", txtx, txty);
g2.setColor(Color.RED);
g2.fillRect(rectx, recty, 100, 100);
g2.setColor(Color.BLUE);
g2.fillOval(ovalx, ovaly, 100, 100);
}
public void gameloop() {
while (running) {
rectx++;
repaint();
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}
package game;
import javax.swing.*;
public class Game {
public static void main(String[] args) {
new Game();
}
public Game() {
JFrame f = new JFrame("My Graphics");
Panel p = new Panel();
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.setVisible(true);
}
}