الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

GUI question

مغلق
بدأه المبرمج1 في 7 أبريل 2004 · 2 رد · 576 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

the following code draw simply two lines..

the problem , there is a bug that i could not find it ..

if i run the prograe some times i tdraw the lines ... ans some times not .. i dont know why ..

if you have any idea about the bug .. can you inform me

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

public class DrawShapes extends JFrame {

  private Color color = Color.black;
  private Graphics g;
  private Graphics2D g2D;
  private int LineWidth;

  public DrawShapes()
  {
   super("test Paint Class");
   setSize(400,400);
   setVisible(true);
  }

  //set&get the color---------------------------------------------------------------
  public void setColor2(Color newColor)
  {  color = newColor; }//end method setColor

  public Color getColor()
  {return color;}// end method getColor

  // method to set the values of Graphics, Color, ------------------------------
  public void setG()
  {
    //g = super.getGraphics();
    g = getContentPane().getGraphics();
    g.setColor(color);
    g2D = (Graphics2D) g;
  }// end method setG

  // to draw a line-------------------------------------------------------------
  public void drawLine2(Point Start , Point End)
  {
     setG();
     g2D.drawLine(Start.x , Start.y , End.x , End.y);
  }//end method

  public static void main(String[] args) {

   DrawShapes d = new DrawShapes();
   Point p1 = new Point(30,50);
   Point p2 = new Point(150,260);

   d.drawLine2(p1,p2);

   d.setColor2(Color.white);
   p1.setLocation(150,260);
   p2.setLocation(150,10);
   d.drawLine2(p1,p2);
   System.out.println();

 }//end the main method




}// end class DrawShapes
#2

you will have to put drawing code in a method called

pain(Graphics g)

If you didn't overwrite this method the drowings will be removed after any events that requires the frame to redrow itself.

try that see if it works

#3

OK ... thanks

i think this is one reason for the problem

هذا الموضوع مغلق.

مواضيع مشابهة