using Graphics g in inner class in java - java

Why can not I see the following output?
g2.drawString(new Date().toString(), 0, 150);
(i used g2 (global variable) for using g in paint method in inner class).
Thanks a lot in advance!
public class RedRect extends Frame
{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RedRect();
}
});
}
public static Point p;
RedRect()
{ super("RedRect");
try{
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}});
setSize (800, 600);
add("Center", new CvRedRect());
show();
}
catch(Exception ex){
}
}
}
class CvRedRect extends Canvas
{
Vector<Point> v=new Vector<Point>();
CvRedRect(){
addMouseListener((new MouseAdapter() {
public void mousePressed(MouseEvent evt){
if(v.size()<3){
v.add(evt.getPoint());
}
else{
flag=false;
//v.removeElementAt(0);
//v.add(evt.getPoint());
}
p=evt.getPoint();
repaint();
}
}));
}
Point p=new Point();
boolean flag=true;
int i=0;
public static Graphics g2;
public void paint(Graphics g)
{
try{
g2=g;
Dimension d = getSize();
int maxX = d.width - 1, maxY = d.height - 1;
g.setColor(Color.red);
g.drawRect(0, 0, maxX, maxY);
g.drawString("x ="+p.x, 10, 30);
g.drawString("y = " +p.y, 10, 60);
if(v.size()>2){
g2.drawLine(v.get(0).x,v.get(0).y,v.get(1).x,v.get(1).y);
g2.drawLine(v.get(0).x,v.get(0).y,v.get(2).x,v.get(2).y);
g2.drawLine(v.get(1).x,v.get(1).y,v.get(2).x,v.get(2).y);
Thread t=new Thread(){
public void run(){
try{
while(flag){
///The following comand//////
g2.drawString(new Date().toString(), 0, 150);
///////////////////////////
Thread.sleep(300);
System.out.println (v.size());
Thread.sleep(300);
}
}
catch(Exception ex){
System.out.println (ex.getMessage());
}
}
};
t.start();
}
//System.out.println ("size="+v.size());
if(!flag){
g.clearRect(0,0,maxX,maxY);
Thread.sleep(1000);
g.drawString("FINISH", 5, 30);
flag=true;
}
}
catch(Exception ex){
}
}
}

The reason your code isn't working is because you're using Graphics wrong. The Graphics object should not be a class field much less a static one. It is not a long-lived object, and so if you try to have it persist in this way, you'll end up with either a null reference or a non-null reference that doesn't work.
Instead if your program is an AWT program, then use the Graphics object inside of the paint method where the JVM supplies you with it when it calls paint. Otherwise if Swing do likewise with the paintComponent method.
Suggestions:
First and foremost, don't code AWT unless this is an absolute requirement for a class. Instead use Swing.
Next read the Swing graphics tutorials as they'll teach you all this and more.
Next do your drawing within the paintComponent(...) overload of a JPanel or other component that extends JComponent.
Don't forget to call the super.paintComponent(g) method within your override.
Don't forget to annotate your override with the #Override annotation.
Use a Swing Timer to do what you're currently using a while loop and Thread.sleep(...) to do.

Related

Swing going from menu to actual gameplay

