• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

BufferedImage ?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all
i am attempting to draw a polygon (in this case a pentagon) and have succeeded in getting it drawn and displayed on JPanel.
trouble starts when i try to texture it so that the pentagon stands out from the rectangle it is painted on.
please help. SEE "// TROUBLE STARTS HERE" comment
compiler errors so far are
MyPoly7A.java [47:1] cannot resolve symbol
symbol : constructor BufferedImage (int[],int[],int,int)
location: class java.awt.image.BufferedImage
BufferedImage buffImage = new BufferedImage ( xy [ 0 ], xy [ 1 ], 5, BufferedImage.TYPE_INT_RGB );
^
MyPoly7A.java [49:1] cannot resolve symbol
symbol : constructor TexturePaint (java.awt.image.BufferedImage,java.awt.Polygon)
location: class java.awt.TexturePaint
TexturePaint tp = new TexturePaint ( buffImage, new Polygon( xy [ 0 ], xy [ 1 ], 5 ) );
^
MyPoly7A.java [51:1] cannot resolve symbol
symbol : method fillPoly (int,int,int,int)
location: class java.awt.Graphics2D
gg.fillPoly(10,10,200,300);
^
3 errors
Errors compiling MyPoly7A.



[ April 22, 2004: Message edited by: Douglas Braxton ]
[ April 22, 2004: Message edited by: Douglas Braxton ]
[ April 22, 2004: Message edited by: Douglas Braxton ]
[ April 22, 2004: Message edited by: Douglas Braxton ]
[ May 05, 2004: Message edited by: Douglas Braxton ]
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the 1.4 javadoc for BufferedImage I don't see a ctor thast takes 4 ints. There is one that takes 3 ints and an IndexColorModel. That might cause the first problem.
The other errors may be similar things. TexturePaint doesn't take a polygon but a Rectangle2D.
[ April 22, 2004: Message edited by: John Wetherbie ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I'm not sure what do you want to do, but I changed your code so it now draws the pentagon and fills it with pattern of green hexagones.
You can play with sizes, shapes and colors.
Hope it helps.
Best regards.
Polina

/*
* MyPoly7A.java
*
* Created on April 21, 2004, 2:59 PM
*/
/**
*
* @author dbraxton
*/
// java packages are imported
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
// Graphics2D component
class MyPoly7A extends JPanel
{ public MyPoly7A()
{
setBackground( Color.red );
}
protected void paintComponent ( Graphics g )
{
super.paintComponent ( g );
Graphics2D g2d = ( Graphics2D ) g;
//g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
int width = getWidth ( );
int height = getHeight ( );
int cx = width / 2;
int cy = height / 2;
int dia = Math.min ( width, height ) / 4;
int strut = Math.min ( width, height ) * 3/4;
g2d.setPaint ( Color.blue );
g2d.draw ( new Rectangle2D.Double ( width / 16, height / 16, width * 7/8, height * 7/8 ) );
int R = ( int )( strut / (4 * Math.cos(Math.PI/5) * Math.sin( Math.PI / 5 ) ) );
int[ ] [ ] xy = generateShapeArrays( R, 5, cx, cy );

Polygon pentagon = new Polygon( xy [ 0 ], xy [ 1 ], 5);
g2d.setPaint( Color.white );
g2d.draw( pentagon );
// TROUBLE STARTS HERE LINE 47 follows
BufferedImage buffi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB);
Graphics2D buffig = buffi.createGraphics();
buffig.setColor(Color.white);
Rectangle textureRect= new Rectangle(0, 0, 15, 15);
buffig.fill(textureRect);
buffig.setColor(Color.green);
int[ ] [ ] xyp = generateShapeArrays(textureRect.height/2, 6, textureRect.x+textureRect.width/2, textureRect.y+textureRect.height/2);
Polygon smallHexagon = new Polygon( xyp [ 0 ], xyp[ 1 ], 6);
buffig.fill(smallHexagon);
Rectangle r = new Rectangle(0,0,25,25);
g2d.setPaint(new TexturePaint(buffi, r));
g2d.fill(pentagon);
}
private int [ ] [ ] generateShapeArrays(int R, int sides, int cx, int cy)
{
int[ ] x = new int[sides];
int[ ] y = new int[sides];
int radInc = 0;
if (sides % 2 == 0)
radInc = 1;
for(int counter = 0; counter < sides; counter++)
{
x [ counter ] = cx + ( int ) ( R * Math.sin ( radInc * Math.PI / sides ) );
y [ counter ] = cy - ( int ) ( R * Math.cos ( radInc * Math.PI / sides ) );
radInc += 2;
}
return new int[ ][ ]
{
x, y
};
}
}
 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
polina and john
thanks for your help.
i implemented some of both your suggestions and eventually got it like i wanted it.
it now fills the poly with a pattern of small smiley faces.
doug
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic