user3050538
user3050538

Reputation: 89

Rectangle2D, borders go missing?

Helo guys,

my problem is that sometimes if my Rectangle2D gets a big width or height its bottom,left border splits and no longer makes a continous border and if I even make it wider the border goes smaller and smaller, like if there were a limitation of how long a rectangles border can be... It is really confusing and so far I have not found the solution :S I put there a link to a picture so you can see for yourself.

 new Rectangle2D.Double(mojrectangle.getUpperleftPointmojRectangle().getX(), mojrectangle.getUpperleftPointmojRectangle().getY(),1000,1000)
repaint();

enter image description here

thanks for your help..

BTW I have the same problem with Arc2D if it gets really big

UPDATE: I removed from the function the setStroke command and now it draws it correctly, but in the future I will need to set the Rectangles stroke so it leaves me sad.

@Override
 public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    Graphics2D g2=(Graphics2D)g;
 //g2.setStroke(stroke);
   g2.draw(rectangle);

}

Here I put an example code of my project, please try it with g2.setStroke(selectedstroke)- it wont work, and without it...I hope I explained myself clear .

package com.awtgraphicsdemo;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.JComboBox;

public class AWTgraphicsdemo extends Frame {

    final  float dash[] = {10.0f};
    final  float solid[] = {1000.0f};               // these must be in an Array
    float  lineWidth[]={2.0f,4.0f,6.0f,8.0f,10.0f}; // width of the drawing line
    String[] lineWidthString={"2.0","4.0","6.0","8.0","10.0"};
    JComboBox strokecombobox=new JComboBox(lineWidthString);
    BasicStroke selectedStroke =  new BasicStroke(lineWidth[0],BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, solid, 0.0f);

   public AWTgraphicsdemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTgraphicsdemo  awtGraphicsDemo = new AWTgraphicsdemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {


      Graphics2D g2 = (Graphics2D) g; 

      g2.setStroke(selectedStroke);
      g2.draw (new Rectangle2D.Double(10, 10, 1000, 900));
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Rectangle2D.Rectangle", 100, 120);
   }
}

Upvotes: 0

Views: 626

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

Better to:

  • Not override paint(...) in top level windows (as MadProgrammer states) since this also changes painting of borders and child components -- a dangerous thing to do.
  • Instead override paintComponent(...) of a JPanel (again as MadProgrammer states) and place that JPanel into your top level window.
  • Don't set the Stroke of the Graphics object passed into your painting method, but rather a copy of the Graphics object so not to have side effects down the road.

e.g.,

public class MyPanel extends JPanel {
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setStroke(....);

    // do drawing with g2 here

    g2.dispose();

  }
}

Upvotes: 0

user3050538
user3050538

Reputation: 89

Helo again,

I figured out my problem, it was in the properties of stroke,so after some lenght of the compoment the stroke got activated which made changed to the drawn shape.By modifying the strokes solid array I was able the get the result I wanted.

Thank you for your help and suggestions :)

Take Care

Upvotes: 1

Related Questions