There is a drawn graph, it marks the boundaries of a certain area: 
I thought that it was possible to make lines parallel to the y axis with a small step, and somehow rotate them by selecting an arbitrary area by points, but even then the whole area will turn and you will not understand what.
What better way to do this? Looking exactly sloping lines.
In the paint it would look like this:
(color / style is not important, then I'll change it). Thank you in advance.
|
1 answer
You can create a java.awt.geom.Area object containing the desired area, and then paint it using the striped java.awt.Paint implementation. You can also limit this area to the drawing area and shade it manually.
import java.awt.*; import java.awt.MultipleGradientPaint.CycleMethod; import java.awt.geom.*; import java.util.function.DoubleUnaryOperator; import javax.swing.*; public class DiagonalPaint { static void initUi() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); DoubleUnaryOperator function = x -> -0.5 * (x-3)*(x-3) + 2; frame.add( new JPanel() { static final double SCALE = 40; static final double STEP = 0.01; static final double LEFT = 0; static final double RIGHT = 10; @Override public void paintComponent( Graphics g ) { Graphics2D g2d = (Graphics2D)g.create(); g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g2d.setColor( Color.WHITE ); g2d.fillRect( 0, 0, getWidth(), getHeight() ); // ΡΡΠΎΠ±Ρ Π½Π΅ ΠΏΠ΅ΡΠ΅ΡΡΠΈΡΡΠ²Π°ΡΡ Π²ΡΠ΅ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΡΠ°Π·, Ρ ΡΠΌΠ΅ΡΠ°Ρ ΠΈ ΠΏΠ΅ΡΠ΅Π²ΠΎΡΠ°ΡΠΈΠ²Π°Ρ // Π²Π΅ΡΡ ΡΠΊΡΠ°Π½ (Π½Π°Π΄ΠΏΠΈΡΠΈ ΡΠΎΠΆΠ΅ ΠΏΠ΅ΡΠ΅Π²Π΅ΡΠ½ΡΡΡΡ) AffineTransform transform = AffineTransform.getTranslateInstance( getWidth() / 2, getHeight() / 2 ); transform.concatenate( AffineTransform.getScaleInstance( 1, -1 ) ); g2d.setTransform( transform ); // ΠΎΡΠΈ g2d.setColor( Color.BLACK ); g2d.drawLine( -1000, 0, 1000, 0 ); for ( int i = -10; i < 10; i += 1 ) { g2d.drawLine( (int)(i * SCALE), -5, (int)(i * SCALE), 5 ); } g2d.drawLine( 0, -1000, 0, 1000 ); for ( int i = -10; i < 10; i += 1 ) { g2d.drawLine( -5, (int)(i * SCALE), 5, (int)(i * SCALE) ); } // ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Area Ρ Π·Π°ΠΊΡΠ°ΡΠΈΠ²Π°Π΅ΠΌΠΎΠΉ ΠΎΠ±Π»Π°ΡΡΡΡ Area area = createGraphArea( function, 1.5, 3 ); area.transform( AffineTransform.getScaleInstance( SCALE, SCALE ) ); // ΠΌΠ°ΡΡΡΠ°Π±ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ // Π»ΠΈΠ½Π΅ΠΉΠ½ΡΠΉ Π³ΡΠ°Π΄ΠΈΠ΅Π½Ρ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π·Π°Π»ΠΈΠ²ΠΊΠΈ g2d.setPaint( new LinearGradientPaint( 0, 0, // ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ Π½Π°ΡΠ°Π»Π° ΠΈ ΠΊΠΎΠ½ΡΠ° Π»ΠΈΠ½ΠΈΠΈ 5, 5, // ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΡΡ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΡΠΈΡΠΈΠ½Ρ ΡΠ°Π³Π° Π³ΡΠ°Π΄ΠΈΠ΅Π½ΡΠ° new float[] {0.4999f, 0.5000f, 0.5001f, 1f}, // ΡΠΎΡΠΊΠΈ Π΄Π»Ρ ΡΠ²Π΅ΡΠΎΠ² // ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ ΠΌΠ΅ΠΆΠ΄Ρ ΠΎΡΠ°Π½ΠΆΠ΅Π²ΡΠΌ ΠΈ Π±Π΅Π»ΡΠΌ ΠΎΡΠ΅Π½Ρ ΠΌΠ°Π», ΠΏΠΎΡΡΠΎΠΌΡ Π±ΡΠ΄Π΅Ρ ΠΊΠ°Π·Π°ΡΡΡΡ ΡΠ΅Π·ΠΊΠΈΠΌ new Color[] {Color.ORANGE, Color.ORANGE, Color.WHITE, Color.WHITE}, CycleMethod.REFLECT ) ); g2d.fill( area ); // ΠΠ»ΡΡΠ΅ΡΠ½Π°ΡΠΈΠ²Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ - ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΎΠ±ΡΠ΅Π·ΠΊΡ ΠΏΠΎ ΡΠΎΡΠΌΠ΅ ΠΎΠ±Π»Π°ΡΡΠΈ // ΠΈ Π·Π°ΡΠΈΡΠΎΠ²Π°ΡΡ Π»ΠΈΠ½ΠΈΡΠΌΠΈ Π²ΡΡΡΠ½ΡΡ /* Shape oldClip = g2d.getClip(); g2d.setClip( area ); Rectangle bounds = area.getBounds(); g2d.setPaint( Color.ORANGE ); for ( int y = bounds.y - bounds.width; y < bounds.y + bounds.height; y += 10) { g2d.drawLine( bounds.x, y, bounds.x + bounds.width, y + bounds.width ); } g2d.setClip( oldClip ); /**/ // ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΠΎ ΠΎΠ±ΡΠΈΡΠΎΠ²Π°ΡΡ ΠΎΠ±Π»Π°ΡΡΡ ΠΏΡΠ½ΠΊΡΠΈΡΠΎΠΌ /* g2d.setPaint( Color.ORANGE ); g2d.setStroke( new BasicStroke( 1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, new float[] { 10f }, 0f ) ); g2d.draw( area ); /**/ // ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° ΡΠ°ΠΌΠΎΠ³ΠΎ Π³ΡΠ°ΡΠΈΠΊΠ° Path2D line = createGraphPath( function, LEFT, RIGHT ); line.transform( AffineTransform.getScaleInstance( SCALE, SCALE ) ); g2d.setPaint( Color.BLACK ); g2d.setStroke( new BasicStroke( 2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL ) ); g2d.draw( line ); g2d.dispose(); } private Path2D.Double createGraphPath( DoubleUnaryOperator function, double left, double right ) { Path2D.Double line = new Path2D.Double(); line.moveTo( left, function.applyAsDouble( left ) ); for ( double x = left + STEP; x < right; x += STEP ) { line.lineTo( x, function.applyAsDouble( x ) ); } line.lineTo( right, function.applyAsDouble( right ) ); return line; } // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΏΠΎΠ΄ Π³ΡΠ°ΡΠΈΠΊΠΎΠΌ private Area createGraphArea( DoubleUnaryOperator function, double left, double right ) { Path2D.Double path = createGraphPath( function, left, right ); Rectangle2D bounds = path.getBounds2D(); path.lineTo( bounds.getMaxX(), 0 ); path.lineTo( bounds.getMinX(), 0 ); path.closePath(); return new Area( path ); } }); frame.setSize( 800, 600 ); frame.setVisible( true ); } public static void main(String[] args) { SwingUtilities.invokeLater( DiagonalPaint::initUi ); } } Instead of LinearGradientPaint you can use TexturePaint .
|