So I am making a space invaders clone. Originally I had no problem getting my game to work with a simple main class that created the frame, created the gameplay and started the thread.
But then I tried to implement a start menu and it all went to crap. The menu appears with success but the gameplay does not appear when I press start.
I am running out of ideas and I am completely stumped. I am somewhat new as well to SO, so if there is anything I left out, I appreciate any help.
Here is the original with no menu that worked fine:
public static void main(String[] args) {
JFrame frame = new JFrame("SpaceRaiders");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gameplay gameplay = new Gameplay();
frame.add(gameplay);
frame.setVisible(true);
Thread t1 = new Thread(gameplay);
t1.start();
}
However, the moment I tried to implement a menu to then play the game, I am running into all sorts of trouble. I created a UI class as well as an actual "game" class like so:
public class UI {
JFrame frame, f2;
JPanel titrePanel, startButtonPanel, loadButtonPanel, p2;
JLabel nomJeu;
JButton startButton, loadButton;
Font fontTitre, fontStart;
Gameplay gameplay;
public void createUI(ChoixJeu cj) {
frame = new JFrame("SpaceRaiders");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//------------------ECRAN MENU---------------------
//Titre
titrePanel = new JPanel();
titrePanel.setBounds(100, 100, 400, 100);
titrePanel.setBackground(Color.BLUE);
Font fontTitre = new Font("Times New Roman", Font.BOLD, 50);
Font fontStart = new Font("Times New Roman", Font.PLAIN, 20);
nomJeu = new JLabel("SpaceRaiders");
nomJeu.setForeground(Color.white);
nomJeu.setFont(fontTitre);
titrePanel.add(nomJeu);
//Start button
startButtonPanel = new JPanel();
startButtonPanel.setBounds(200, 400, 200, 40);
startButtonPanel.setBackground(Color.BLACK);
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
startButton.setFont(fontStart);
startButton.setFocusPainted(false);
startButton.addActionListener(cj);
startButton.setActionCommand("start");
startButtonPanel.add(startButton);
//Load Button
loadButtonPanel = new JPanel();
loadButtonPanel.setBounds(200, 440, 200, 100);
loadButtonPanel.setBackground(Color.BLACK);
loadButton = new JButton("LOAD");
loadButton.setBackground(Color.BLACK);
loadButton.setForeground(Color.WHITE);
loadButton.setFont(fontStart);
loadButton.setFocusPainted(false);
titrePanel.add(nomJeu);
loadButtonPanel.add(loadButton);
frame.add(startButtonPanel);
frame.add(titrePanel);
//------------------ECRAN MENU FIN---------------------
frame.setVisible(true);
}
And the game class...
public class Jeu {
ChoixJeu cj = new ChoixJeu();
UI ui = new UI();
Ecrans e = new Ecrans(ui);
Gameplay gp;
public static void main(String[] args) {
new Jeu();
}
public Jeu() {
ui.createUI(cj);
Gameplay gameplay = new Gameplay();
this.gp = gameplay;
}
public class ChoixJeu implements ActionListener {
#Override
public void actionPerformed(ActionEvent ae) {
String yourChoice = ae.getActionCommand();
switch (yourChoice) {
case "start":
e.montrerEcranJeu();
new Thread(gp).start();
ui.frame.add(gp);
break;
default:
break;
}
}
}
}
I also tried to make a class/method that hides the menu panels
public void montrerEcranJeu() {
//Cache Menu
ui.titrePanel.setVisible(false);
ui.startButtonPanel.setVisible(false);
//Montre Jeu
// ui.frame.add(gameplay);
}
And just in case the Gameplay class. The run() method is at the bottom
public class Gameplay extends JPanel implements KeyListener, ActionListener, Runnable {
private Ship player = new Ship(new Point(200, 555));
Timer t = new Timer(5, this);
private ArrayList<Laser> lasers = new ArrayList<Laser>();
private int laserNb;
private boolean readytofire;
private boolean shot = false;
private ArrayList<Invader> invaders = new ArrayList<Invader>();
private boolean pause;
public Gameplay() {
super();
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
for (int j = 0; j < 80; j += 20) {
for (int i = 0; i < 20; i++) {
invaders.add(new Invader(5 + i * 30, j));
}
}
}
public boolean addLaser(Laser a) {
lasers.add(a);
return true;
}
public boolean addPlayer(Ship p) {
this.player = p;
return true;
}
#Override
public void keyTyped(KeyEvent ke) {
}
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_RIGHT == e.getKeyCode()) {
moveRight();
}
if (KeyEvent.VK_LEFT == e.getKeyCode()) {
moveLeft();
}
if (KeyEvent.VK_SPACE == e.getKeyCode()) {
shoot();
System.out.println("Space Action from Gameplay is working");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void moveRight() {
if (player.getCentre().getX() >= 580) {
player.setX(580);
} else {
double movement = player.getCentre().getX();
movement += 10;
player.setX(movement);
}
this.repaint();
}
public void moveLeft() {
if (player.getCentre().getX() <= 20) {
player.setX(20);
} else {
double movement = player.getCentre().getX();
movement -= 10;
player.setX(movement);
}
this.repaint();
}
public void shoot() {
shot = true;
if (readytofire) {
Point top = new Point(player.getTopX(), player.getTopY());
Laser laser = new Laser(top);
addLaser(laser);
}
}
public void moveShot() {
if (shot) {
for (Laser l : lasers) {
l.setY(l.getTopLeft().getY() - 1);
}
}
}
#Override
public void paint(Graphics g) {
setBackground(Color.black);
super.paint(g);
player.draw(g);
for (Laser l : lasers) {
l.draw(g);
}
for (Invader i : invaders) {
i.draw(g);
}
}
// public void paintComponent (Graphics g){
// Controle Thread
public void run() {
while (true) {
moveShot();
for (Invader i : invaders) {
i.moveAndUpdate();
}
// for (Invader i : invaders) {
// if (){
// System.out.println("YOU ARE DEAD!");
// }
// }
try {
Thread.sleep(10);
readytofire = true;
} catch (InterruptedException ex) {
Logger.getLogger(Gameplay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
So, using null layouts is the beginning of your problems. I might recommend using CardLayout which is designed to help you dynamically switch between views. See How to Use CardLayout for more details. I'd also suggest taking the time to read through Laying Out Components Within a Container and finding one or more appropriate layouts to support your menu.
You're also making a lot of fundamental mistakes. Swing is not thread safe, so you should avoid updating the UI (or something the UI depends on) from outside the context of the EDT - see Concurrency in Swing for more information and How to Use Swing Timers for a possible solution.
As a general recommendation, you should avoid overriding paint and, in the case of classes which extend from JComponent, prefer paintComponent instead. You should also avoid call methods which might change the state of the component during a paint cycle, this can increase the number of repaint requests and degrade the performance of your program (ie, don't call setBackground inside paint).
Have a look at Performing Custom Painting and Painting in AWT and Swing for more details about how the paint system works and how best you can work with it.
You should also avoid KeyListener, this is likely to cause you issues when you introduce other, focusable, components into the picture. Instead, you should favour the Key bindings API instead
I've read through [insert link or tutorial], but it still doesn't help...
And forgive me if this doesn't happen all the time.
The point of providing you the tutorial links is to encourage you to learn something;
Learn where to find answers to your questions
Learn how the APIs work
Expand your knowledge and understanding of how the APIs work
Having said that, they're not always "obvious" as to the solution. What I do when I'm in this situation is start with one or more new projects, dedicated to just working on that aspect of the API I'm trying to understand. For here I can explore the concepts in isolation and when I "think" I understand them, try and implement them into the project I'm working on. This might take a number of iterations, but once it works, I have gained a much deeper understanding and appreciation of the API then I would have gained from a simple "copy-n-paste" solution

Why doesn't the ball show up in the frame if I add the ball after the for loop?

The program makes a ball glide across from top left to bottom right and works. But if I were to shift the line
frame.getContentPane().add(ball);
from its current position to after the for loop, why doesn't the ball show up on the frame.
I agree that the ball should no longer move, because all the shifting done in the for loop happens even before I add the ball to the JFrame,but I don't understand why the ball doesn't show up on the screen when I ultimately add it to the frame.
Here's the code of the working program, if you shift the line mentioned above to after the for loop, the ball no longer shows up
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Animate
{
private JFrame frame;
private int x,y;
public static void main(String args[])
{
Animate ballRoll = new Animate();
ballRoll.go();
}
public void go()
{
frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyRoll ball = new MyRoll();
frame.getContentPane().add(ball);
for(x = 5;x<=350;x++)
{
y=x;
try
{
Thread.sleep(50);
}
catch(Exception e)
{
System.out.println("dsfsd");
}
ball.repaint();
}
}
class MyRoll extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.ORANGE);
g.fillOval(x, y, 100, 100);
}
}
}
Some points to remember:
Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.
Read more How to Use Swing Timers
Don't forget to call super.paintComponent() in overridden paintComponent() method.
Call frame.setVisible(true) in the end after adding all the components.
Use frame.pack() instead of frame.setSize(500,500) that fits the components as per component's preferred size.
Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.
Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.
Read more
Why to use SwingUtilities.invokeLater in main method?
SwingUtilities.invokeLater
Should we use EventQueue.invokeLater for any GUI update in a Java desktop application?
Sample code: (change it as per your custom painting)
private Timer timer;
...
timer = new javax.swing.Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
y = ++x;
ball.repaint();
if (x > 350) {
timer.stop();
}
}
});
timer.setRepeats(true);
timer.start();
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Animate ballRoll = new Animate();
ballRoll.go();
}
});
}
class MyRoll extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
#Override
public Dimension getPreferredSize() {
return new Dimension(..., ...);
}
}

