diff --git a/jdk/src/share/classes/java/awt/image/SampleModel.java b/jdk/src/share/classes/java/awt/image/SampleModel.java
index 2412da2db94..8b51116f75b 100644
--- a/jdk/src/share/classes/java/awt/image/SampleModel.java
+++ b/jdk/src/share/classes/java/awt/image/SampleModel.java
@@ -1315,9 +1315,16 @@ public abstract class SampleModel
                            int iArray[], DataBuffer data) {
 
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, iArray[Offset++], data);
             }
         }
@@ -1345,9 +1352,17 @@ public abstract class SampleModel
     public void setSamples(int x, int y, int w, int h, int b,
                            float fArray[], DataBuffer data) {
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
+
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, fArray[Offset++], data);
             }
         }
@@ -1375,9 +1390,18 @@ public abstract class SampleModel
     public void setSamples(int x, int y, int w, int h, int b,
                            double dArray[], DataBuffer data) {
         int Offset=0;
+        int x1 = x + w;
+        int y1 = y + h;
 
-        for (int i=y; i<(y+h); i++) {
-            for (int j=x; j<(x+w); j++) {
+
+        if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
+            y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
+        {
+            throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
+        }
+
+        for (int i=y; i<y1; i++) {
+            for (int j=x; j<x1; j++) {
                 setSample(j, i, b, dArray[Offset++], data);
             }
         }
diff --git a/jdk/test/java/awt/image/GetSamplesTest.java b/jdk/test/java/awt/image/GetSamplesTest.java
index 86780c5cf9f..2352f9efce2 100644
--- a/jdk/test/java/awt/image/GetSamplesTest.java
+++ b/jdk/test/java/awt/image/GetSamplesTest.java
@@ -23,9 +23,9 @@
 
 /*
  * @test
- * @bug     6735275
- * @summary Test verifies that SampleModel.getSamples() throws an appropriate
- *           exception if coordinates are not in bounds.
+ * @bug     6735275 6993561
+ * @summary Test verifies that SampleModel.getSamples() SampleModel.setSamples()
+ *          throw an appropriate exception if coordinates are not in bounds.
  *
  * @run     main GetSamplesTest
  */
@@ -75,6 +75,7 @@ public class GetSamplesTest {
 
         try {
             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
+            sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             iOk = true;
@@ -82,6 +83,7 @@ public class GetSamplesTest {
 
         try {
             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
+            sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             fOk = true;
@@ -89,6 +91,7 @@ public class GetSamplesTest {
 
         try {
             sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
+            sm.setSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
         } catch (ArrayIndexOutOfBoundsException e) {
             System.out.println(e.getMessage());
             dOk = true;