MouseListener mouseClicked() missing first event

public class TesterApplication {
static JPanel CenterPanel;
public static void main(String[] args){
/* get image MapImg */
JFrame frame=new JFrame();
CenterPanel = new JPanel(){
#Override
protected void paintComponent(Graphics g){
g.drawImage(MapImg, 0, 0, null);
}
};
CenterPanel.addMouseListener(new LineBuildListener(new TesterApplication()));
frame.getContentPane().add(BorderLayout.CENTER, CenterPanel);
frame.setSize(x, y);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
now inner class
class LineBuildListener implements MouseListener {
TesterApplication TA;
int xFirstClick;
int yFirstClick;
int ClickCounter=0;
int xClick;
int yClick;
LineBuildListener(TesterApplication TA){
this.TA=TA;
}
#Override
public void mouseClicked(MouseEvent e) {
xFirstClick=xClick;
yFirstClick=yClick;
xClick=e.getX();
yClick=e.getY();
TA.CenterPanel.getGraphics().fillOval(xClick, yClick, 10, 10);
if(ClickCounter!=0){
SecondClick();
ClickCounter++;
}else{
ClickCounter++;
}
System.out.println(ClickCounter);
}
public void SecondClick(){
TA.CenterPanel.getGraphics().drawLine(xClick, yClick, xFirstClick,yFirstClick);
}
}
meanwhile I make first click, my GUI blink, Click Counter print that i have made 1 click, but yet i don't get my first circle. If i keep clicking everything work fine, it prints next circle, increase Counter and draw line between them, so i dont get why first circle is missing
Look at this:
xFirstClick=xClick;
yFirstClick=yClick;
xClick=e.getX();
yClick=e.getY();
xClick and yClick are not initialized the first time

Java painter program drawing shapes

I have a problem in this java code.
I want to make a painter program but whenever I choose a shape and draw it
all the shapes that were drawn before become the same as this shape. Here's the code.
I know that the problem is from the for statement in the paintComponent but what can I replace it with?
class inner extends JPanel implements MouseListener,MouseMotionListener{
private int oldx,oldy,newx,newy;
private Point point1,point2;
private Shape newRec,line1;
Rectangle currRec;
private Vector shape;
private boolean status,status1;
private int count=0;
private Object line;
inner(){
point1=point2=new Point(0,0);
shape = new Vector();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent event) {
point2=event.getPoint();
newx = point2.x;
newy = point2.y;
if(Universal==7){
line = new Object(oldx,oldy,newx,newy);
status = true;
repaint();
}
currRec = new Rectangle(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y));
status = true;
repaint();
}
#Override
public void mouseMoved(MouseEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
#Override
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent event) {
point1=event.getPoint();
oldx=event.getX();
oldy=event.getY();
}
#Override
public void mouseReleased(MouseEvent event) {
point2=event.getPoint();
newx=event.getX();
newy=event.getY();
//count++;
if(Universal==7){
line1 = new Shape(point1.x,point1.y,point2.x,point2.y);
shape.add(line1);
//Graphics g = getGraphics();
//g.setColor(colour);
//g.drawLine(point1.x,point1.y,point2.x,point2.y);
count++;
repaint();
}
else if(Universal==1||Universal==2||Universal==3||Universal==4||Universal==5||Universal==6){
newRec = new Shape(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y));
shape.add(newRec);
count++;
repaint();
}
}
///// the problem is in here
public void paintComponent(Graphics g){
super.paintComponent(g);
Shape c;
g.setColor(colour);
for(int i=0;i<shape.size();i++){
c = (Shape) shape.get(i);
if(Universal==1){
g.drawRect(c.x, c.y, c.w, c.h);
if(status){
g.drawRect(currRec.x, currRec.y, currRec.width, currRec.height);
}
}
if(Universal==2){
g.fillRect(c.x, c.y, c.w, c.h);
if(status){
g.fillRect(currRec.x, currRec.y, currRec.width, currRec.height);
}
}
if(Universal==3){
g.drawRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4);
if(status){
g.drawRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4);
}
}
if(Universal==4){
g.fillRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4);
if(status){
g.fillRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4);
}
}
if(Universal==5){
g.drawOval(c.x, c.y, c.w, c.h);
if(status){
g.drawOval(currRec.x, currRec.y, currRec.width, currRec.height);
}
}
if(Universal==6){
g.fillOval(c.x, c.y, c.w, c.h);
if(status){
g.fillOval(currRec.x, currRec.y, currRec.width, currRec.height);
}
}
if(Universal==7){
g.drawLine(c.x, c.y, c.w, c.h);
if(status){
g.drawLine(line1.x, line1.y, line1.w,line1.h);
}
}
if(Universal==8){
shape.clear();
}
}
}
Universal is only ever going to be a given value at any given time.
Paints are not accumulative, they are destructive.
That is, each time paintComponent is called, all the previous contents is removed/wiped clean and you are expected to "repaint" the contents.
Instead of relying on a single flag, you should add the Shapes to some kind of List and redraw them all when ever paintComponent is called. Equally, you could simply add the "type" (int) to a List and process that list on each repaint
Take a look at Painting in AWT in Swing for an explanation of the paint process
See Custom Painting Approaches for two different ways to do this:
Add shapes to a List and then repaint all the Shapes from the list every time paintComponent() is invoked.
Draw Shapes to a BufferedImage and then just repaint the image ever time paintComponent() is invoked.
Neither example does exactly what you want, it only shows you the approach.

How to "do something" on Swing component resizing?

I've a class which extends JPanel. I overwrote protected void paintComponent(Graphics g).
There is a variable which has to be recalculated when the panel's dimensions change. How do I do that in a proper way?
Like Adam Paynter suggested, you can also add an inner class to your code, like this:
class ResizeListener extends ComponentAdapter {
public void componentResized(ComponentEvent e) {
// Recalculate the variable you mentioned
}
}
The code you have entered between the innermost brackets will be executed everytime the component get resized.
Then you add this listener to your component with
myJPanel.addComponentListener(new ResizeListener());
You can get your component by using e.getComponent(). This way you can call any method of your component from inside the inner class like
e.getComponent().getWeight();
I suppose you could override the various setSize and resize methods and perform the calculation there. However, you may not find all the places where the size can be changed. You may want to have your class implement ComponentListener and simply listen to itself for resize events.
Warning: I am not a Swing expert.
Warning: I have not compiled this code.
public class MyJPanel extends JPanel implements ComponentListener {
public MyJPanel() {
this.addComponentListener(this);
}
public void paintComponent(Graphics g) {
// Paint, paint, paint...
}
public void componentResized(ComponentEvent e) {
// Perform calculation here
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
}
If I understand the question correctly then you should read the section from the Swing tutorial on How to Write a Component Listener which shows you how to listen for a change in a components size.
If the calculation isn't time consuming, I would just re-calculate the value each time in paintComponent().
Otherwise, you can save a value that is the size of the component and check it against the new size in paintComponent. If the size changed, then recalculate, otherwise don't.
private Dimension size;
protected void paintComponent(Graphics g){
if (!size.equals(getSize())){
size = getSize();
// recalculate value
}
}
Or, you can do the calculation on a resize event.
//in the constructor add the line
addComponentListener(resizeListener);
private ComponentListener resizeListener = new ComponentAdapter(){
public void componentResized(ActionEvent e){
// recalculate value
}
};
The simplest way is to implement a ComponentListener:
myjpanel.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
//recalculate variable
}
});
Here, I have used a ComponentAdapter because I only intend on overriding componentResized().
Here's what I use (where CoordinatePlane is a JPanel):
I'm not an expert
public CoordinatePlane() {
setBackground(Color.WHITE);
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
//YOUR CODE HERE
}
});
}
It resizes automatically if it's
inside a BorderLayout panel and
put there as BorderLayout.CENTER component.
If it doesn't work, you probably have forgotten one of these two.
This simple example is drawing a red circle in the resized frame....
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.*;
import java.awt.geom.*;
public class RedCircle extends JFrame implements ComponentListener {
int getWidth;
int getHeight;
public RedCircle() {
super("Red Circle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentListener(this);
pack();
setVisible(true);
}
public void componentResized(ComponentEvent e) {
getWidth = e.getComponent().getWidth();
getHeight = e.getComponent().getHeight();
Panel pane = new Panel(getWidth,getHeight);
add(pane);
}
public static void main(String[] args) {
RedCircle rc = new RedCircle();
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
}
class Panel extends JPanel {
int panelWidth;
int panelHeight;
public Panel(Integer getWidth,Integer getHeight) {
panelWidth = getWidth;
panelHeight = getHeight;
}
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
int realWidth = panelWidth - 17;
int realHeight = panelHeight - 40;
float Height = (realHeight);
float Width = (realWidth);
// draw the Red Circle
comp2D.setColor(Color.red);
Ellipse2D.Float redCircle = new Ellipse2D.Float(0F, 0F, Width, Height);
comp2D.fill(redCircle);
}
}

Categories